Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-21-2012, 09:54 AM
nicnad nicnad is offline Mail Merge to multiple .doc files named after a mergefield Windows XP Mail Merge to multiple .doc files named after a mergefield Office 2007
Novice
Mail Merge to multiple .doc files named after a mergefield
 
Join Date: Feb 2012
Posts: 2
nicnad is on a distinguished road
Default Mail Merge to multiple .doc files named after a mergefield

Hi,

I have a mail merge .doc file. I use the directory function to create a single .doc file with all the records in it.

I have the following code (taken from this website : http://vbaexpress.com/kb/getarticle.php?kb_id=727

Code:
Option Explicit 'This goes in the Declarations section of your code module.
 'Hopefully it is already there because you have ticked the 'Require Variable Declaration' _
checkbox. (Tools/Options, Editor tab.) 
 
 
Sub SplitIntoPages() 
    Dim docMultiple As Document 
    Dim docSingle As Document 
    Dim rngPage As Range 
    Dim iCurrentPage As Integer 
    Dim iPageCount As Integer 
    Dim strNewFileName As String 
     
    Application.ScreenUpdating = False 'Makes the code run faster and reduces screen _
    flicker a bit. 
    Set docMultiple = ActiveDocument 'Work on the active document _
    (the one currently containing the Selection) 
    Set rngPage = docMultiple.Range 'instantiate the range object
    iCurrentPage = 1 
     'get the document's page count
    iPageCount = docMultiple.Content.ComputeStatistics(wdStatisticPages) 
    Do Until iCurrentPage > iPageCount 
        If iCurrentPage = iPageCount Then 
            rngPage.End = ActiveDocument.Range.End 'last page (there won't be a next page)
        Else 
             'Find the beginning of the next page
             'Must use the Selection object. The Range.Goto method will not work on a page
            Selection.GoTo wdGoToPage, wdGoToAbsolute, iCurrentPage + 1 
             'Set the end of the range to the point between the pages
            rngPage.End = Selection.Start 
        End If 
        rngPage.Copy 'copy the page into the Windows clipboard
        Set docSingle = Documents.Add 'create a new document
        docSingle.Range.Paste 'paste the clipboard contents to the new document
         'remove any manual page break to prevent a second blank
        docSingle.Range.Find.Execute Findtext:="^m", ReplaceWith:="" 
         'build a new sequentially-numbered file name based on the original multi-paged file name and path
        strNewFileName = Replace(docMultiple.FullName, ".doc", "_" & Right$("000" & iCurrentPage, 4) & ".doc") 
        docSingle.SaveAs strNewFileName 'save the new single-paged document
        iCurrentPage = iCurrentPage + 1 'move to the next page
        docSingle.Close 'close the new document
        rngPage.Collapse wdCollapseEnd 'go to the next page
    Loop 'go to the top of the do loop
    Application.ScreenUpdating = True 'restore the screen updating
     
     'Destroy the objects.
    Set docMultiple = Nothing 
    Set docSingle = Nothing 
    Set rngPage = Nothing 
End Sub


It splits the document in separate .doc files.

First, I would like to add code to have the new word document have specific margins and paragraph parameters since the splitted files have default parameters and does not fit in a single page.

Second, I would like the code to name the files after a mergefield instead of adding 1,2,3,etc... to the filename.

Could you please help me add the necessary code in the above macro?

Thank you.
Reply With Quote
  #2  
Old 02-22-2012, 01:59 AM
macropod's Avatar
macropod macropod is offline Mail Merge to multiple .doc files named after a mergefield Windows 7 64bit Mail Merge to multiple .doc files named after a mergefield Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

hi nicnad,

Since you obtained the macro from another forum, you should post any queries about it there - as much as anything as a prompt to the author who, after all, will also be more familiar with the code than someone who's not been involved in its development.

Having said that, you might find it worthwhile visiting: http://www.gmayor.com/individual_merge_letters.htm
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mail Merge and Files on Network Drive Joe Switch Mail Merge 0 09-15-2011 09:06 AM
Mail Merge to multiple .doc files named after a mergefield Multiple Mail Merge? imogul Mail Merge 1 06-24-2011 07:20 PM
Mail Merge to multiple .doc files named after a mergefield convert multiple csv files to multiple excel files mit Excel 1 06-14-2011 10:15 AM
Mail Merge to multiple .doc files named after a mergefield email as pdf attachment - subject line and attachment named after mail merge Nexus Mail Merge 12 04-13-2011 11:34 PM
Mergefield date error michielvanderbiest Mail Merge 1 04-12-2010 11:22 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:32 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft