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

Use a word merge field for the file name

Status
Not open for further replies.

janyc

Technical User
Nov 17, 2008
3
US
I have a Word Merge document that includes a unique id for each section. I want to save each section as a separate file using the unique id as the file name (ex. TL201.doc). I'm merging the data into Word from an Access Database.

Here is my code:

Sub MergeAllRecordsToFolder()
'
' MergeAllRecordsToFolder Macro

Dim x As Long
Dim i As Long
Dim docid2 As String


With ActiveDocument.MailMerge

.Destination = wdSendToNewDocument
.SuppressBlankLines = True

'get the record count of the datasource
With .DataSource
.ActiveRecord = wdLastRecord
x = .ActiveRecord
'set the activerecord back to the first
.ActiveRecord = wdFirstRecord

End With

'loop the datasource count and merge one record at a time
For i = 1 To x
.DataSource.FirstRecord = i
.DataSource.LastRecord = i
.Execute Pause:=True
ActiveDocument.SaveAs FileName:="C:\Documents and Settings\user\My Documents\Projects\TARGETLETTERS\Letters" & i & ".doc"
ActiveDocument.Close wdDoNotSaveChanges

Next i
End With
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top