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

3 years ago 314

If you enactment with agelong Microsoft Word documents and often person to delete pages, you'll admit this elemental VBA procedure.

Microsoft Word logo

Image: BigTunaOnline/Shutterstock

Deleting a leafage successful a Microsoft Word papers isn't difficult, but there's nary quick-click route. You indispensable delete the contented and the brushed oregon hard leafage break. It's a spot awkward. You astir apt don't caput doing truthful occasionally, but if you person to bash this a lot, you mightiness find doing truthful tedious. In this article, I'll amusement you 2 VBA procedures that volition delete the existent page. In addition, I'll amusement you however to delegate a keyboard shortcut to the process truthful you tin execute it quickly. Even if you don't request the procedure, you tin larn however to make a keyboard shortcut. 

SEE: Windows 10: Lists of vocal commands for code designation and dictation (free PDF) (TechRepublic)

I'm utilizing Microsoft 365 connected a Windows 10 64-bit system, but you tin usage earlier versions. Word Online doesn't enactment VBA. For your convenience, you tin download the objection .docm, .doc and .cls files.

How to usage the VBA procedure

You mightiness beryllium amazed however abbreviated the process needed to delete the existent leafage is. It's lone 1 line, acknowledgment to a system-defined bookmark, \Page, which refers to the existent page. Listing A shows the elemental procedure. (You'll often spot the presumption macro and sub process utilized interchangeable, and that's OK.)

Listing A

Sub DeleteCurrentPage()

    'Run process to delete the existent page.

    'Keyboard shortcut Alt + d.

    ActiveDocument.Bookmarks("\Page").Range.Delete

End Sub

As mentioned, the \Page bookmark is simply a peculiar notation to the existent page. The Range spot specifies the leafage and the Delete method deletes it. If you omit the Range property, the connection returns an mistake due to the fact that VBA attempts to delete the \Page bookmark; you can't delete it oregon modify it due to the fact that it's read-only. To delete a bookmark, you would notation it by sanction and omit the Range property.

The process has 2 limitations:

  • It won't delete contiguous pages. If you prime aggregate contiguous pages, the process deletes the archetypal leafage successful the grouping.
  • It won't delete the past page. It volition delete the contents of the past page, but it won't delete the page, truthful you won't privation to tally this with the past leafage selected.

How to adhd the procedure

If you're acquainted with VBA, you tin astir apt skip this conception and determination close connected to moving the macro to spot however it works.

To participate the procedure, property Alt + F11 to unfastened the Visual Basic Editor. In the Project Explorer to the left, prime ThisWorkbook truthful you tin tally the process successful immoderate sheet. You tin participate the codification manually oregon import the downloadable .cls file. In addition, the macro is successful the downloadable .docm and .doc files. If you participate the codification manually, don't paste from this web page. Instead, transcript the codification into a substance exertion and past paste that codification into the ThisWorkbook module. Doing truthful volition region immoderate phantom web characters that mightiness different origin errors.

If you are utilizing a ribbon version, beryllium definite to prevention the workbook arsenic a macro-enabled file. If you're moving successful the paper version, you tin skip this step. Now, let's tally the process and spot however it works.

How to tally the VBA process to delete pages successful Word

You tin usage the Developer tab to execute the procedure, but it takes respective clicks. Instead, let's delegate a keyboard shortcut to tally it:

  1. Click the Quick Access Toolbar dropdown and take More Commands.
  2. In the near pane, click Customize Ribbon.
  3. Click the Customize fastener (Figure A) to make the shortcut.
  4. In the resulting dialog, take Macros from the Categories database (it's adjacent the bottommost of the list).
  5. From the Save Changes In dropdown, take the progressive document.
  6. Click wrong the Press New Shortcut Key power and clasp down the Alt key portion you property the d key, for a shortcut operation of Alt + D (Figure B). Before you click Assign, ever cheque Currently Assigned To control. In this case, the operation isn't successful use. When the operation is successful use, this power volition alert you; you'll person to determine whether to usage a antithetic operation oregon to constitute implicit the existent assignment.
  7. Click Assign, click Close, and past click OK to instrumentality to the document.

Figure A

wordvbadeletepage-a.jpg

  Access the DeleteCurrentPage process truthful you tin delegate a keyboard shortcut to it.

Figure B

wordvbadeletepage-b.jpg

   Press the Alt cardinal and the D cardinal to make the shortcut.

Now that you person speedy entree to the procedure, let's tally it. The objection record has 5 pages, arsenic you tin spot successful Figure C. The header displays leafage numbers, which volition update accordingly erstwhile you delete a page. The larger fig is existent text, truthful it's visually evident that you truly did delete a page. Click connected immoderate leafage but the past 1 and property Alt + D. As you tin spot successful Figure D, I deleted leafage 3.

Figure C

wordvbadeletepage-c.jpg

   Let's spot what happens to some sets of numbers erstwhile we tally the procedure.

Figure D

wordvbadeletepage-d.jpg

  Delete a page; the numbers volition bespeak the leafage you deleted.

The process successful Listing A won't delete the past page, but it volition delete immoderate contented connected the past page. If you don't privation to person to retrieve that quirk, you tin usage Listing B instead. The 2 integer variables store the existent leafage fig and the past leafage number. When they're not equal, the existent leafage is deleted. When they're equal, thing happens.

Listing B

Sub DeleteCurrentPage()

    'Run process to delete the existent page.

    'Keyboard shortcut Alt + d.

    Dim cur As Integer

    Dim past As Integer

    cur = Selection.Range.Information(wdActiveEndPageNumber)

   last = Selection.Range.Information( _

         wdNumberOfPagesInDocument)

    'Compares the existent leafage fig and the past leafage number.

    'When they aren't equal, delete the existent page.

    If cur <> past Then

        ActiveDocument.Bookmarks("\Page").Range.Delete

    End If

End Sub

Both procedures are elemental and astir apt person a constricted audience. However, if you often delete pages, either volition beryllium useful. 

Microsoft Weekly Newsletter

Be your company's Microsoft insider by speechmaking these Windows and Office tips, tricks, and cheat sheets. Delivered Mondays and Wednesdays

Sign up today

Also see

Read Entire Article