Link to home
Start Free TrialLog in
Avatar of NovaRising
NovaRising

asked on

Macro: Save Word Document as Merge Field

I have a large merge done from a .dat file into word. I would like to save each record in the merge as a seperate file. This is easy to do manually. However, I have so many I'm wondering if there is a way to get a macro to do this. I was able to suceed in getting that to work except I don't know how to name the document as a field name contained in the merge.

For example...
Child ID: CnBio_ID
First Name: First_Name
Last Name: Last_Name
etc.......

How can I make the macro save the file as what is in the CNBio_ID merge field?
Sub SaveAsSeperateDocument()
 
Dim x As Long
Dim i As Long
 
 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
 
///////////////////////// PROBLEM IS HERE ///////////////////////////////////////    
     ChildID = MERGEFIELD CnBio_ID
         
      ActiveDocument.SaveAs FileName:=ChildID
/////////////////////////////////////////////////////////////////////////////////////////////
 
      ActiveDocument.Close
   
   Next i
 End With
End Sub

Open in new window

SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NovaRising
NovaRising

ASKER

I wasn't able to get that to work. Might it place the documents in some location? It didn't appear to do anything.
Taking what you did have there I was able to come up with the following that will save the first document but says "it cannot save the next document with the same name".

Sub MergeAllRecordsToPrinter()
 
'
 
' MergeAllRecordsToPrinter Macro
 
 
 
 
 
Dim x As Long
 
Dim i As Long
 
 
 
 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 .DataSource.DataFields("CnBio_ID") & ".doc"
        ActiveDocument.Close wdDoNotSaveChanges
 
 
   
 
   Next i
 
 End With
 
End Sub

Open in new window

By the way, I'm using Word 2003 if that makes a difference.
I think that the rest of the message is: 'as an open document'.

Make sure that all earlier results documents are closed.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks that works perfectly. Sorry for the slow reply, I was out of the office.