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

In case you work with lengthy Microsoft Phrase paperwork and ceaselessly should delete pages, you may recognize this straightforward VBA process.

Microsoft Word logo

Picture: BigTunaOnline/Shutterstock

Deleting a web page in a Microsoft Phrase doc is not troublesome, however there isn’t any quick-click route. You will need to delete the content material and the comfortable or exhausting web page break. It’s kind of awkward. You most likely do not thoughts doing so sometimes, but when you must do that so much, you would possibly discover doing so tedious. On this article, I will present you two VBA procedures that can delete the present web page. As well as, I will present you easy methods to assign a keyboard shortcut to the process so you’ll be able to execute it shortly. Even should you do not want the process, you’ll be able to learn to create a keyboard shortcut. 

SEE: Home windows 10: Lists of vocal instructions for speech recognition and dictation (free PDF) (TechRepublic)

I am utilizing Microsoft 365 on a Home windows 10 64-bit system, however you should utilize earlier variations. Phrase On-line does not assist VBA. To your comfort, you’ll be able to obtain the demonstration .docm, .doc and .cls information.

Find out how to use the VBA process

You could be stunned how quick the process wanted to delete the present web page is. It is just one line, due to a system-defined bookmark, Web page, which refers back to the present web page. Itemizing A reveals the easy process. (You may usually see the phrases macro and sub process used interchangeable, and that is OK.)

Itemizing A

Sub DeleteCurrentPage()

    ‘Run process to delete the present web page.

    ‘Keyboard shortcut Alt + d.

    ActiveDocument.Bookmarks(“Web page”).Vary.Delete

Finish Sub

As talked about, the Web page bookmark is a particular reference to the present web page. The Vary property specifies the web page and the Delete technique deletes it. In case you omit the Vary property, the assertion returns an error as a result of VBA makes an attempt to delete the Web page bookmark; you’ll be able to’t delete it or modify it as a result of it is read-only. To delete a bookmark, you’ll reference it by title and omit the Vary property.

The process has two limitations:

  • It will not delete contiguous pages. If you choose a number of contiguous pages, the process deletes the primary web page within the grouping.
  • It will not delete the final web page. It is going to delete the contents of the final web page, nevertheless it will not delete the web page, so you will not need to run this with the final web page chosen.

Find out how to add the process

In case you’re conversant in VBA, you’ll be able to most likely skip this part and transfer proper on to working the macro to see the way it works.

To enter the process, press Alt + F11 to open the Visible Fundamental Editor. Within the Venture Explorer to the left, choose ThisWorkbook so you’ll be able to run the process in any sheet. You may enter the code manually or import the downloadable .cls file. As well as, the macro is within the downloadable .docm and .doc information. In case you enter the code manually, do not paste from this net web page. As a substitute, copy the code right into a textual content editor after which paste that code into the ThisWorkbook module. Doing so will take away any phantom net characters which may in any other case trigger errors.

If you’re utilizing a ribbon model, make sure to save the workbook as a macro-enabled file. In case 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 VBA process to delete pages in Phrase

You need to use the Developer tab to execute the process, nevertheless it takes a number of clicks. As a substitute, let’s assign a keyboard shortcut to run it:

  1. Click on the Fast Entry Toolbar dropdown and select Extra Instructions.
  2. Within the left pane, click on Customise Ribbon.
  3. Click on the Customise button (Determine A) to create the shortcut.
  4. Within the ensuing dialog, select Macros from the Classes checklist (it is close to the underside of the checklist).
  5. From the Save Adjustments In dropdown, select the energetic doc.
  6. Click on contained in the Press New Shortcut Key management and maintain down the Alt key whilst you press the d key, for a shortcut mixture of Alt + D (Determine B). Earlier than you click on Assign, all the time test At the moment Assigned To manage. On this case, the mixture is not in use. When the mixture is in use, this management will warn you; you may should resolve whether or not to make use of a special mixture or to put in writing over the present project.
  7. Click on Assign, click on Shut, after which click on OK to return to the doc.

Determine A

wordvbadeletepage-a.jpg

  Entry the DeleteCurrentPage process so you’ll be able to assign a keyboard shortcut to it.

Determine B

wordvbadeletepage-b.jpg

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

Now that you’ve fast entry to the process, let’s run it. The demonstration file has 5 pages, as you’ll be able to see in Determine C. The header shows web page numbers, which is able to replace accordingly whenever you delete a web page. The bigger quantity is precise textual content, so it is visually obvious that you just actually did delete a web page. Click on on any web page however the final one and press Alt + D. As you’ll be able to see in Determine D, I deleted web page 3.

Determine C

wordvbadeletepage-c.jpg

   Let’s have a look at what occurs to each units of numbers once we run the process.

Determine D

wordvbadeletepage-d.jpg

  Delete a web page; the numbers will point out the web page you deleted.

The process in Itemizing A will not delete the final web page, however it would delete any content material on the final web page. In case you do not need to should keep in mind that quirk, you should utilize Itemizing B as an alternative. The 2 integer variables retailer the present web page quantity and the final web page quantity. After they’re not equal, the present web page is deleted. After they’re equal, nothing occurs.

Itemizing B

Sub DeleteCurrentPage()

    ‘Run process to delete the present web page.

    ‘Keyboard shortcut Alt + d.

    Dim cur As Integer

    Dim final As Integer

    cur = Choice.Vary.Data(wdActiveEndPageNumber)

   final = Choice.Vary.Data( _

         wdNumberOfPagesInDocument)

    ‘Compares the present web page quantity and the final web page quantity.

    ‘After they aren’t equal, delete the present web page.

    If cur <> final Then

        ActiveDocument.Bookmarks(“Web page”).Vary.Delete

    Finish If

Finish Sub

Each procedures are easy and doubtless have a restricted viewers. Nevertheless, should you ceaselessly delete pages, both can be helpful. 

Additionally see

Recent Articles

spot_img

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox