Solved

Separate Documents Macro

Posted on 2010-11-18
16
379 Views
Last Modified: 2013-11-25
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

0
Comment
Question by:PLA_LTM
  • 7
  • 7
16 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34170712
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
0
 

Author Comment

by:PLA_LTM
ID: 34173351
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.


0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34245060
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
0
 

Author Comment

by:PLA_LTM
ID: 34248706
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
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34248771
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
0
 

Author Comment

by:PLA_LTM
ID: 34248846
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.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34248907
Sorry you missed the point to rephrase:

Will there be more than one document break, (triggers) in a file?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:PLA_LTM
ID: 34249006
a per-merged file will have one "trigger"

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

0
 

Author Comment

by:PLA_LTM
ID: 34249170
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?
0
 

Author Comment

by:PLA_LTM
ID: 34253264
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
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34255060
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
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 34255362
The following seems to work ok for me:

You need to change:

                   ActiveDocument.SaveAs "c:\deleteme\fred.doc"

for your save docname.

It looks for the appearance of the string <<Fred>> anywhere on a page to indicate that the PREVIOUS page denotes a complete document therefore if <<Fred>> appears on page 4 then pages 1 to 3 will be cut for the save.

Chris
Sub getComments()
Dim cmt As Comment
Dim pg As Integer
Dim docRange As Range
Dim bolFoundTag As Boolean
Dim bolEndItAll As Boolean

   Application.ScreenUpdating = False
   bolFoundTag = True
   Do While Not bolEndItAll
       For pg = 1 To ThisDocument.Range.Information(wdNumberOfPagesInDocument)
           If pg =
ThisDocument.Range.Information(wdNumberOfPagesInDocument) Then
bolEndItAll = True
           bolFoundTag = False
           Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=pg
   '        ThisDocument.Range(ThisDocument.Bookmarks("\Page").Range.Start,
ThisDocument.Bookmarks("\Page").Range.End).Select
           With
ThisDocument.Range(ThisDocument.Bookmarks("\Page").Range.Start,
ThisDocument.Bookmarks("\Page").Range.End)
               If .Comments.Count > 0 Then
                   For Each cmt In .Comments
                       If LCase(cmt.Range.Text) = LCase("<<Fred>>") Then
                           bolFoundTag = True
                           cmt.Delete
                           Exit For
                       End If
                   Next
               End If
               If bolFoundTag Then
                   ' save docrange
                   'Stop
                   docRange.Cut
                   Application.Documents.Add
                   Selection.Paste
                   ActiveDocument.SaveAs "c:\deleteme\fred.doc"
                   ActiveWindow.Close
                   Exit For
               Else
                   Set docRange =
ThisDocument.Range(ThisDocument.Range.Start, .End)
               End If
           End With
       Next
   Loop
   Application.ScreenUpdating = True

End Sub

Open in new window

0
 

Author Comment

by:PLA_LTM
ID: 34257269
your code lines, 17 through 21, are they supposed to be commented out?  

0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34257349
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
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now