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

Access to Word Merge Problem 1

Status
Not open for further replies.
Feb 6, 2003
48
US
Hi-

I have a database that creates a text file that serves as the datasource for a merge letter. I have created a form in Access with a command button that when clicked opens Word and the merge document and also performs the merge. However only the first record merges, nothing happens with the remaining records. Any thoughts, thanks in advance for any help? Here is the code:

Private Sub Command7_Click()

Dim WordApp As Object
Dim MailMerge As Object

'Open Word
Set WordApp = CreateObject("Word.Application")

With WordApp
.Visible = True
.Documents.Open FileName:="C:\totcomp\TotalCompLetter-mm.doc", ReadOnly:=False

Set MailMerge = WordApp.ActiveDocument.MailMerge

MailMerge.MainDocumentType = wdFormLetters
MailMerge.OpenDataSource Name:="C:\TotComp\source-empdata.txt", _
ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, Revert:=False, Connection:=""

With MailMerge
.Destination = wdSendToNewDocument
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
End With

End Sub
 
I tried your code and found that it opened the template document and merged to a new document, Form Letters, that had pages / sections for each record in the sample merge data file. Have you checked earlier coding to ensure that sufficient records are being sent to the text file?
 
Hi-

Thanks for the response. The source file has 30 records in it. When I run the code, only the first record merges. Nothing happens to the remaining records. A colleague of mine suggested that I try using For..Next to cycle through the text record, but I don't know how to make it work (or if it will).
 




In this Word merge document, turn on your macro recorder and record advancing on record using the NEXT button in the MailMerge Toolbar.

Use/customize this code in your application.

Skip,

[glasses] [red][/red]
[tongue]
 
Hi Skip-

Thanks for the information. I tried to do this but I can't get find an option that allows me to merge the document one record at a time. As soon as I click "merge" the whole merge is completed, and I am left with the code I already have.
 




"...As soon as I click 'merge'..."

Don't click merge. Just connect the data source and insert your merge fields.

Toggle the View Merged Data button on the MailMerge Toolbar to make the DATA visible.

NEXT and PREVIOUS buttons will navigate the source data.

Skip,

[glasses] [red][/red]
[tongue]
 
Hi Skip-

I wanted to say thanks for the input. I never could get the merge to work from Access. It would only merge the first record and I could not figure out how to write the code so that each record in the text file would be merged (the perils of being a self taught newb).

I was able to find a work around however. I put the merge code that my Word macro generated in a Document_Open event. I open the document from Access and it merges like a charm. It feels a bit like a hack, but at this point I'll take what I can get.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top