Solved

vba code to split and save mail merged word document

Posted on 2014-02-14
8
5,065 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 52

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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 52

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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
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 …

622 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