Hey everybody I am currently trying to merger data from a form in MS access but it keeps giving me the error 91: Object variable or with block variable not set. could somebody please look at the code below and please guide me to a solution. Thank you!
[code=vb]'************** *************** *************** *************** *****
'MailMerge method opens specified Word document and merges fields
'specified within the document with the Access fields located in
'the specified query FRM00034
'************** *************** *************** *************** *****
Public Sub MailMerge034()
Dim strPath As String
Dim strDataSource As String
Dim doc As Word.Document
Dim wrdApp As Word.Applicatio n
On Error GoTo HandleErrors
' can't use CurrentProject. Path in Access 97
strPath = GetCurrentDBPat h
' Delete the rtf file, if it already exists.
strDataSource = strPath & "MAILMERGEQUERI ES\" & conQuery034 & rel_Base_Mask & ".doc"
Kill strDataSource
' Export the data to rtf format
DoCmd.OutputTo acOutputQuery, conQuery034, _
acFormatRTF, strDataSource, False
' Start Word using mailmerge template
Set wrdApp = New Word.Applicatio n
Set doc = wrdApp.Document s.Add(strPath & conTemplate034)
' Do the mail merge to a new document.
With doc.MailMerge
.OpenDataSource Name:=strDataSo urce
.Destination = wdSendToNewDocu ment
.SuppressBlankL ines = True
With .DataSource
.FirstRecord = wdDefaultFirstR ecord
.LastRecord = wdDefaultLastRe cord
End With
If .State = wdMainAndDataSo urce Then .Execute
End With
' Display the mail merge document
wrdApp.Visible = True
ExitHere:
Set doc = Nothing
Set wrdApp = Nothing
Exit Sub
HandleErrors:
Select Case Err.Number
Case 5922
MsgBox "The Data Requested Does Not Exist in Database"
Resume ExitHere
Case 2501
Resume ExitHere
Case 70
MsgBox "Close Previously Generated Documents"
Resume ExitHere
Case 53 ' File not found
Resume Next
Case Else
MsgBox Err.Number & ": " & Err.Description
Resume ExitHere
End Select
Resume
End Sub[/code]
[code=vb]'************** *************** *************** *************** *****
'MailMerge method opens specified Word document and merges fields
'specified within the document with the Access fields located in
'the specified query FRM00034
'************** *************** *************** *************** *****
Public Sub MailMerge034()
Dim strPath As String
Dim strDataSource As String
Dim doc As Word.Document
Dim wrdApp As Word.Applicatio n
On Error GoTo HandleErrors
' can't use CurrentProject. Path in Access 97
strPath = GetCurrentDBPat h
' Delete the rtf file, if it already exists.
strDataSource = strPath & "MAILMERGEQUERI ES\" & conQuery034 & rel_Base_Mask & ".doc"
Kill strDataSource
' Export the data to rtf format
DoCmd.OutputTo acOutputQuery, conQuery034, _
acFormatRTF, strDataSource, False
' Start Word using mailmerge template
Set wrdApp = New Word.Applicatio n
Set doc = wrdApp.Document s.Add(strPath & conTemplate034)
' Do the mail merge to a new document.
With doc.MailMerge
.OpenDataSource Name:=strDataSo urce
.Destination = wdSendToNewDocu ment
.SuppressBlankL ines = True
With .DataSource
.FirstRecord = wdDefaultFirstR ecord
.LastRecord = wdDefaultLastRe cord
End With
If .State = wdMainAndDataSo urce Then .Execute
End With
' Display the mail merge document
wrdApp.Visible = True
ExitHere:
Set doc = Nothing
Set wrdApp = Nothing
Exit Sub
HandleErrors:
Select Case Err.Number
Case 5922
MsgBox "The Data Requested Does Not Exist in Database"
Resume ExitHere
Case 2501
Resume ExitHere
Case 70
MsgBox "Close Previously Generated Documents"
Resume ExitHere
Case 53 ' File not found
Resume Next
Case Else
MsgBox Err.Number & ": " & Err.Description
Resume ExitHere
End Select
Resume
End Sub[/code]
Comment