Hi
I have spent the last 2 weeks trying different options and can't get my version of code working, so help is greatly needed!
I have a form with a command button on it that sends a template name to a module which "should" merge that one record with a document based on the template in Word. Should be easy but I can't get the module to work! When I click the command button I get Error 91: Object variable or With Block variable not set. Any help gratefully received.
Here's my module code:
Public Sub MergeDocument(DocumentName) ' this is the template name passed from the command button
On Error GoTo ErrorHandler
' function to produce selected inlays based on the qLabels query
Dim NewDocPath As String
Dim objWord As Word.Application
Dim DocName As String
' save the record first
DoCmd.RunCommand acCmdSaveRecord
' set the path to the templates folder
NewDocPath = "S:\LabelsDB\LabelTemplates\" & DocumentName
objWord.Documents.Add (NewDocPath)
DocName = objWord.ActiveDocument.Name
With objWord.ActiveDocument.MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource _
Name:="S:\LabelsDB\LabelsXP.mdb", _
LinkToSource:=True, AddToRecentFiles:=False, _
Connection:="QUERY qLabels"
.Destination = wdSendToNewDocument
.Execute
End With
objWord.Documents(DocName).Close False
DoCmd.Hourglass False
objWord.Visible = True
objWord.Activate
Set objWord = Nothing
Exit Sub
ErrorHandler:
DoCmd.Hourglass False
MsgBox "It looks like we have a problem. Please write down the error number and what you were doing then call technical support." _
& vbCrLf & vbCrLf & "Error number: " & Err.Number & " and Description: " & Error, vbExclamation, "This could be serious"
End If
objWord.Visible = True
Set objWord = Nothing
End Sub
I have spent the last 2 weeks trying different options and can't get my version of code working, so help is greatly needed!
I have a form with a command button on it that sends a template name to a module which "should" merge that one record with a document based on the template in Word. Should be easy but I can't get the module to work! When I click the command button I get Error 91: Object variable or With Block variable not set. Any help gratefully received.
Here's my module code:
Public Sub MergeDocument(DocumentName) ' this is the template name passed from the command button
On Error GoTo ErrorHandler
' function to produce selected inlays based on the qLabels query
Dim NewDocPath As String
Dim objWord As Word.Application
Dim DocName As String
' save the record first
DoCmd.RunCommand acCmdSaveRecord
' set the path to the templates folder
NewDocPath = "S:\LabelsDB\LabelTemplates\" & DocumentName
objWord.Documents.Add (NewDocPath)
DocName = objWord.ActiveDocument.Name
With objWord.ActiveDocument.MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource _
Name:="S:\LabelsDB\LabelsXP.mdb", _
LinkToSource:=True, AddToRecentFiles:=False, _
Connection:="QUERY qLabels"
.Destination = wdSendToNewDocument
.Execute
End With
objWord.Documents(DocName).Close False
DoCmd.Hourglass False
objWord.Visible = True
objWord.Activate
Set objWord = Nothing
Exit Sub
ErrorHandler:
DoCmd.Hourglass False
MsgBox "It looks like we have a problem. Please write down the error number and what you were doing then call technical support." _
& vbCrLf & vbCrLf & "Error number: " & Err.Number & " and Description: " & Error, vbExclamation, "This could be serious"
End If
objWord.Visible = True
Set objWord = Nothing
End Sub