none
Word Document Splitter Macro Error when blank page RRS feed

  • Question

  • I have a working Macro for Word that splits the pages for me the way I need it to, except if it finds a blank page it generates an error. Is there anyway for it to catch that error and ignore it so I do not have to hit end?

    '
    ' Splitter Macro
    '
    '
       ' Used to set criteria for moving through the document by page.
       Application.Browser.Target = wdBrowsePage
     Dim i As Integer
     Dim DocNum As Integer
       For i = 1 To (ActiveDocument.BuiltInDocumentProperties("Number of Pages"))
          
          'Select and copy the text to the clipboard.
          ActiveDocument.Bookmarks("\page").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 "C:\Test\"
          DocNum = DocNum + 1
          ActiveDocument.SaveAs FileName:="test_" & DocNum & ".docx"
          ActiveDocument.Close
    
          ' Move the selection to the next page in the document.
          Application.Browser.Next
       Next i
       ActiveDocument.Close savechanges:=wdDoNotSaveChanges

    • Moved by Bill_Stewart Friday, May 29, 2015 2:31 PM Move to more appropriate forum
    Friday, May 29, 2015 2:09 PM

Answers

  • Ah oops :) I will make sure to put it there next time!

    Found my solution by using an errhandler.

    ErrHandler Tutorial:

    https://support.microsoft.com/en-us/kb/141571

    Friday, May 29, 2015 2:35 PM

All replies

  • You are posting in the wrong forum.  You want the Office forums for macros: http://answers.microsoft.com/en-us/office

    \_(ツ)_/

    Friday, May 29, 2015 2:33 PM
  • Ah oops :) I will make sure to put it there next time!

    Found my solution by using an errhandler.

    ErrHandler Tutorial:

    https://support.microsoft.com/en-us/kb/141571

    Friday, May 29, 2015 2:35 PM
  • Hi Chuck

    Another possibile way - which would avoid the "overhead" of error handling, would be to check the content of the page. A document with a "blank page" will always have one, and only one, character: a paragraph mark (ANSI 13). A non-blank page will have more than one character. So it should be enough to test the number of characters before proceding. In Word VBA-speak (meaning you might need to ajdust it for the programming language you're using):

    If Len(ActiveDocument.Content.Text) > 1 Then


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, May 29, 2015 4:59 PM
    Moderator