PDA

View Full Version : Solved: Want Document_Open To Trigger Once Only



johnske
10-08-2004, 03:51 PM
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) :bink:

EDITED since 1st post:
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

TonyJollans
10-08-2004, 04:54 PM
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?

johnske
10-08-2004, 05:28 PM
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?) :bink:

mdmackillop
10-09-2004, 02:25 PM
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

TonyJollans
10-09-2004, 04:13 PM
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:

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

Sub Page_Setup
' Do your stuff
End SUb

johnske
10-09-2004, 07:19 PM
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"...:bink:

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

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