How to use a VBA procedure that deletes the current page in a Word document

If you work with long Microsoft Word documents and frequently have to delete pages, you'll appreciate this simple VBA procedure.

Must-read Windows coverage

Deleting a page in a Microsoft Word document isn’t difficult, but there’s no quick-click route. You must delete the content and the soft or hard page break. It’s a bit awkward. You probably don’t mind doing so occasionally, but if you have to do this a lot, you might find doing so tedious. In this article, I’ll show you two VBA procedures that will delete the current page. In addition, I’ll show you how to assign a keyboard shortcut to the procedure so you can execute it quickly. Even if you don’t need the procedure, you can learn how to create a keyboard shortcut.

I’m using Microsoft 365 on a Windows 10 64-bit system, but you can use earlier versions. Word Online doesn’t support VBA. For your convenience, you can download the demonstration .docm, .doc and .cls files.

How to use the VBA procedure

You might be surprised how short the procedure needed to delete the current page is. It’s only one line, thanks to a system-defined bookmark, Page, which refers to the current page. Listing A shows the simple procedure. (You’ll often see the terms macro and sub procedure used interchangeable, and that’s OK.)

Listing A

‘Run procedure to delete the current page.

Keyboard shortcut Alt + d.

As mentioned, the Page bookmark is a special reference to the current page. The Range property specifies the page and the Delete method deletes it. If you omit the Range property, the statement returns an error because VBA attempts to delete the Page bookmark; you can’t delete it or modify it because it’s read-only. To delete a bookmark, you would reference it by name and omit the Range property.

The procedure has two limitations:

How to add the procedure

If you’re familiar with VBA, you can probably skip this section and move right on to running the macro to see how it works.

To enter the procedure, press Alt + F11 to open the Visual Basic Editor. In the Project Explorer to the left, select ThisWorkbook so you can run the procedure in any sheet. You can enter the code manually or import the downloadable .cls file. In addition, the macro is in the downloadable .docm and .doc files. If you enter the code manually, don’t paste from this web page. Instead, copy the code into a text editor and then paste that code into the ThisWorkbook module. Doing so will remove any phantom web characters that might otherwise cause errors.

If you are using a ribbon version, be sure to save the workbook as a macro-enabled file. If you’re working in the menu version, you can skip this step. Now, let’s run the procedure and see how it works.

How to run the VBA procedure to delete pages in Word

You can use the Developer tab to execute the procedure, but it takes several clicks. Instead, let’s assign a keyboard shortcut to run it:

  1. Click the Quick Access Toolbar dropdown and choose More Commands.
  2. In the left pane, click Customize Ribbon.
  3. Click the Customize button (Figure A) to create the shortcut.
  4. In the resulting dialog, choose Macros from the Categories list (it’s near the bottom of the list).
  5. From the Save Changes In dropdown, choose the active document.
  6. Click inside the Press New Shortcut Key control and hold down the Alt key while you press the d key, for a shortcut combination of Alt + D (Figure B). Before you click Assign, always check Currently Assigned To control. In this case, the combination isn’t in use. When the combination is in use, this control will alert you; you’ll have to decide whether to use a different combination or to write over the current assignment.
  7. Click Assign, click Close, and then click OK to return to the document.

Figure A

Figure B

Now that you have quick access to the procedure, let’s run it. The demonstration file has five pages, as you can see in Figure C. The header displays page numbers, which will update accordingly when you delete a page. The larger number is actual text, so it’s visually apparent that you really did delete a page. Click on any page but the last one and press Alt + D. As you can see in Figure D, I deleted page 3.

Figure C

Figure D

The procedure in Listing A won’t delete the last page, but it will delete any content on the last page. If you don’t want to have to remember that quirk, you can use Listing B instead. The two integer variables store the current page number and the last page number. When they’re not equal, the current page is deleted. When they’re equal, nothing happens.

Listing B

‘Run procedure to delete the current page.

Keyboard shortcut Alt + d.

Dim cur As Integer

Dim last As Integer

last = Selection.Range.Information( _

‘Compares the current page number and the last page number.

‘When they aren’t equal, delete the current page.

If cur <> last Then

Both procedures are simple and probably have a limited audience. However, if you frequently delete pages, either will be useful.

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays

Share Article

Account Information

TechRepublic close modal

Share with Your Friends

How to use a VBA procedure that deletes the current page in a Word document

Also Read

Susan Harkins

Susan Sales Harkins is an IT consultant, specializing in desktop solutions. Previously, she was editor in chief for The Cobb Group, the world's largest publisher of technical journals.