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.
- Skill level:
Other People Are Reading
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"
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
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
isBlankSelection = False Exit Function
isBlankSelection = True
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.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for