Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem using mail merge

Status
Not open for further replies.

daz321

Programmer
May 31, 2002
17
GB
I have created a project that combines with MS Access to create letters, using data from access and letter templates in word. I have different letter type depending on the data involved. I can run one mail merge but when i finish that, close word, and try to run a second mail merge i get an "Invalid merge field" error, that claims that all my merge fields in the document don't exist in my data source (i even does this when repeating the first merge). My code is:


Private Sub cmdFullRun_Click()
Dim msg As String
Dim Letter As String
Dim Data As String
Dim LetterType As String
Dim AccessApp As Access.Application
Dim DBPath As String
Dim DropMacro As String

LetterType = Left(lblLetterType.Caption, 3)

DropMacro = Left(LetterType, 3)

DBPath = "C:\My Documents\darryl.mdb"
Set AccessApp = New Access.Application
With AccessApp
.OpenCurrentDatabase DBPath
.DoCmd.RunMacro ("export " & DropMacro) ' exports the data to darryl.txt
End With

Letter = "c:\darryl\uni work\project\" & LetterType & "Letter.doc"

Data = "C:\my documents\darryl.txt"

msg = FireUpWord(Letter, _
Data)
If msg <> &quot;&quot; Then MsgBox msg
End Sub

Function FireUpWord(DocName As String, DSName As String) _
As String
Dim WordApp As Object

Err.Clear
Set WordApp = CreateObject(&quot;Word.Application&quot;)
WordApp.Visible = True
Set Merge = WordApp.Documents.Open(DocName).MailMerge
If (Err.Number <> 0) Then
FireUpWord = &quot;Couldn't open document '&quot; & DocName _
& &quot;'&quot; & Chr(10) & &quot;Errno= &quot; & Err.Number
Else
Merge.OpenDataSource (DSName)
WordApp.ActiveDocument.MailMerge.Execute Pause:=True
FireUpWord = &quot;&quot;
End If
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top