Often we have clients who want Word to open without an initial document being displayed. This is usually because they don’t want the initial document based on the normal template. Doing a winword /a is no good, as it kills off all the other add-ins – so your DMS is usually killed off in the process, let alone all your other add-ins.
Life was simple in the days of Word 2003 – to get rid of the initial you could just do an ActiveDocument.Close in an AutoExec sub and you’re away. But that doesn’t work in 2010 as there no active document yet during the AutoExec.
So how did we get around it? Well it turns out we had to use the Document_Change event instead.
Within one of your startup templates, add a new Class object, call it AppEvents and insert the following code:
Option Explicit
Private WithEvents objApplication As Word.Application
Private Sub Class_Initialize()
objApplication = Word.Application
End Sub
Private Sub objApplication_DocumentChange()
On Error Resume Next
If StartingUp Then
If Documents.Count > 0 Then
‘ Close any new document created using normal.dot/normal.dotm
If ActiveDocument.Name = ActiveDocument.Fullname And _
InStr(1, ActiveDocument.AttachedTemplate, “Normal.dot”, vbTextCompare) Then
ActiveDocument.Close(wdDoNotSaveChanges)
StartingUp = False
Exit Sub
End If
End If
End If
End Sub
Now create a new module, call it AutoMacros (or anything else you like) and add the following code:
Option Explicit
Dim oAppEvents As AppEvents
Dim StartingUp As Boolean
Sub AutoExec()
StartingUp = True
oAppEvents = New AppEvents
End Sub
So why add the StartingUp variable? Well if we don’t have it, then any new document you try to create based on Normal will automatically close. Whilst that doesn’t sound too bad in a lot of cases, it has one bad side effect – you can’t do a compare into a new document, as the new document that gets created is always based on Normal. There are probably a few other possible bad side effects too – but we didn’t bother looking any further after that one!