Log in

View Full Version : [SOLVED:] Split merge document, save as pdf and rename each individual file.



PanCea
01-21-2015, 03:06 PM
Hi there,

I was searching around this forum, but couldn't find really an answer for what I'm looking for.

My question is:
I have a mergefile, which I have wrote a code to split the file in seperate 3pages-files.
Each new file which contain 3 pages, have to be renamed on criteria.
So far, it works, for the first loop. When I debug, en do the next loop, it stuck in the first page.

I'm missing something in the code I wrote, but after 3 hours I honestly don't know what I'm missing.
Someone has a suggestion?

Hére is the code:

Sub SplitSaveAsMetaData()
Dim SP As Long
Dim LP As Long
Dim DocName As String




LP = ActiveDocument.ComputeStatistics(wdStatisticPages)
SP = 1


Do
Selection.HomeKey Unit:=wdStory
Selection.Find.Execute "Blahblah:", MatchCase = False
Selection.MoveRight wdWord, 1
Selection.Expand wdWord

DocName = Trim(Selection.Text)

ActiveDocument.ExportAsFixedFormat outputfilename:= _
"C:\Test\" & DocName & ".pdf", Exportformat:= _
wdExportFormatPDF, openafterexport:=False, optimizefor:= _
wdExportOptimizeForPrint, Range:=wdPrintFromTo, from:=SP, to:=SP + 2, _
Item:=wdExportDocumentContent, includedocprops:=True, keepirm:=True, _
createbookmarks:=wdExportCreateNoBookmarks, docstructuretags:=True, _
bitmapmissingfonts:=True, UseISO19005_1:=False


DoEvents
SP = SP + 3
Loop Until SP > LP
End Sub

PanCea
01-22-2015, 03:07 AM
Anyone who can help me out?

I forgot to mention that "blahblah:" comes 3 or for times in each splitted document.
I only need the first one (I think), because that's te selected criteria for the DocName.

gmayor
01-22-2015, 04:13 AM
It might save you a lot of effort if you simply merged your document using http://www.gmayor.com/individual_merge_letters.htm which will save a document for each record as PDF. The page also features example code which would be easily adapted to save as PDF if you prefer to roll your own.

Even closer to what you are doing now is http://www.gmayor.com/MergeAndSplit.htm.

PanCea
01-22-2015, 04:51 AM
Dear gmayor,

if I wanted a solution served on a platter, I woud already have download software.
But that's not what I'm looking for.

I want to learn, en understand what i did wrong.

gmayor
01-22-2015, 05:49 AM
Fair enough, though the first linked page does have code examples; however in order to provide a specific recommendation, it would help to know how the document is formatted with section breaks related to the page count. A merge would normally insert a section break between each record. Your code does not allow for section breaks.

There are several issues including a syntax error in the line Selection.Find.Execute "Blahblah:", MatchCase = False, and you move the search point back to the start in every loop so it will only ever find the first string.

It would be better to use Range.Find. As we don't know how your search string is positioned in the document, the following is only a guide:



Sub SplitSaveAsMetaData()
Dim SP As Long
Dim LP As Long
Dim oRng As Range
Dim DocName As String
'The characters in the filename string.
Const strList As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

LP = ActiveDocument.ComputeStatistics(wdStatisticPages)
SP = 1
Set oRng = ActiveDocument.Range
Do While oRng.Find.Execute(FindText:="Blahblah: ", MatchCase:=False) 'Find the search string (include the space after the search string if present)
oRng.Collapse 0 'Collapse the range to its end
oRng.MoveEndWhile strList 'Move the end of the range to the end of the word following the search string
DocName = Trim(oRng.Text) 'The range now should contain your filename
MsgBox DocName 'inserted as a name check while testing
'The following will only work if there are no section breaks in the document.
'If there are section breaks you need to investigate the syntax for From:= and To:= to include the section and page reference.
'ActiveDocument.ExportAsFixedFormat outputfilename:= _
"C:\Test\" & DocName & ".pdf", Exportformat:= _
wdExportFormatPDF, openafterexport:=False, optimizefor:= _
wdExportOptimizeForPrint, Range:=wdPrintFromTo, from:=SP, to:=SP + 2, _
Item:=wdExportDocumentContent, includedocprops:=True, keepirm:=True, _
createbookmarks:=wdExportCreateNoBookmarks, docstructuretags:=True, _
bitmapmissingfonts:=True, UseISO19005_1:=False
DoEvents
oRng.Collapse 0 'collapse the range to allow the search to continue from here.
'It might be better to split the document in reverse order into new documents and save those as PDFs.
SP = SP + 3
Loop
End Sub

PanCea
01-22-2015, 05:53 AM
Thank you.

This will help me (more than downloading software).
I sounded a little crue in my first reply, for which I apologize.

The problem is, I don't know exactly the sectionbreaks.
The mergefile was already in PDF file, meaning the merge was alreay done. I converted it into Word.
I don't know how to resolve this, or how to find the orinial sectionbreaks.

With this information I can process the understandings of VBA, and learn to do next time better, of differently.
I will look into your code. Thank you for looking into it.

I'll be back if I have questions.

gmayor
01-22-2015, 06:15 AM
If you converted the document from PDF, chances are that there are no section breaks, so you should be OK splitting by pages.

PanCea
01-22-2015, 06:20 AM
Dear Gmayor,

I tweaked my code into yours, and this is exactly what I'm looking for.
This is great, thanks!

Just a question for my understandings:
Why is it better to split the document in reverse order?

gmayor
01-22-2015, 07:35 AM
Just a question for my understandings:
Why is it better to split the document in reverse order?This is only necessary if you are breaking the document up by tearing off chunks and adding them to new documents. If you start from the beginning, then you run out of document before you reach the end count. You can allow for this, but it is easier just to start at the end and work backwards.:)

PanCea
01-22-2015, 09:01 AM
That makes sense.
Glad you helped me out!

In the meanwhile, I'm checking your site and codes, and try to understand how I can use this in the future.

I wil mark this as solved.