Microsoft Phrase doesn’t provide a fast click on choice possibility for choosing and deleting pages. Use this VBA process to pick a particular web page and delete it.
The article How you can use a VBA process that deletes the present web page in a Phrase doc reveals you how you can use a VBA process to delete the present Microsoft Phrase web page. It’s helpful if you happen to’re on the web page you wish to delete. When you must choose the web page earlier than deleting, you’ll want a bit extra code.
On this tutorial, I’ll present you a easy VBA process that prompts a web page quantity from the consumer after which deletes it. Subsequently, you don’t should be on the web page you delete — you will be wherever within the doc.
I’m utilizing Microsoft 365 on a Home windows 10 64-bit system, however you need to use older variations. Phrase for the online doesn’t assist VBA macros.
SEE: Home windows, Linux, and Mac instructions everybody must know (free PDF) (TechRepublic)
How the VBA macro works in Phrase
The process in Code A declares and defines a couple of variables earlier than prompting for a web page quantity from the consumer with an InputBox() assertion. The code saves the consumer enter worth to the pge variable. The second Set assertion selects the web page recognized by pge. The subsequent assertion makes use of a particular bookmark to pick your entire web page. The final rng.Delete assertion deletes the chosen web page. A remaining Set assertion destroys the rng object.
There’s a minimal of error-handling. Ought to an unhandled error come up, the MsgBox() assertion will show that error quantity and outline. For example, if the consumer enters something apart from a quantity, the MsgBox() operate will show the message
13: Kind Mismatch.
'Go to particular web page and delete it.
'Could be undone with Ctrl + z.
Dim rng As Vary
Dim pge As Integer
On Error GoTo Errhandler:
pge = InputBox("Please enter variety of web page you wish to delete.", "Delete web page")
Set rng = ActiveDocument.Vary(0, 0)
Set rng = rng.GoTo(What:=wdGoToPage, Title:=pge)
Set rng = rng.GoTo(What:=wdGoToBookmark, Title:="web page")
Set rng = Nothing
MsgBox Err.Quantity & ": " & Err.Description
Set rng = Nothing
Now let’s enter the VBA process.
How you can enter the VBA process in Phrase
If you happen to’re acquainted with VBA, you may in all probability skip this part and transfer proper on to the following part, the place we’ll run the process to see the way it works.
To enter the process, press Alt + F11 to open the Visible Primary Editor. Within the Undertaking Explorer to the left, choose ThisDocument. If you happen to enter the code manually, don’t paste from this net web page. As an alternative, copy the code right into a textual content editor after which paste that code into the ThisDocument module. Doing so will take away any phantom net characters which may in any other case trigger errors.
If you happen to’re utilizing a ribbon model, you’ll want to save the workbook as a macro-enabled file. If you happen to’re working within the menu model, you may skip this step. Now, let’s run the process and see the way it works.
How you can run the process
We’ll use the process to delete a web page within the five-page doc proven in Determine A. There are two note-worthy issues to say earlier than we accomplish that:
- This process gained’t delete the final web page. It’s going to delete the content material, however it gained’t delete the web page.
- In case your web page numbering scheme doesn’t replicate the precise web page quantity — suppose you begin web page quantity on web page 2 in order that the web page quantity within the header shows 1 relatively than 2 — VBA will account for this and delete the web page displaying web page 2, which might be web page 3 by precise depend.
Every web page shows a distinct font coloration and a web page quantity within the header. That’s so you may visually discern that the process deletes a web page. Observe that web page 2, the web page will delete, is inexperienced.
Earlier than operating the process, click on inside any web page apart from 2. The macro will delete the present web page with no downside, however I would like you to see that it’ll additionally delete a web page apart from the present web page — that’s the main target of this process.
Click on the Developer tab after which click on Macros within the Code group. Within the ensuing dialog field, choose FindDeletePage(), as proven in Determine B, and click on Run.
When the code shows the InputBox(), enter 2, as proven in Determine C and click on OK.
As you may see in Determine D, the inexperienced web page, web page 2, is gone and web page three (purple) has moved as much as web page 2.
Almost certainly, you gained’t wish to work by all these steps to run this macro. I like to recommend that you just add it to the Fast Entry Toolbar or a customized group. If you happen to need assistance with that, learn How you can add Workplace macros to the QAT toolbar for fast entry.