Reply
 
Thread Tools Display Modes
  #1   Report Post  
cbedell cbedell is offline
Junior Member
 
Posts: 1
Default Save Mailmerge split pages by unique file name

Hello -

I am running the following macro to split my mailmerge document into individual dcouments. I want to save each document with the unique merge field ("invoice"). Some older posts suggested placing the desired filename as the first line and then have the macro select that as the filename and then remove it. I tried that, but couldn't combine the two macros. Can anyone help?

cbedell

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(wdStatisticP ages)
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
Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to split word macro file into indivisual pages vinod koti Microsoft Word Help 1 August 5th 09 12:23 PM
how can I split each page of a file and save them in a seprated file? Graham Mayor Microsoft Word Help 3 October 5th 05 12:40 AM
how can I split the every pages of a file and save them seprately in another files? Charles Kenyon Formatting Long Documents 1 October 4th 05 07:51 AM
how do i split 1500 pages doc file with cross-links ? noob Microsoft Word Help 1 March 30th 05 01:36 PM
Split 600 pg doc and save as 300 different file names. 1 pair of. BudMan New Users 0 February 17th 05 07:13 PM


All times are GMT +1. The time now is 07:39 AM.

Copyright ©2000 - 2023, Jelsoft Enterprises Ltd.
Copyright 2004-2023 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"