Mail Merge Run Time Error (1 Viewer)

abbaddon223

Registered User.
Local time
Today, 04:45
Joined
Mar 13, 2010
Messages
162
Hi,

I've put together the below code for merging text into a dotx document (it has the bookmarks in already).

Dim AddyLineVar As String, SalutationVar As String
If IsNull([ContactSurname]) Then
AddLineVar = [Company_SchoolName]
SalutationVar = "Sir Or Madam"
Else
AddyLineVar = [ContactForename] & " " & [ContactSurname]
If Not IsNull([Company_SchoolName]) Then
AddLineVar = AddyLineVar & vbCrLf & [Company_SchoolName]
End If
SaluationVar = [ContactForename]
End If
AddyLineVar = AddyLineVar & vbCrLf & [AddressLine1]
If Not IsNull([AddressLine2]) Then
AddyLineVar = AddyLineVar & vbCrLf & [AddressLine2]
AddyLineVar = AddyLineVar & vbCrLf & [Town]
AddyLineVar = AddyLineVar & vbCrLf & [County]
AddyLineVar = AddyLineVar & vbCrLf & [Postcode]
End If
Dim Wrd As New Word.Application
Set Wrd = CreateObject("Word.Application")
Dim MergeDoc As String
MergeDoc = Application.CurrentProject.Path
MergeDoc = MergeDoc & "\C:\Users\PLittle\Desktop\SQL\Databases\Functioning\CAHMS Case Management\Live\Merge Template\MYALetter2"
Wrd.Documents.Add MergeDoc
Wrd.Visible = True
With Wrd.ActiveDocument.Bookmarks
.Item("LetterDate").Range.Text = Date
.Item("AddresseeName").Range.Text = SalutationVar
.Item("AddressLines").Range.Text = AddyLineVar
End With
End Sub

When I run the proceedures, I get an error saying that file is corrupt at the wrd.documents.add Mergedoc line. Can anyone help please?

Thanks for the assistance in advance!!
 

boblarson

Smeghead
Local time
Today, 04:45
Joined
Jan 12, 2001
Messages
32,059
This:

MergeDoc = Application.CurrentProject.Path
MergeDoc = MergeDoc & "\C:\Users\PLittle\Desktop\SQL\Databases\Functioni ng\CAHMS Case Management\Live\Merge

is not giving you a valid path.

Add a Debug.Print MergeDoc just after the second one and you will see your problem.
 

abbaddon223

Registered User.
Local time
Today, 04:45
Joined
Mar 13, 2010
Messages
162
Brilliant - now working - that's had me stumped for a while.

Thanks a lot!!
 

Users who are viewing this thread

Top Bottom