It may well be that not destroying object variables that are used repeatedly
in quick succession is causing the problem. I'm afraid this is a bit of a
mystery. I suspect it is an extremely complex topic/problem. The general
advice is that you should always destroy object variables when you're
finished with them, even though VBA should destroy them automatically when
they go out of scope.
In the light of you last post, I would offer one more suggestion. Everything
you can do with a macro, you can do with VBA and it's usually better to do
it with VBA. So, to run the letters independently or together, you could
simply set up three different "master" subroutines, MergeRegular,
MergeTribute, or MergeBothRegularAndTribute. This would overcome the
problem of only setting up and destroying object variables once.
Furthermore, to check if there are any records before merging, you could
examine the RecordCount property of the DataSource object in the MyMailmerge
subprocedure:
With .MailMerge
' See if there are records to be merged:
If .DataSource.RecordCount = 0 Then
GoTo Exit_NoRecordsMessage
End If
Here's the new code:
' Declare variables with module-wide scope
' (at the top of a module):
Private mobjWord As Word.Application
Private mobjMergeDoc As Word.Document
Public Sub MergeRegular()
On Error GoTo ErrHandler
Call StartWord
Call MyMailmerge("H:\Access\Testing\ack regular.dot")
mobjWord.Activate
Bye:
' Destroy object variables:
Set mobjMergeDoc = Nothing
Set mobjWord = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Number & ": " & Err.Description, vbOKOnly, "Error"
Resume Bye
End Sub
Public Sub MergeTribute()
On Error GoTo ErrHandler
Call StartWord
Call MyMailmerge("H:\Access\Testing\ack tribute.dot")
mobjWord.Activate
Bye:
' Destroy object variables:
Set mobjMergeDoc = Nothing
Set mobjWord = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Number & ": " & Err.Description, vbOKOnly, "Error"
Resume Bye
End Sub
Public Sub MergeBothRegularAndTribute()
On Error GoTo ErrHandler
Call StartWord
Call MyMailmerge("H:\Access\Testing\ack regular.dot")
Call MyMailmerge("H:\Access\Testing\ack tribute.dot")
mobjWord.Activate
Bye:
' Destroy object variables:
Set mobjMergeDoc = Nothing
Set mobjWord = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Number & ": " & Err.Description, vbOKOnly, "Error"
Resume Bye
End Sub
Private Sub StartWord()
On Error Resume Next
Set mobjWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set mobjWord = CreateObject("Word.Application")
End If
End Sub
Private Sub MyMailmerge(strMainPathName As String)
Set mobjMergeDoc = mobjWord.Documents.Add(strMainPathName)
' Do stuff with mailmerge object:
With mobjMergeDoc
With .MailMerge
' See if there are records to be merged:
If .DataSource.RecordCount = 0 Then
GoTo Exit_NoRecordsMessage
End If
' Always check the State property before
' executing the merge as you must have
' a main document attached to a datasource
' before executing:
If .State = wdMainAndDataSource Then
.Execute
End If
End With
' Close the main document:
.Close wdDoNotSaveChanges
End With
Bye:
Exit Sub
Exit_NoRecordsMessage:
' Insert a message to user here if you want,
' using the MsgBox function.
GoTo Bye
End Sub
Geoff