Controlling Word MailMerge from within Access

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
 
B

BAC

Is there some reason you did not use the With docWord structure here but you
did later on..

Word may be visible, but you are still running in Access..

Try a With...End With across the entire Mail Merge section of the code

BAC
 
J

Jonathan Lax

I figured it out, had to declare range (you were right about the With clause
too)...here's what I put:

'Build Word Document
Set appWord = New Word.Application
appWord.Visible = True
Set docWord = appWord.Documents.Open(documentPath, , True)
Dim range As range
Set range = docWord.range(0, 0)

With docWord.MailMerge
.Fields.AddSkipIf _
range:=range, _
MergeField:="ID", _
Comparison:=wdMergeIfNotEqual, _
CompareTo:=ID
.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

BAC said:
Is there some reason you did not use the With docWord structure here but you
did later on..

Word may be visible, but you are still running in Access..

Try a With...End With across the entire Mail Merge section of the code

BAC


Jonathan Lax said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top