PDA

View Full Version : Split Into Pages Macro - update for .docm



lilactrap
01-29-2014, 01:27 PM
My company has used this macro to "split" large mail merge files into single page Word documents. It has worked great up until the new version of Word.
I changed my file type to .docm. The macro ran and made the new files, but they were made also in the docm format. When you try to open them "...cannot be opened because there are problems with the contents."
I'm suspecting the problem is when it makes the new documents, they are macro-enabled as well (as opposed to normal docx). Does anyone have a suggestion to fix this?

--please-- have patience with me. I'm not a programmer. this is my first time doing code :(

The macro was originally from this website (but this post won't let me put the link in because I'm a new member).


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

gmaxey
01-29-2014, 03:05 PM
Try this. Merge documents are typically delimited with section breaks so the "Section Break" option should work.

http://gregmaxey.mvps.org/word_tip_pages/document_splitter.html

westconn1
01-30-2014, 02:22 AM
I'm suspecting the problem is when it makes the new documents, they are macro-enabled as well (as opposed to normal docx). Does anyone have a suggestion to fix this?
you should be able to specify document type at the saveAs method

macropod
02-01-2014, 10:33 PM
you should be able to specify document type at the saveAs method
but even your own code doesn't do that:

strNewFileName = Replace(docMultiple.FullName, ".doc", "_" & Right$("000" & iCurrentPage, 4) & ".doc")
docSingle.SaveAs strNewFileName 'save the new single-paged document
To save as a docx file, you need to specify both the docx extension and use the SaveAs2 method.