How to use VBA to select and delete a single page in Word

Estimated read time 5 min read


Microsoft Word doesn’t offer a quick click selection option for selecting and deleting pages. Use this VBA procedure to select a specific page and delete it.

Starting microsoft word application
Image: PixieMe/Adobe Stock

The article How to use a VBA procedure that deletes the current page in a Word document shows you how to use a VBA procedure to delete the current Microsoft Word page. It’s useful if you’re on the page you want to delete. When you need to select the page before deleting, you’ll need a bit more code.

In this tutorial, I’ll show you a simple VBA procedure that prompts a page number from the user and then deletes it. Subsequently, you don’t need to be on the page you delete — you can be anywhere in the document.

I’m using Microsoft 365 on a Windows 10 64-bit system, but you can use older versions. Word for the web doesn’t support VBA macros.

SEE: Windows, Linux, and Mac commands everyone needs to know (free PDF) (TechRepublic)

How the VBA macro works in Word

The procedure in Code A declares and defines a few variables before prompting for a page number from the user with an InputBox() statement. The code saves the user input value to the pge variable. The second Set statement selects the page identified by pge. The next statement uses a special bookmark to select the entire page. The last rng.Delete statement deletes the selected page. A final Set statement destroys the rng object.

There’s a minimum of error-handling. Should an unhandled error arise, the MsgBox() statement will display that error number and description. For instance, if the user enters anything other than a number, the MsgBox() function will display the message 13: Type Mismatch.

Code A

Sub FindDeletePage()

'Go to specific page and delete it.

'Can be undone with Ctrl + z.

Dim rng As Range

Dim pge As Integer

On Error GoTo Errhandler:

pge = InputBox("Please enter number of page you want to delete.", "Delete page")

Set rng = ActiveDocument.Range(0, 0)

Set rng = rng.GoTo(What:=wdGoToPage, Name:=pge)

Set rng = rng.GoTo(What:=wdGoToBookmark, Name:="\page")

rng.Delete

Set rng = Nothing

Exit Sub

Errhandler:

MsgBox Err.Number & ": " & Err.Description

Set rng = Nothing

End Sub

Now let’s enter the VBA procedure.

How to enter the VBA procedure in Word

If you’re familiar with VBA, you can probably skip this section and move right on to the next section, where we’ll run the procedure 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 ThisDocument. 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 ThisDocument module. Doing so will remove any phantom web characters that might otherwise cause errors.

If you’re 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 procedure

We’ll use the procedure to delete a page in the five-page document shown in Figure A. There are two note-worthy things to mention before we do so:

  1. This procedure won’t delete the last page. It will delete the content, but it won’t delete the page.
  2. If your page numbering scheme doesn’t reflect the actual page number — suppose you start page number on page 2 so that the page number in the header displays 1 rather than 2 — VBA will account for this and delete the page displaying page 2, which would be page 3 by actual count.

Figure A

We’ll delete one of the pages in this document.

Each page displays a different font color and a page number in the header. That’s so you can visually discern that the procedure deletes a page. Note that page 2, the page will delete, is green.

Before running the procedure, click inside any page other than 2. The macro will delete the current page with no problem, but I want you to see that it will also delete a page other than the current page — that’s the focus of this procedure.

Click the Developer tab and then click Macros in the Code group. In the resulting dialog box, select FindDeletePage(), as shown in Figure B, and click Run.

Figure B

Choose the procedure FindDeletePage().

When the code displays the InputBox(), enter 2, as shown in Figure C and click OK.

Figure C

Enter 2 to delete the page 2.

Figure D

The procedure deleted page 2.

As you can see in Figure D, the green page, page 2, is gone and page three (purple) has moved up to page 2.

Most likely, you won’t want to work through all those steps to run this macro. I recommend that you add it to the Quick Access Toolbar or a custom group. If you need help with that, read How to add Office macros to the QAT toolbar for quick access.



Source link

You May Also Like

More From Author