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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Mail Merge from VB6

Status
Not open for further replies.

SteveHall

Programmer
Jan 4, 2001
6
GB
I have an application running at a client who has every flavour of Word from Word 6.0 to Word 2000 running in various offices. I do a mail merge as follows:-

Set objWord = CreateObject("Word.Basic")

objWord.FileOpen Name:=strNetworkDrive & "\newnead\data\lettera.rtf"

objWord.AppShow

objWord.MailMergeOpenDataSource Name:="c:\neadwork\merge.txt", LinkToSource:=False

objWord.MailMerge CheckErrors:=1, Destination:=0, MailMerge:=1

objWord.Activate "lettera.rtf"

objWord.FileClose 2

On most of the pc's running Word 97 I get a message "Word cannot open the merge data file" even though these pc's can run the mail merge from within Word. Word 6.0 and Word 2000 work ok.

Any thoughts?

Steve Hall
 
change you code :

Dim oApp As New Word.Application
Dim oMainDoc As Word.Document
Dim oSel As Word.Selection

'Start a new main document for the mail merge
Set oMainDoc = oApp.Documents.Add

With oMainDoc.MailMerge
.MainDocumentType = wdFormLetters

'Set up the mail merge data source to the DSN "Northwind"
.OpenDataSource Name:="", Connection:="DSN=Northwind", _
SQLStatement:="SELECT CompanyName, Address, " & _
"ContactName, City, Country, Region FROM Customers"

'Add the field codes to the document to create the form letter
With .Fields
Set oSel = oApp.Selection 'Reduces interface requests
.Add oSel.Range, "CompanyName"
oSel.TypeParagraph
.Add oSel.Range, "Address"
oSel.TypeParagraph
.Add oSel.Range, "City"
oSel.TypeText ", "
.Add oSel.Range, "Country"
oSel.TypeParagraph
oSel.TypeParagraph
oSel.TypeText "Dear "
.Add oSel.Range, "ContactName"
oSel.TypeText ","
oSel.TypeParagraph
oSel.TypeParagraph
oSel.TypeText " This letter is to inform you..."
oSel.TypeParagraph
oSel.TypeParagraph
oSel.TypeText "Sincerely, John Smith"
End With
End With

'Perform the mail merge to a new document
With oMainDoc
.MailMerge.Destination = wdSendToNewDocument
.MailMerge.Execute Pause:=False
End With

'Make Word visible so that the user can see the new
'mail merge document
oApp.Visible = True Eric De Decker
vbg.be@vbgroup.nl

License And Copy Protection AxtiveX.

Download Demo version on my Site:
 
Thanks for that - will that work with Word 6? I thought you had to use Word Basic with Word 6?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top