Solved

Macro: Save Word Document as Merge Field

Posted on 2009-04-07
6
975 Views
Last Modified: 2013-11-10
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

0
Comment
Question by:NovaRising
  • 3
  • 2
6 Comments
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 200 total points
ID: 24091030
This is a macro that I have used in the past.
Sub MergeToSingles()
Dim wrdMainDoc As Word.Document
Dim l As Integer
 
Set wrdMainDoc = ActiveDocument
With wrdMainDoc.MailMerge
    For l = 1 To .DataSource.RecordCount
        .Destination = wdSendToNewDocument
        .DataSource.FirstRecord = l
        .DataSource.LastRecord = l
        .Execute
        .DataSource.ActiveRecord = l
        ActiveDocument.SaveAs .DataSource.DataFields("Last_Name") & ".doc"
        ActiveDocument.Close wdDoNotSaveChanges
    Next l
End With
End Sub

Open in new window

0
 
LVL 3

Author Comment

by:NovaRising
ID: 24091533
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

0
 
LVL 3

Author Comment

by:NovaRising
ID: 24091547
By the way, I'm using Word 2003 if that makes a difference.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24091570
I think that the rest of the message is: 'as an open document'.

Make sure that all earlier results documents are closed.
0
 
LVL 23

Accepted Solution

by:
irudyk earned 300 total points
ID: 24092278
It seems like you are not getting the CnBio_ID value of the current record you are merging.  Using the code you post along with that from GrahamSkan, try the following code to see if that corrects the issue.
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
 
          'set active record to the one that was just merged
          .DataSource.ActiveRecord = i
 
          ActiveDocument.SaveAs .DataSource.DataFields("CnBio_ID") & ".doc"
          ActiveDocument.Close wdDoNotSaveChanges
 
     Next i
 
End With
 
End Sub

Open in new window

0
 
LVL 3

Author Closing Comment

by:NovaRising
ID: 31567698
Thanks that works perfectly. Sorry for the slow reply, I was out of the office.
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Surface Book vs Surface Pro 4 79
Excel copy picture into Outlook email 7 58
WORD 2013 "Link to Previous" Footer not working 3 38
Selenium docs api java index 3 20
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question