Link to home
Start Free TrialLog in
Avatar of PLA_LTM
PLA_LTM

asked on

Separate Documents Macro

Hello Experts,

We use the Word's Mail Merge function to produce, well, a mail merged document.  Then, using the attached Code, the user can run a macro that will separate out each record and save them individually.  
The macro looks for a section break and "cuts" everything prior to it and save it in a new file.  This have been working great until now.  our forms are getting more complicated and the section break isn't cutting it.

How can i modify the macro to look for another "trigger" other than a section break? I'm open to using any ascii character, a text in white font (which won't print), or any other suggestion
Sub SeparateDocs()
' splitter Macro

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

Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter < Letters
    DocName = "U:\" & Environ("USERNAME") & "\SepDocs\" & Format(Time, "h mm ss") & " " & LTrim$(Str$(Counter))
ActiveDocument.Sections.First.Range.Cut
Documents.Add Template:="V:\Macros\Template.doc", _
NewTemplate:=False, DocumentType:=0
Selection.Paste
ActiveDocument.SaveAs FileName:=DocName, FileFormat:=wdFormatDocument
ActiveWindow.Close
Counter = Counter + 1
Wend

End Sub

Open in new window

Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

It might help to know why the section break doesn't 'cut it' anymore.

Also if you can provide a sample file without anything sensitive?

Chris
Avatar of PLA_LTM
PLA_LTM

ASKER

Thank you for your prompt response.

Because our forms now require landscaped pages interspersed throughout the form per record, the use of Section breaks to denote the end of a "record" does not work.

example:
Page1    Portrait
Page2    Portrait
Page3    Landscape (previously this was Portrait)
Page4    Portrait

NOTE: The above is per record.

The use of our existing macro will give us Pages1&2  saved as one file.  Page 3 as one file and Page 4 as one file.  

It should be that pages 1 through 4 should be separated out as one file.

I am looking for a "marker" that can be placed at the end of Page4.  The macro should be able to read this marker and "cut everything above it and save it as a new file.


Apologies for dropping the ball ... but do you have a sample file as requested ... it'll be easier to make sure we address the relevant structure.

Chris
Avatar of PLA_LTM

ASKER

Attached are the example files requested.

it's a very simple setup.

Page 1 ends with a page break
Pages 2 and 3 end with a section break
Page 4 is the last page

the current macro (shown in my first entry) will separate the document in this manner, per record:

Pages 1 and 2 as one file
Page 3 as one file
Page 4 as one file

We would like to have pages 1 through 4 be saved as one file per record.

the current macro will look for a section break and "Cut" everything preceding it and save as a new file.  Because page 3 is in Landscape, which requires a section break, it separates out Pages 1 and 2 as one file, page 3 as one file and Page 4 as one file..

I guess what i'm asking is, can i change this line in the macro:  

Letters = Selection.Information(wdActiveEndSectionNumber)

to reference something other than a Section Break.  Ideally some unusual ascii character.


 Page-1.doc Test.csv
Will a file only have the one 'break', (ignoring section breaks I mean document break).

i.e. is the activity recursive until the document break is no more?

Chris
Avatar of PLA_LTM

ASKER

Each document may have several page breaks.


in the example i listed above,

Page 1 has a page break
Pages 2 and 3 has a section break
Page 4 is the last page.

It is possible to have a more pages, each page will have a page break (or a section break depending on orientation) .  

Ideally i would like to add a character at the end of the last page (whether the last page be page 4 or page 100)  and the macro will look for this character as the "separate trigger".   Right now the section break is the trigger.
Sorry you missed the point to rephrase:

Will there be more than one document break, (triggers) in a file?
Avatar of PLA_LTM

ASKER

a per-merged file will have one "trigger"

in a merged file there will be several "Triggers"  (basically a trigger per record)

Avatar of PLA_LTM

ASKER

Really all i need is a way to choose something else other than a section break as a trigger.

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


how can i modify the above string to achieve that?
Avatar of PLA_LTM

ASKER

I'm new to coding

how can i incorporate the following to my original macro?

Dim splitdoc As String
splitdoc = "test"
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = splitdoc
        .Replacement.Text = ""
        .Forward = True
       End With
    Selection.Find.Execute
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
   
End Sub
I am at present trying to use a 'comment' for the seperator.  The problem is that (for me) word range management is not easy and with a new version to contend with it's a right 'kerfuffle'.

I hope to try on an older version of word later today.  Bacsially the same range management methods would be required in each case so if I can get 'my' solution to work it ought to adapt easily to yours.

Chris
ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PLA_LTM

ASKER

your code lines, 17 through 21, are they supposed to be commented out?  

No, only 17/18 should be commented out and that was there for testing to make sure the required range was being 'selected' when I went for the cut.

Chris