Loop

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
 
D

Doug Robbins - Word MVP on news.microsoft.com

Dim i as Long
For i = 1 to 7
ActiveDocument.Sections(1).Range.Cut
Next i

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
D

discostu1975

Thanks so much, for some reason it didn't like the i counter again so I
changed it to a j and away we went....MUCH appreciated!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top