View Single Post
 
Old 09-02-2018, 09:22 PM
raywood raywood is offline Windows 10 Office 2010 64bit
Novice
 
Join Date: Sep 2018
Posts: 10
raywood is on a distinguished road
Default Divide Text Doc into Multiple Parts at .pa Delimiter

I have a number of text documents that have .pa where manual page breaks should be. If memory serves, that was how we did manual page breaks in XyWrite.

I'd like to develop a macro that would use those .pa delimiters as document ending points. So there would be some text; the macro would grab that text, down to the first occurrence of .pa, and move it to a new file; that first occurrence of .pa would itself be deleted; the newly created file would be saved with some automatic name; and the process would repeat, down to the (new) first occurrence of .pa, until the original file was empty.

I've used Word macros, but I don't know a lot about them. After some digging, I found this, and tried to modify it to fit, but I don't know what to substitute for " ActiveDocument.Bookmarks."

Code:
Sub BreakOnPage()
   ' Used to set criteria for moving through the document by page.
   Application.Browser.Target = wdBrowsePage

   For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")
      
      'Select and copy the text to the clipboard
      ActiveDocument.Bookmarks(".pa").Range.Copy

      ' Open new document to paste the content of the clipboard into.
      Documents.Add
      Selection.Paste

      ' Removes the break that is copied at the end of the page, if any.
      Selection.TypeBackspace
      ChangeFileOpenDirectory "D:\"
      DocNum = DocNum + 1
      ActiveDocument.SaveAs FileName:="test_" & DocNum & ".doc"
      ActiveDocument.Close

      ' Move the selection to the next page  in the document
      Application.Browser.Next
   Next i
   ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub
Reply With Quote