We Value Your Privacy

We and our partners use technology such as cookies on our site to personalise content and ads, provide social media features, and analyse our traffic. Click below to consent to the use of this technology across the web. You can change your mind and change your consent choices at anytime by returning to this site.

How to Remove a Blank Page in Word in VBA

Darrin Koltow 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 ...

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.

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.

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

Public Sub deleteBlankPage()

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

End Sub

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.

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

If isBlankSelection Then

Selection.Delete

End If

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.

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

Public Function isBlankSelection()

For Each c In Selection.Characters

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

End If

Next

isBlankSelection = True

End Function

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.

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

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.

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

Loading ...

References

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.