Solved

Separate Documents Macro

Posted on 2010-11-18
16
391 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

707 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