Solved

vba code to split and save mail merged word document

Posted on 2014-02-14
8
4,671 Views
Last Modified: 2016-10-22
Hi Experts word 2007

I need vba code to split and save mail merged word document into individual word doc and saved to a specific file path...based on company name as unique identifier..

Unable find the code on the net..also need instructions on hiw the code works for learning purposes.
0
Comment
Question by:route217
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39858891
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 250 total points
ID: 39858923
In a letter-type of  mail merge the result document is separated into sections, one per record in the datasource.

Here is the code with some comments added.
Sub SplitMergeResult()
     'declare the variables
    Dim sec As Section 
    Dim rng As Range
    Dim strName As String
    Dim DocA As Document
    Dim DocB As Document
   
    Set DocA = ActiveDocument 'make the actived ocument the source document
    For Each sec In DocA.Sections 'step through each section 
        Set rng = sec.Range 'get the range of the section
        rng.MoveEnd wdCharacter, -1 'omit section break
        rng.Copy
        strName = Split(sec.Range.Paragraphs(1).Range.Text, vbTab)(0) 'find the new name in the text. It might well be in a different place in your document
        Set DocB = Documents.Add 'create a new blank document
        DocB.Range.Paste 'paste the text from the current section
        DocB.SaveAs strName & ".doc" 'save the new document and 
        DocB.Close 'close it
    Next sec 'now treat the nest section
End Sub 'all done, so end the procedure

Open in new window

0
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 250 total points
ID: 39859398
Hi,

You could use this code  in you mail merge template document ( placed in the ThisDocument module )

it captures the mailmerge events
Dim WithEvents wdapp As Application
Dim bCustomProcessing As Boolean

Private Sub Document_Open()

Set wdapp = Application
bCustomProcessing = False
ThisDocument.MailMerge.DataSource.ActiveRecord = 1
ThisDocument.MailMerge.ShowWizard 1
With ActiveDocument.MailMerge
   If .MainDocumentType = wdFormLetters Then
       .ShowSendToCustom = "Custom Letter Processing"
   End If
End With

End Sub
Private Sub wdapp_MailMergeWizardSendToCustom(ByVal Doc As Document)

bCustomProcessing = True
Doc.MailMerge.Destination = wdSendToNewDocument
With Doc.MailMerge
    For rec = 1 To .DataSource.RecordCount
        .DataSource.ActiveRecord = rec
        .DataSource.FirstRecord = rec
        .DataSource.LastRecord = rec
        .Execute
    Next
End With

MsgBox "Merge Finished"
End Sub


Private Sub wdapp_MailMergeAfterMerge(ByVal Doc As Document, ByVal DocResult As Document)
If bCustomProcessing = True Then
    With Doc.MailMerge.DataSource.DataFields
        sFirmPathName = .Item(1).Value ' First Column of the data
    End With
    DocResult.SaveAs "c:\Documents\" & sFirmPathName & "\MailDocument.docx", wdFormatXMLDocument
    DocResult.Close False
End If
End Sub

Open in new window


at line 37  replace the 1 of  ".Item(1).Value" with the column number of the FirmID
at line 39 Change the filename and Path to your convenience

Close the file and reopen before doing the merge, The Wizard will appear and at the last step Click "Custom Letter Processing"

Regards
EE-20140214.zip
1
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:route217
ID: 39859548
Thanks experts extremely appreciate the feedback.
0
 

Author Comment

by:route217
ID: 39860531
Hi experts

Can I run the vba via a button in the word document? If yes how what are the necessary steps...so I can learn.
0
 

Author Comment

by:route217
ID: 39864303
Hi experts

How do I run this macro.
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39864388
For my Code
First Activate the document containîng the mailmerge
Press Alt+F11 to go to the Visual Basic Editor.

Insert the code  in the module ThisDocument int the Microsoft Word Objects of your file ( Not the Normal)
Click Ctrl+R to Toggle the Project Explorer

You wi8ll have to change the code to accomodate your need ( Change the path,, name of the document and so on)

then close the document and reopen it to make the macro work
Regards
0
 

Expert Comment

by:Vincent chuan
ID: 41854952
HI Rgonzo1971

i got a 2 issue after i try out the code


1) Dim WithEvents wdapp As Application , the code is highlighted in red
2) .MainDocumentType  , the code is highlighted in red
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
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…

777 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