-
VBA Access running mail merge
Currently I have a sub that runs a mail merge which works fine.
I have now made a second sub that once the mail merge is open should "Merge to new document" and then save.
On the initial run it works perfectly but any further attempt fails with "Run-time error '462': The remote server machine does not exist or is unavailable"
This error is explained at support dot microsoft dot com /kb/189618
After reading this I believe it to be that I havent put objWord infront of each of the active documents?!?
When I do try to correct this it just starts messing up and I get stuck? Any help would be greatly appreciated, Thanks
[vba] Public Sub MergeItMissingInfo()
Dim docWord As Word.Document
Set docWord = GetObject("C:\Documents and Settings\Username\Desktop\mailmerge.doc", "Word.Document")
' Make Word visible.
docWord.Application.Visible = True
' Set the mail merge data source
docWord.MailMerge.OpenDataSource _
Name:="C:\Documents and Settings\Username\Desktop\Database.mdb", _
LinkToSource:=True, _
Connection:="VIEWS qry_LETTER_missing_info", _
SQLStatement:="SELECT * FROM [qry_LETTER_missing_info]"
Call SaveAsNewDoc[/vba]
[vba] Public Sub SaveAsNewDoc()
Dim objWord As Word.Application
'Merges into a new document
'Set objWord = New Word.Application
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With ActiveDocument.MailMerge.DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
'Saves the new document
With ActiveDocument
.SaveAs ("C:\Documents and Settings\Username\Desktop\Mail Merge Letters\Cheese.doc")
'.Close
End With
'objWord.Quit
'Set objWord = Nothing
' Set docWord = Nothing
End Sub[/vba]
-
To answer my own problem
It just needed to include a .execute and the release code needed to be in the same sub as the set object.
[VBA]Public Sub MergeItMissingInfo()
Public Sub MergeItMissingInfo()
Dim docWord As Word.Document
Set docWord = GetObject("C:\Documents and Settings\Username\Desktop\mailmerge.doc", "Word.Document")
' Make Word visible.
docWord.Application.Visible = True
' Set the mail merge data source
docWord.MailMerge.OpenDataSource _
Name:="C:\Documents and Settings\Username\Desktop\Database.mdb", _
LinkToSource:=True, _
Connection:="VIEWS qry_LETTER_missing_info", _
SQLStatement:="SELECT * FROM [qry_LETTER_missing_info]"
.Destination = wdSendToNewDocument
With docWord.MailMerge.DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute
End With
objWord.Quit
Set docWord = Nothing
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
-
Forum Rules