[ad_1]
Microsoft Phrase doesn’t supply a fast click on choice possibility for choosing and deleting pages. Use this VBA process to pick a selected web page and delete it.
The article How to use a VBA procedure that deletes the current page in a Word document exhibits you how you can use a VBA process to delete the present Microsoft Word web page. It’s helpful in case you’re on the web page you wish to delete. When you could 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 person after which deletes it. Subsequently, you don’t have to 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 should utilize older variations. Phrase for the online doesn’t assist VBA macros.
SEE: Windows, Linux, and Mac commands everyone needs to know (free PDF) (TechRepublic)
How the VBA macro works in Phrase
The process in Code A declares and defines just a few variables earlier than prompting for a web page quantity from the person with an InputBox() assertion. The code saves the person enter worth to the pge variable. The second Set assertion selects the web page recognized by pge. The following assertion makes use of a particular bookmark to pick your complete web page. The final rng.Delete assertion deletes the chosen web page. A last 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. As an example, if the person enters something aside from a quantity, the MsgBox() perform will show the message 13: Sort Mismatch
.
Code A
Sub FindDeletePage()
'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, Identify:=pge)
Set rng = rng.GoTo(What:=wdGoToBookmark, Identify:="web page")
rng.Delete
Set rng = Nothing
Exit Sub
Errhandler:
MsgBox Err.Quantity & ": " & Err.Description
Set rng = Nothing
Finish Sub
Now let’s enter the VBA process.
Find out how to enter the VBA process in Phrase
Should you’re conversant in VBA, you’ll be able to in all probability skip this part and transfer proper on to the subsequent 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 Fundamental Editor. Within the Undertaking Explorer to the left, choose ThisDocument. Should you enter the code manually, don’t paste from this internet 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 internet characters that may in any other case trigger errors.
Should you’re utilizing a ribbon model, be sure you save the workbook as a macro-enabled file. Should you’re working within the menu model, you’ll be able to skip this step. Now, let’s run the process and see the way it works.
Find out how to 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 should 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 quite 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.
Determine A
Every web page shows a special font coloration and a web page quantity within the header. That’s so you’ll be able to visually discern that the process deletes a web page. Be aware that web page 2, the web page will delete, is inexperienced.
Earlier than working the process, click on inside any web page aside from 2. The macro will delete the present web page with no drawback, however I would like you to see that it’ll additionally delete a web page aside 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.
Determine B
When the code shows the InputBox(), enter 2, as proven in Determine C and click on OK.
Determine C
Determine D
As you’ll be able to 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.
Most probably, 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. Should you need assistance with that, learn How to add Office macros to the QAT toolbar for quick access.
[ad_2]
Source link