D
discostu1975
I am working on a function that will copy the first 7 sections of a document
into their own files in a document manager. The second part of the function
I would like to take the last 13 sections of the document and save it as 1
file into our DM. The first 7 happen with no difficulty. The last 13
unfortunately I am having difficulty with. I have tried to reuse code and
it's not working. What happens is the cut portion of the code takes out
every other section for 7 sections...I imagine what is happening is that the
cut moves my document up and now what used to be section 2 is now section 1
but my counter has moved on to section 2....how can I reset i (my counter) to
properly grab the first 7 sections?
Function BreakOnSection()
'
' BreakOnSection Macro
' Macro created 4/3/2009 by switze_s
'
' Used to set criteria for moving through the document by section.
Application.Browser.Target = wdBrowseSection
'A mailmerge document ends with a section break next page.
'Subtracting one from the section count stop error message.
For i = 1 To ((ActiveDocument.Sections.Count) - 13)
'Select and copy the section text to the clipboard
ActiveDocument.Bookmarks("\Section").Range.Copy
'Create a new document to paste text from clipboard.
Application.DisplayAlerts = False
Documents.Add
Selection.Paste
' Removes the break that is copied at the end of the section, if any.
Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
ChangeFileOpenDirectory "u:\"
DocNum = DocNum + 1
ActiveDocument.Save
ActiveDocument.Close
' Move the selection to the next section in the document
Application.Browser.Next
Next i
' Cut the firt 7 sections from the document to leave the agreement as 1
document
For i = 1 To ((ActiveDocument.Sections.Count) - 13)
ActiveDocument.Bookmarks("\Section").Range.Cut
Next i
ActiveDocument.Save
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Function
into their own files in a document manager. The second part of the function
I would like to take the last 13 sections of the document and save it as 1
file into our DM. The first 7 happen with no difficulty. The last 13
unfortunately I am having difficulty with. I have tried to reuse code and
it's not working. What happens is the cut portion of the code takes out
every other section for 7 sections...I imagine what is happening is that the
cut moves my document up and now what used to be section 2 is now section 1
but my counter has moved on to section 2....how can I reset i (my counter) to
properly grab the first 7 sections?
Function BreakOnSection()
'
' BreakOnSection Macro
' Macro created 4/3/2009 by switze_s
'
' Used to set criteria for moving through the document by section.
Application.Browser.Target = wdBrowseSection
'A mailmerge document ends with a section break next page.
'Subtracting one from the section count stop error message.
For i = 1 To ((ActiveDocument.Sections.Count) - 13)
'Select and copy the section text to the clipboard
ActiveDocument.Bookmarks("\Section").Range.Copy
'Create a new document to paste text from clipboard.
Application.DisplayAlerts = False
Documents.Add
Selection.Paste
' Removes the break that is copied at the end of the section, if any.
Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
ChangeFileOpenDirectory "u:\"
DocNum = DocNum + 1
ActiveDocument.Save
ActiveDocument.Close
' Move the selection to the next section in the document
Application.Browser.Next
Next i
' Cut the firt 7 sections from the document to leave the agreement as 1
document
For i = 1 To ((ActiveDocument.Sections.Count) - 13)
ActiveDocument.Bookmarks("\Section").Range.Cut
Next i
ActiveDocument.Save
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Function