J
Jonathan Lax
I am trying to write some VBA code that is contained in Access 2003 on a
button event. When the event is launched I am trying to insert a skipif
field at the beginning of the document and then merge the document with my
database. I continue to get the following error: "Run-time error '91':
Object variable or With block variable not set". I get this error for the
block of code "docWord.MailMerge.Fields...CompareTo:=ID". I am also aware
that I am likely not refernecing the database properly, but if I can only
make it through this problem and insert the mergefield I can then tackle the
next problem. This code was originally a recorded macro in Word which I have
cut up and editted into my Access. I hope what I am trying to do is clear
and that someone could provide me with some help. I would greatly appreciate
it. My code follows:
Public Sub CreateDoc_Click()
'Declarations
Dim appWord As Word.Application
Dim docWord As Word.document
Dim documentName As String
Dim documentPath As String
Dim Carrier As String
Dim ID As String
'Set ID
ID = Me![ID]
' Determine Document Name
If Me![Frame75] = 1 Then
documentName = "report"
ElseIf Me![Frame75] = 2 Then
documentName = "let"
ElseIf Me![Frame75] = 3 Then
documentName = "inv"
ElseIf Me![Frame75] = 4 Then
documentName = "not"
ElseIf Me![Frame75] = 5 Then
documentName = "Fax"
ElseIf Me![Frame75] = 6 Then
documentName = "FaxBS"
End If
' Determine Status of Carrier
If Me![Carrier] = "No Carrier" Then
Carrier = ""
Else
Carrier = "C"
End If
'Create Document Path
documentPath = "C:\Reportsv2.0\Templates\" & documentName & Carrier & ".doc"
'Build Word Document
Set appWord = New Word.Application
appWord.Visible = True
Set docWord = appWord.Documents.Open(documentPath, , True)
docWord.MailMerge.Fields.AddSkipIf _
Range:=Selection.Range, _
MergeField:="ID", _
Comparison:=wdMergeIfNotEqual, _
CompareTo:=ID
With docWord.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
docWord.Close (No)
Set docWord = Nothing
appWord.NormalTemplate.Saved = True
Set appWord = Nothing
End Sub
button event. When the event is launched I am trying to insert a skipif
field at the beginning of the document and then merge the document with my
database. I continue to get the following error: "Run-time error '91':
Object variable or With block variable not set". I get this error for the
block of code "docWord.MailMerge.Fields...CompareTo:=ID". I am also aware
that I am likely not refernecing the database properly, but if I can only
make it through this problem and insert the mergefield I can then tackle the
next problem. This code was originally a recorded macro in Word which I have
cut up and editted into my Access. I hope what I am trying to do is clear
and that someone could provide me with some help. I would greatly appreciate
it. My code follows:
Public Sub CreateDoc_Click()
'Declarations
Dim appWord As Word.Application
Dim docWord As Word.document
Dim documentName As String
Dim documentPath As String
Dim Carrier As String
Dim ID As String
'Set ID
ID = Me![ID]
' Determine Document Name
If Me![Frame75] = 1 Then
documentName = "report"
ElseIf Me![Frame75] = 2 Then
documentName = "let"
ElseIf Me![Frame75] = 3 Then
documentName = "inv"
ElseIf Me![Frame75] = 4 Then
documentName = "not"
ElseIf Me![Frame75] = 5 Then
documentName = "Fax"
ElseIf Me![Frame75] = 6 Then
documentName = "FaxBS"
End If
' Determine Status of Carrier
If Me![Carrier] = "No Carrier" Then
Carrier = ""
Else
Carrier = "C"
End If
'Create Document Path
documentPath = "C:\Reportsv2.0\Templates\" & documentName & Carrier & ".doc"
'Build Word Document
Set appWord = New Word.Application
appWord.Visible = True
Set docWord = appWord.Documents.Open(documentPath, , True)
docWord.MailMerge.Fields.AddSkipIf _
Range:=Selection.Range, _
MergeField:="ID", _
Comparison:=wdMergeIfNotEqual, _
CompareTo:=ID
With docWord.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
docWord.Close (No)
Set docWord = Nothing
appWord.NormalTemplate.Saved = True
Set appWord = Nothing
End Sub