Splitter

D

Dominique Feteau

I got some code that would help me split a large merged document into
individual documents. problem is that the final individual document has an
extra page at the end. how can i fix the code or the original document so
that i dont have that extra page at the end. here's the code i'm using:

Sub Splitter()

Dim mask As String
Selection.EndKey Unit:=wdStory
Letters = Selection.Information(wdActiveEndSectionNumber)
mask = "ddMMyy"

Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter < Letters
DocName = "C:\My Documents" & Format(Date, mask) & " " &
LTrim$(Str$(Counter))
ActiveDocument.Sections.First.Range.Cut
Documents.Add
Selection.Paste
ActiveDocument.SaveAs FileName:=DocName, FileFormat:=wdFormatDocument
ActiveWindow.Close
Counter = Counter + 1
Wend

End Sub
 
K

Klaus Linke

Hi Dominique,

Since this newsgroup is more appropriate, I'll re-post my reply from
microsoft.public.word.word6-7macros.
Please don't post the same question separately to different groups.

The first section contains the trailing section mark.
That section mark in turn needs to be followed by a paragraph mark.

If you don't need the section mark, you can eliminate both the section mark
and paragraph mark before cutting:

Dim myRange As Range
' ...
Set myRange = ActiveDocument.Sections.First.Range
myRange.MoveEndWhile _
Cset:=ChrW(12) & ChrW(13), _
Count:=wdBackward
myRange.Cut
' ...

Regards,
Klaus
 
K

Klaus Linke

Thanks for the response. But where should I put that?


Hi Dominique,

The code was a replacement for
ActiveDocument.Sections.First.Range.Cut

It just excluded trailing section breaks or paragraph marks.

Testing your macro, I re-wrote it a bit -- nothing of it really necessary
.... just to show some different ways to reach the same goal.

Hope it works as you wanted,
Klaus


Sub SplitterTake2()

Dim strMask As String
Dim strDocName As String
Dim i As Long
Dim rngSection As Range
Dim docNew As Document
Dim docOld As Document

strMask = "ddMMyy"
Set docOld = ActiveDocument

For i = 1 To ActiveDocument.Sections.Count
strDocName = _
Options.DefaultFilePath(wdDocumentsPath) & _
"\" & Format(Date, strMask) & " " & _
LTrim$(Str$(i))
Set rngSection = _
ActiveDocument.Sections(i).Range
rngSection.MoveEndWhile _
Cset:=ChrW(12) & ChrW(13), _
Count:=wdBackward

Set docNew = Documents.Add
docNew.Content.FormattedText = rngSection
docNew.SaveAs _
FileName:=strDocName, _
FileFormat:=wdFormatDocument
docNew.Close
Next i

End Sub
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Here is a modification of that macro that converts the Next Page Section
Break at the end of each document created by the macro into a Continuous
Section Break that eliminates the unwanted page while retaining any page
setup information contained in the section break.

Sub splitter()

' splitter Macro

' Macro created by Doug Robbins to save each letter created by a mailmerge
as a separate file.

Dim Letters As Integer, Counter As Integer
Letters = ActiveDocument.Sections.Count
Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter < Letters
DocName = "Myletter" & LTrim$(Str$(Counter))
ActiveDocument.Sections.First.Range.Cut
Documents.Add
Selection.Paste
ActiveDocument.Sections(2).PageSetup.SectionStart = wdSectionContinuous
ActiveDocument.SaveAs FileName:=DocName, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
ActiveWindow.Close
Counter = Counter + 1
Wend

End Sub

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
K

Klaus Linke

Doug said:
Here is a modification of that macro that converts the Next Page Section
Break at the end of each document created by the macro into a Continuous
Section Break that eliminates the unwanted page while retaining any page
setup information contained in the section break.


Hi Dominique, Doug,

That may be the better solution in case you want to keep the section
formatting.

Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top