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

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

Image: BigTunaOnline / Shutterstock

Deleting a page in a Microsoft Word document isn’t difficult, but there is no quick click route. You need to delete the content and the soft or hard pagination. It’s a little awkward. You probably don’t mind doing this occasionally, but if you have to do this frequently it can become a chore. In this article, I will show you two VBA procedures that will clear the current page. I will also show you how to assign a keyboard shortcut to the procedure so that you can do it quickly. Even if you don’t need the procedure, you can still learn how to create a keyboard shortcut.

SEE: Windows 10: Lists of Voice Commands for Speech Recognition and Dictation (Free PDF) (TechRepublic)

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

How to use the VBA method

You will be surprised how quick it is to delete the current page. It’s only one line, thanks to a system-defined bookmark, Page, which points to the current page. Entry A shows the simple procedure. (You will often see that the terms macro and sub-procedure are used interchangeably, and that’s fine.)

Entry A

Under DeleteCurrentPage ()

‘Perform the procedure to delete the current page.

‘Keyboard shortcut Alt + d.

ActiveDocument.Bookmarks (” Page”). Range.Delete

End Sub

As mentioned earlier, 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 will return an error because VBA is trying to delete the Page bookmark. You cannot delete or change it as it is read-only. To delete a bookmark, you would reference it by name and omit the Range property.

The procedure has two limitations:

  • No contiguous pages are deleted. If you select multiple contiguous pages, the procedure deletes the first page in the grouping.
  • The last page is not deleted. It will erase the contents of the last page, but it will not erase the page so you don’t want to do this on the last page selected.

How to add the procedure

If you are familiar with VBA, you can probably skip this section and go straight 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 on the left, select This Workbook so that you can perform the procedure on any worksheet. You can enter the code manually or import the downloadable .cls file. The macro is also in the downloadable .docm and .doc files. If you enter the code manually, do not paste it from this webpage. Instead, copy the code into a text editor and then paste that code into the ThisWorkbook module. This will remove any phantom web characters that might otherwise cause errors.

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

How to do the VBA procedure to delete pages in Word

You can complete the procedure from the Developer tab, but it requires multiple clicks. Instead, let’s assign a keyboard shortcut to run it:

  1. Click the Quick Access Toolbar drop-down menu and select More Commands.
  2. In the left pane, click Customize the Ribbon.
  3. Click the Customize button (Figure A.) to create the shortcut.
  4. In the dialog box that appears, choose Macros from the Categories list (at the bottom of the list).
  5. Select the active document from the Save changes in drop-down list.
  6. Click inside the Press New Shortcut Key control and hold down the Alt key while pressing the D key to select a keyboard shortcut of Alt + D (Figure B.). Before you click Assign, always activate the Currently assigned to control. In this case the combination is not used. If the combination is in use, this control will warn you; You have to decide whether to use a different combination or to overwrite the current assignment.
  7. Click Assign, click Close, and then click OK to return to the document.

Figure A.

wordvbadeletepage-a.jpg

Access the DeleteCurrentPage procedure so that you can assign a keyboard shortcut to it.

Figure B.

wordvbadeletepage-b.jpg

Press the Alt key and the D key to create the shortcut.

Now that you have quick access to the procedure, let’s run it. The demo file has five pages as you can see in Figure C.. Page numbers are displayed in the header, which are updated accordingly when a page is deleted. The larger number is actual text, so it’s visually obvious that you’ve actually deleted a page. Click on any page except the last one and press Alt + D. As you can see in Figure 4 Figure D., I deleted page 3.

Figure C.

wordvbadeletepage-c.jpg

Let’s see what happens to both sets of numbers when we perform the procedure.

Figure D.

wordvbadeletepage-d.jpg

Deleting a page; the numbers indicate the deleted page.

The procedure in Entry A does not delete the last page, but does delete all content on the last page. If you don’t want to remember this quirk, you can Entry B instead of this. The two integer variables store the current page number and the last page number. If they are not the same, the current page will be deleted. If they are the same, nothing happens.

Entry B

Under DeleteCurrentPage ()

‘Perform the procedure to delete the current page.

‘Keyboard shortcut Alt + d.

Dim cur As Integer

Dim last as an integer

cur = Selection.Range.Information (wdActiveEndPageNumber)

last = selection.area.Information (_

wdNumberOfPagesInDocument)

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

‘If they are not the same, delete the current page.

If Cur <> last Then

ActiveDocument.Bookmarks (” Page”). Range.Delete

End if

End Sub

Both procedures are straightforward and likely to have a limited audience. However, if you delete pages frequently, both of these are useful.

Microsoft weekly newsletter

Be your company’s Microsoft Insider by reading these tips, tricks, and cheat sheets for Windows and Office. Delivery on Mondays and Wednesdays

Register today

See also

Comments are closed.