I am not a programmer. With this said, our database was recently upgraded to 2016. I am getting errors on one of our forms which uses a query to merge to one of any 10 word merge documents. The two errors I am receiving are error 91 on the With oWdoc.MailMergee line and the "expect a name error on the mailmerge section. Can anyone help me. I have spent three days researching this and found a thread on this forum similar, but do not know how to apply it to my specific form. I am thinking I am missing a line or similar. I have included all code associated with this form in this post. The form is in Access 2016. I am disheartened by this not being able to get the merge to work. Within my database, I am using an Access Query which is used to fill out several word merge documents. The query is called "mqpinsp". Have I done something as simple as left out a bracket or quotations?
The errors occur once I call on the mail merge. An error 91 occurs and an error indicating a name is expected occurs. Both errors are in the following section of the code,
Code:
With oWdoc.MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource _
Name:=CurrentProject.FullName, _
ReadOnly:=True, _
AddToRecentFiles:=False, _
LinkToSource:=True, _
Connection:="QUERY mqpinsp",
SQLStatement:="SELECT * FROM [mqpinsp] ' Change the table name or your query"
.Destination = wdSendToNewDocument
.Execute Pause:=False
End With
The only code on this one form is this:
Code:
Option Compare DatabaseOption Explicit
Private Sub cmdQUIT_Click()
DoCmd.Close acForm, "fLettersPInsp"
End Sub
Private Sub lstLetters_Click()
Dim strWordDoc As String
'Path to the word document of the Mail Merge
'###-1 CHANGE THE FOLLOWING LINE TO POINT TO YOUR DOCUMENT!!
strWordDoc = "H:\USERS\Templates" & "\" & "lstLetters.Column(0)"
Dim MergeQuery As String
MergeQuery = lstLetters.Column(1)
' Call the code to merge the latest info
startMerge strWordDoc
End Sub
'----------------------------------------------------
' Auto Mail Merge With VBA and Access (Early Binding)
'----------------------------------------------------
' NOTE: To use this code, you must reference
' The Microsoft Word 14.0 (or current version)
' Object Library by clicking menu Tools > References
' Check the box for:
' Microsoft Word 14.0 Object Library in Word 2010
' Microsoft Word 15.0 Object Library in Word 2013
' Click OK
'----------------------------------------------------
Function startMerge(strDocPath As String)
Dim oWord As Word.Application
Dim oWdoc As Word.Document
Dim wdInputName As String
Dim wdOutputName As String
Dim outFileName As String
' Set Template Path
wdInputName = strDocPath ' was CurrentProject.Path & "\mail_merge.docx"
' Create unique save filename with minutes and seconds to prevent overwrite
outFileName = "MailMergeFile_" & Format(Now(), "yyyymmddmms")
' Output File Path w/outFileName
wdOutputName = CurrentProject.Path & "\" & outFileName
Set oWord = New Word.Application
Set oWdoc = oWord.Documents.Open(wdInputName)
' Start mail merge
With oWdoc.MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource _
Name:=CurrentProject.FullName, _
ReadOnly:=True, _
AddToRecentFiles:=False, _
LinkToSource:=True, _
Connection:="QUERY mqpinsp",
SQLStatement:="SELECT * FROM [mqpinsp] ' Change the table name or your query"
.Destination = wdSendToNewDocument
.Execute Pause:=False
End With
' Hide Word During Merge
oWord.Visible = False
' Save file as PDF
' Uncomment the line below and comment out
' the line below "Save file as Word Document"
'------------------------------------------------
'oWord.ActiveDocument.SaveAs2 wdOutputName & ".pdf", 17
' Save file as Word Document
' ###-3 IF YOU DON'T WANT TO SAVE AS A NEW NAME, COMMENT OUT NEXT LINE
oWord.ActiveDocument.SaveAs2 wdOutputName & ".docx", 16
' SHOW THE DOCUMENT
oWord.Visible = True
' Close the template file
If oWord.Documents(1).FullName = strDocPath Then
oWord.Documents(1).Close savechanges:=False
ElseIf oWord.Documents(2).FullName = strDocPath Then
oWord.Documents(2).Close savechanges:=False
Else
MsgBox "Well, this should never happen! Only expected two documents to be open"
End If
' Quit Word to Save Memory
'oWord.Quit savechanges:=False
End Function