[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 396
  • Last Modified:

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

0
PLA_LTM
Asked:
PLA_LTM
  • 7
  • 7
1 Solution
 
Chris BottomleyCommented:
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
 
PLA_LTMAuthor Commented:
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
 
Chris BottomleyCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
PLA_LTMAuthor Commented:
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
 
Chris BottomleyCommented:
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
 
PLA_LTMAuthor Commented:
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
 
Chris BottomleyCommented:
Sorry you missed the point to rephrase:

Will there be more than one document break, (triggers) in a file?
0
 
PLA_LTMAuthor Commented:
a per-merged file will have one "trigger"

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

0
 
PLA_LTMAuthor Commented:
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
 
PLA_LTMAuthor Commented:
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
 
Chris BottomleyCommented:
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
 
Chris BottomleyCommented:
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
 
PLA_LTMAuthor Commented:
your code lines, 17 through 21, are they supposed to be commented out?  

0
 
Chris BottomleyCommented:
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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now