Microsoft Word does not provide a quick click option to select and delete pages. Use this VBA procedure to select and delete a specific page.
The article How to use a VBA procedure that deletes current page in Word document? shows how to use a VBA procedure to get the current Microsoft Word page. It is helpful if you are on the page you want to delete. If you have to select the page before deleting it, you’ll need a little more code.
In this tutorial, I’ll show you a simple VBA procedure that asks for a page number from the user and then removes it. Then you don’t have to be on the page you’re deleting – you can be anywhere in the document.
I use Microsoft 365 on a Windows 10 64-bit system, but you can use older versions. Word for the web does not support VBA macros.
TO SEE: Windows, Linux, and Mac Commands Everyone Should Know (Free PDF) (TechRepublic)
How the VBA Macro Works in Word
The procedure in Code A declares and defines a few variables before prompting the user for a page number with an InputBox() statement. The code stores the user input value in the pge variable. The second Set statement selects the page identified by pge. The following 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 is a minimum of error handling. Should an unhandled error occur, the MsgBox() statement displays that error number and its description. For example, if the user enters something other than a number, the MsgBox() function will display the message
13: Type Mismatch†
'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")
Set rng = Nothing
MsgBox Err.Number & ": " & Err.Description
Set rng = Nothing
Now let’s enter the VBA procedure.
How to Enter VBA Procedure in Word
If you’re familiar with VBA, you can probably skip this part and go straight to the next section, where we’ll run the procedure to see how it works.
To open the procedure, press Alt + F11 to open the Visual Basic Editor. Select This Document in the Project Explorer on the left. If you enter the code manually, do not paste it from this web page. Instead, copy the code into a text editor and then paste that code into the ThisDocument module. Doing this will remove any ghost web characters that might otherwise cause errors.
If you are using a ribbon version, make sure to save the workbook as a macro file. If you are working in the menu version, you can skip this step. Now let’s do the procedure and see how it works.
How to carry out the procedure?
We use the procedure to delete a page in the five-page document shown in Image A† There are two noteworthy things to mention before we do this:
- This procedure does not delete the last page. It will delete the content, but it will not delete the page.
- If your page numbering scheme doesn’t show the actual page number – say you start page number on page 2 so that the page number in the header shows 1 instead of 2 – VBA will take this into account and remove the page with page 2, which would be page 3 by the actual count.
Each page shows a different font color and page number in the header. That is so that you can visually discern that the procedure is removing a page. Note that page 2, the page being deleted, is green.
Before running the procedure, click in any page other than 2. The macro deletes the current page without any problems, but I want you to see that a page other than the current page is also deleted – that is 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 Band click Run.
When the code displays the InputBox() then enter 2 as shown in Figure C and click OK.
As you can see in Figure Dthe green page, page 2, has disappeared and page three (purple) has moved to page 2.
Most likely, you don’t want to go through all those steps to run this macro. I recommend adding it to the Quick Access Toolbar or a custom group. If you need help with that, read Add Office macros to the QAT toolbar for quick access†