Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running VBA when Word Starts

Status
Not open for further replies.

VBAWillow

Programmer
Jun 8, 2004
4
GB
I have modified Normal.dot to add a footer to a new document which works on all occasions except when Word is started.

I have some VBA code running when a new document is created but none of the usual Document_Open() or AutoNew() subs seem to work on the blank document that Word gives you when it loads.

Anyone any ideas?
 
Try AutoExec().

Failing that, have AutoNew()create another new document - which will have your footer - then close the first one.


Gerry
 
Hi VBAWillow,

This really is an awkward one. As you say, various Events that one might think ought to fire, don't. AutoExec runs before the document (or even window) is created.

The only Event that I know that fires after creation and before the User gets the document is the Application Document Change Event. If you'd like detailson how to use it, please post back.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Thanks Tony,

Using your suggestion I found this article at which worked:
-----------------------------------------------------------
Using Word 97
If you create a DocumentChange event and store it in an Addin, it will be triggered when any document is created, opened, closed or the focus changes from one document to another. You can take advantage of this to simulate AutoNew, AutoOpen, AutoClose macros (as well as being able to monitor when the focus changes). Unlike the application event DocumentChange, AutoNew, AutoOpen and AutoClose macros only behave globally if stored in Normal.dot.

You can do it as follows:

In a Module:

Option Explicit

Dim oAppClass As New ThisApplication
Public oldNoOfOpenDocs As Long
Public FirstNewDoc As Boolean

Public Sub AutoExec()
Set oAppClass.oApp = Word.Application
oldNoOfOpenDocs = 0
FirstNewDoc = True
End Sub

In a Class Module (name this ThisApplication):

Option Explicit

Public WithEvents oApp As Word.Application


--------------------------------------------------------------------------------

Private Sub oApp_DocumentChange()

On Error GoTo ExitCode

Dim newNoOfOpenDocs As Long
Dim docAdded As Boolean
Dim docClosed As Boolean

newNoOfOpenDocs = Application.Documents.Count

If newNoOfOpenDocs > oldNoOfOpenDocs Then
docAdded = True

If ActiveDocument.Name = "Document1" And FirstNewDoc Then
FirstNewDoc = True
Else
FirstNewDoc = False
End If
oldNoOfOpenDocs = oldNoOfOpenDocs + 1

ElseIf oldNoOfOpenDocs > newNoOfOpenDocs Then
docClosed = True
FirstNewDoc = False
oldNoOfOpenDocs = oldNoOfOpenDocs - 1
End If

If docAdded Then
If Len(ActiveDocument.Path) = 0 Then
Call PsuedoAutoNew
Else
Call PsuedoAutoOpen
End If

ElseIf docClosed Then
Call PsuedoAutoClose

ElseIf FirstNewDoc Then
If Len(ActiveDocument.Path) = 0 Then
Call PsuedoAutoNew
Else
Call PsuedoAutoOpen
End If

Else
Call DocChangedFocus
End If

Exit Sub

ExitCode:

End Sub


--------------------------------------------------------------------------------

Private Sub PsuedoAutoNew()
'Your code here
End Sub


--------------------------------------------------------------------------------

Private Sub PsuedoAutoOpen()
'Your code here
End Sub


--------------------------------------------------------------------------------

Private Sub PsuedoAutoClose()
'Your code here
End Sub


--------------------------------------------------------------------------------

Private Sub DocChangedFocus()
'Your code here
End Sub

Notes:

1.
Unfortunately, unlike a true AutoClose macro or DocumentBeforeClose event, the PsuedoAutoClose fires after the document has closed rather than before; so in this respect, this workaround is no substitute for the real thing; but maybe it's better than no workaround at all. (This is logical, if unfortunate, because the document focus hasn't changed until the document being closed has completely closed.).

Also, (and unfortunately), if you quit Word with only one document open, the PsuedoAutoClose macro doesn't fire at all.

And if you quit Word using File + Exit, even the (Word 2000-specific) DocumentBeforeClose event, which is covered below, fires after the message asking whether you want to save changes has appeared!! This bug is unfortunate to say the least, as it means that if you want to perform some validation before that dialog appears, you have to use an AutoClose macro (see the article: A pseudo DocumentBeforeClose Event).

2.
In one important respect, the PsuedoAutoNew and PsuedoAutoOpen macros work better than the Word 2000-specific NewDocument and DocumentOpen events.

When you start Word, a new blank document is created; the PsuedoAutoNew macro runs, but neither AutoNew nor the (Word 2000-specific) NewDocument events are triggered.

Similarly, if you start Word with an AutoExec macro that opens a document, or that allows the user to open a document, for example:

Public Sub AutoExec()

Dim myFile As String
myFile = Application.RecentFiles(1).Path & "\" _
& Application.RecentFiles(1).Name
Documents.Open myFile

End Sub

... AutoOpen and the PsuedoAutoOpen macro both fire, but the (Word 2000-specific) DocumentOpen event is not triggered.

So even Word 2000 developers who don't have to cater for Word 97 users may still prefer to use the use the DocumentChange event to simulate AutoOpen and AutoNew, as it's more reliable.


2. Using Word 2000 and later versions
In Word 2000 and above, you can store NewDocument, DocumentOpen and DocumentBeforeClose event procedures in an Addin, and they will be global. They would look like this:

Private Sub oApp_NewDocument(ByVal Doc As Document)
'Your code here
End Sub

Private Sub oApp_DocumentOpen(ByVal Doc As Document)
'Your code here
End Sub

Private Sub oApp_DocumentBeforeClose(ByVal Doc As Document, _
Cancel As Boolean)
'Your code here
End Sub

In the case of the DocumentBeforeClose event, the “Cancel” parameter allows you to prevent the document closing if required, e.g.:

Private Sub oApp_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)

If ActiveDocument.Paragraphs.Count > 3 Then
Cancel = True
MsgBox "You are not allowed more than 3 paragraphs."
End If

End Sub

But see the Notes at the end of the “Word 97” section for a discussion of the numerous “gotchas” associated with this – and with all three of these events – and for some workarounds.
 
Hi VBAWilllow,

That looks fairly comprehensive [smile]

Glad you got it working.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top