shieldsco
asked on
Print Word Mail Merge using VBA from Access
I'm using the below code on an access 2007 form to perform a word 2007 mail merge. The merge works find however I'm need code to print all documents and close documents and quit the application
Option Compare Database
Dim WithEvents oApp As Word.Application
Private Sub Command0_Click()
DoCmd.SetWarnings False
DoCmd.RunCommand acCmdRecordsGoToNew
DoCmd.OpenQuery "qryMail1"
DoCmd.OpenQuery "qryUpdMailSent"
DoCmd.SetWarnings True
Set oApp = CreateObject("Word.Applica tion")
Dim oMainDoc As Word.Document
Dim oSel As Word.Selection
Dim sDBPath As String
Set oMainDoc = oApp.Documents.Open("\\v07 .med.va.go v\atg\Psyc h\Homeless Program\Interest List Database\InterestLetter2.d oc")
oApp.Visible = True
With oMainDoc.MailMerge
.MainDocumentType = wdFormLetters
sDBPath = "\\v07.med.va.gov\atg\Psyc h\Homeless Program\Interest List Database\Interest List V2.accdb"
.OpenDataSource Name:=sDBPath, _
SQLStatement:="SELECT * FROM [tblMail1]"
End With
With oMainDoc
.MailMerge.Destination = wdSendToNewDocument
.MailMerge.Execute
End With
oApp.Activate
oApp.Documents.Parent.Visi ble = True
oApp.Application.WindowSta te = 1
oApp.ActiveWindow.WindowSt ate = 1
Set oApp = Nothing
End Sub
Option Compare Database
Dim WithEvents oApp As Word.Application
Private Sub Command0_Click()
DoCmd.SetWarnings False
DoCmd.RunCommand acCmdRecordsGoToNew
DoCmd.OpenQuery "qryMail1"
DoCmd.OpenQuery "qryUpdMailSent"
DoCmd.SetWarnings True
Set oApp = CreateObject("Word.Applica
Dim oMainDoc As Word.Document
Dim oSel As Word.Selection
Dim sDBPath As String
Set oMainDoc = oApp.Documents.Open("\\v07
oApp.Visible = True
With oMainDoc.MailMerge
.MainDocumentType = wdFormLetters
sDBPath = "\\v07.med.va.gov\atg\Psyc
.OpenDataSource Name:=sDBPath, _
SQLStatement:="SELECT * FROM [tblMail1]"
End With
With oMainDoc
.MailMerge.Destination = wdSendToNewDocument
.MailMerge.Execute
End With
oApp.Activate
oApp.Documents.Parent.Visi
oApp.Application.WindowSta
oApp.ActiveWindow.WindowSt
Set oApp = Nothing
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
There are two problems - after printing the user is prompted : Do you want to save changes?
The are two letters that open - the template and the populated document. The template closes ok however the populated document prompts the user.
The second problem is when I run the merge again I get a run-time error 462 remote server does not exist or is unavailable. It appears that word is not being released. I can close and reopen the database and then the merge works again
The are two letters that open - the template and the populated document. The template closes ok however the populated document prompts the user.
The second problem is when I run the merge again I get a run-time error 462 remote server does not exist or is unavailable. It appears that word is not being released. I can close and reopen the database and then the merge works again
ASKER
I moved the quit statement and it works find with the exceptio of the user being promted to save.
ASKER
made a few adjustments and it works find - I just need to put some error checking in.
Thanks
Private Sub Command0_Click()
DoCmd.SetWarnings False
DoCmd.RunCommand acCmdRecordsGoToNew
DoCmd.OpenQuery "qryMail1"
DoCmd.OpenQuery "qryUpdMailSent"
DoCmd.SetWarnings True
Set oApp = CreateObject("Word.Applica tion")
Dim oMainDoc As Word.Document
Dim oSel As Word.Selection
Dim sDBPath As String
Set oMainDoc = oApp.Documents.Open("\\v07 .med.va.go v\atg\Psyc h\Homeless Program\Interest List Database\InterestLetter2.d oc")
oApp.Visible = True
With oMainDoc.MailMerge
.MainDocumentType = wdFormLetters
sDBPath = "\\v07.med.va.gov\atg\Psyc h\Homeless Program\Interest List Database\Interest List V2.accdb"
.OpenDataSource Name:=sDBPath, _
SQLStatement:="SELECT * FROM [tblMail1]"
End With
With oMainDoc
.MailMerge.Destination = wdSendToNewDocument
.MailMerge.Execute
End With
With oMainDoc
.MailMerge.Destination = wdSendToPrinter
.MailMerge.Execute
End With
oMainDoc.Close wdDoNotSaveDocument
oApp.ActiveDocument.Close wdDoNotSaveDocument
oApp.Activate
oApp.Documents.Parent.Visi ble = True
oApp.Application.WindowSta te = 1
' oApp.ActiveWindow.WindowSt ate = 1
oApp.Quit
Set oApp = Nothing
End Sub
Thanks
Private Sub Command0_Click()
DoCmd.SetWarnings False
DoCmd.RunCommand acCmdRecordsGoToNew
DoCmd.OpenQuery "qryMail1"
DoCmd.OpenQuery "qryUpdMailSent"
DoCmd.SetWarnings True
Set oApp = CreateObject("Word.Applica
Dim oMainDoc As Word.Document
Dim oSel As Word.Selection
Dim sDBPath As String
Set oMainDoc = oApp.Documents.Open("\\v07
oApp.Visible = True
With oMainDoc.MailMerge
.MainDocumentType = wdFormLetters
sDBPath = "\\v07.med.va.gov\atg\Psyc
.OpenDataSource Name:=sDBPath, _
SQLStatement:="SELECT * FROM [tblMail1]"
End With
With oMainDoc
.MailMerge.Destination = wdSendToNewDocument
.MailMerge.Execute
End With
With oMainDoc
.MailMerge.Destination = wdSendToPrinter
.MailMerge.Execute
End With
oMainDoc.Close wdDoNotSaveDocument
oApp.ActiveDocument.Close wdDoNotSaveDocument
oApp.Activate
oApp.Documents.Parent.Visi
oApp.Application.WindowSta
' oApp.ActiveWindow.WindowSt
oApp.Quit
Set oApp = Nothing
End Sub
ASKER
Thanks very much
Sorry, I had to go out.
I glad that you have been able to sort it out.
I glad that you have been able to sort it out.
ASKER