DISCOVER
×
Loading ...

How to Remove a Blank Page in Word in VBA

Updated April 17, 2017

Visual Basic for Applications (VBA) is the language that programmers use to extend Word's default functionality. With VBA, programmers can automate many of the step-by-step manual procedures that Word users regularly perform. Removing a blank page with VBA consists essentially of these two tasks: selecting a page and determining whether that selection is blank or not. A principal benefit of using VBA to achieve these tasks is the time saved from not having to do them by hand.

Loading ...
  1. Open any Word document consisting of several pages. Make sure that at least one or two of the pages have only nonprinting characters such as carriage returns or page breaks and that the remaining pages have at least one printable character.

  2. Open the VBA integrated development environment (IDE) by pressing Alt plus F11. Press the "Insert" menu heading, followed by pressing "Module" to insert a new module into your document. You'll create the VBA program in this module.

  3. Type or paste the following subroutine into the new code module:

  4. Public Sub deleteBlankPage()

  5. Selection.GoTo What:=wdGoToBookmark, Name:="\page"

  6. End Sub

  7. This subroutine directs VBA to go to a specific, hidden bookmark that Word maintains for each document. The hidden bookmark is called "\page" and refers to the page continuing the current selection or insertion point.

  8. Type this code into the area below the statement from the previous step that begins with "Selection":

  9. If isBlankSelection Then

  10. Selection.Delete

  11. End If

  12. This portion of code directs VBA to call a function (which you'll write in a subsequent step) that determines whether the current selection is blank or not. If it is, the code directs VBA to delete the selection.

  13. Type the following code after the "End Sub" statement to implement the function "isBlankSelection":

  14. Public Function isBlankSelection()

  15. For Each c In Selection.Characters

  16. If (c <> vbCr And c <> vbTab And c <> vbFormFeed And c <> " ") Then

  17. End If

  18. Next

  19. isBlankSelection = True

  20. End Function

  21. This function performs a series of iterations (i.e. "loop") through the individual characters of the currently selected text. The "if" code block performs a series of tests on the character to determine if the character is blank. "Blank" is defined as these nonprinting characters: Carriage return, tab, form feed, or space. If the currently tested character is none of those just mentioned, the function immediately reports back "False," meaning that the selection is not blank.

  22. Press Alt+F11 to return to the Word document and click on any of the pages with nonprinting characters.

  23. Press Alt+F8 to display the list of VBA macros, then double click the "deleteBlankPage" item to run the macro. Your program will select, then delete the page.

  24. Click on any page with visible characters and run the program again. This time the program will not delete the page.

Loading ...

About the Author

Darrin Koltow wrote about computer software until graphics programs reawakened his lifelong passion of becoming a master designer and draftsman. He has now committed to acquiring the training for a position designing characters, creatures and environments for video games, movies and other entertainment media.

Loading ...