Consulting

Results 1 to 6 of 6

Thread: Solved: Want Document_Open To Trigger Once Only

  1. #1
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location

    Solved: Want Document_Open To Trigger Once Only

    Hi, the basis of the problem is this: I have the following code that I want to run the very first time the Word doc is opened, but not after that.

    However I might very well change my mind about the page setup later on, so I'll include the public sub SetupPage that can be run manually to trigger the Open event later on.

    I can do this in XL by writing somethng in an obscure cell after the
    Document_Open event, but what is "an obscure cell that I can write something in" in Word? (the missing commands are written in as comments)

    EDITED since 1st post:
    [vba]Private Sub Document_Open()
    Dim Answer As VbMsgBoxResult
    'If (there is a note this sub has been run) Then Exit Sub
    Answer = MsgBox("Your preferred setup?", vbYesNo)
    'Leave a note indicating this sub has been run
    If Answer = vbNo Then Exit Sub
    With ActiveDocument.PageSetup
    .Orientation = wdOrientPortrait
    .TopMargin = InchesToPoints(0.6)
    .BottomMargin = InchesToPoints(0.97)
    .LeftMargin = InchesToPoints(0.6)
    .RightMargin = InchesToPoints(0.6)
    .PageWidth = InchesToPoints(8.27)
    .PageHeight = InchesToPoints(11.69)
    End With
    ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitBestFit
    Selection.Font.Name = "Verdana"
    ActiveDocument.Save
    MsgBox ("All done - And saved! :o)")
    End Sub

    Sub SetupPage()
    'Remove the note indicating Document_Open has been run
    Document_Open
    End Sub[/vba]

  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    I would use a Document Variable for what you ask, but ..

    I'm confused by what you're doing. You want to run something the first time a document is opened? That is after it's been created and saved once? Why not run it when the document is created? Or build your settings into the template?
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by TonyJollans
    I would use a Document Variable for what you ask, but ..

    I'm confused by what you're doing. You want to run something the first time a document is opened? That is after it's been created and saved once? Why not run it when the document is created? Or build your settings into the template?
    This is something I have not attempted before so I'm not quite sure how to go about it.... What I eventually want to do is this:

    When I click onto "new office cocument"/"Blank document" - to be offered the yes/no option of "my favourite page setup".

    If I select No and save this doc with the Office default setup, but then at any time later on decide to change this doc to "my" page setup I can do so at the click of a button....(if that makes sense?)

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Johnske,
    You could write a value to the registry, related to the particular document. Sample code here. http://www.vbaexpress.com/kb/getarticle.php?kb_id=208
    MD

  5. #5
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    The disadvantage of using the registry is that if you move the document you lose the association.

    You don't say how the Page Setup code would be invoked after first use - I presume a toolbar button or similar. I think a better solution would be to put the code in the Document_New event instead of the Document_Open. The disadvantage of this is that it is not fired when you start Word and it opens with a new document but it is fired when you create a new document via "New Office Document" etc.

    You could do something like:

    [VBA]Sub Document_New
    If Msgbox("Do you want etc") = vbYes Then Call Page_Setup
    End Sub

    Sub Page_Setup
    ' Do your stuff
    End SUb[/VBA]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  6. #6
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Thanx Tony, I found the following does the job OK. I didn't know there was a "Document_New" event so that's why I was trying to use "Document_Open"...

    Thank To you also MD, that's a handy one to know...

    [vba]Private Sub Document_New()
    Dim Answer As VbMsgBoxResult
    Answer = MsgBox("Your usual setup?", vbYesNo)
    If Answer = vbNo Then Exit Sub
    ActiveDocument.Save
    SetupPage
    End Sub


    Sub SetupPage()
    With ActiveDocument.PageSetup
    .Orientation = wdOrientPortrait
    .TopMargin = InchesToPoints(0.6)
    .BottomMargin = InchesToPoints(0.97)
    .LeftMargin = InchesToPoints(0.6)
    .RightMargin = InchesToPoints(0.6)
    .PageWidth = InchesToPoints(8.27)
    .PageHeight = InchesToPoints(11.69)
    ActiveDocument.Save
    End With
    ActiveDocument.ActiveWindow.View.Zoom.PageFit = wdPageFitBestFit
    Selection.Font.Name = "Verdana"
    MsgBox ("All done! :o)")
    End Sub[/vba]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •