Search

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

tederes.blogspot.com

If you work with long Microsoft Word documents and frequently have to delete pages, you'll appreciate this simple VBA procedure.

Microsoft Word logo

Image: BigTunaOnline/Shutterstock

More about Windows

Deleting a page in a Microsoft Word document isn't difficult, but there's no quick-click route. You must delete the content and the soft or hard page break. It's a bit awkward. You probably don't mind doing so occasionally, but if you have to do this a lot, you might find doing so tedious. In this article, I'll show you two VBA procedures that will delete the current page. In addition, I'll show you how to assign a keyboard shortcut to the procedure so you can execute it quickly. Even if you don't need the procedure, you can learn how to create a keyboard shortcut. 

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

I'm using Microsoft 365 on a Windows 10 64-bit system, but you can use earlier versions. Word Online doesn't support VBA. For your convenience, you can download the demonstration .docm, .doc and .cls files.

How to use the VBA procedure

You might be surprised how short the procedure needed to delete the current page is. It's only one line, thanks to a system-defined bookmark, \Page, which refers to the current page. Listing A shows the simple procedure. (You'll often see the terms macro and sub procedure used interchangeable, and that's OK.)

Listing A

Sub DeleteCurrentPage()

    'Run procedure to delete the current page.

    'Keyboard shortcut Alt + d.

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

End Sub

As mentioned, the \Page bookmark is a special reference to the current page. The Range property specifies the page and the Delete method deletes it. If you omit the Range property, the statement returns an error because VBA attempts to delete the \Page bookmark; you can't delete it or modify it because it's read-only. To delete a bookmark, you would reference it by name and omit the Range property.

The procedure has two limitations:

  • It won't delete contiguous pages. If you select multiple contiguous pages, the procedure deletes the first page in the grouping.
  • It won't delete the last page. It will delete the contents of the last page, but it won't delete the page, so you won't want to run this with the last page selected.

How to add the procedure

If you're familiar with VBA, you can probably skip this section and move right on to running the macro to see how it works.

To enter the procedure, press Alt + F11 to open the Visual Basic Editor. In the Project Explorer to the left, select ThisWorkbook so you can run the procedure in any sheet. You can enter the code manually or import the downloadable .cls file. In addition, the macro is in the downloadable .docm and .doc files. If you enter the code manually, don't paste from this web page. Instead, copy the code into a text editor and then paste that code into the ThisWorkbook module. Doing so will remove any phantom web characters that might otherwise cause errors.

If you are using a ribbon version, be sure to save the workbook as a macro-enabled file. If you're working in the menu version, you can skip this step. Now, let's run the procedure and see how it works.

How to run the VBA procedure to delete pages in Word

You can use the Developer tab to execute the procedure, but it takes several clicks. Instead, let's assign a keyboard shortcut to run it:

  1. Click the Quick Access Toolbar dropdown and choose More Commands.
  2. In the left pane, click Customize Ribbon.
  3. Click the Customize button (Figure A) to create the shortcut.
  4. In the resulting dialog, choose Macros from the Categories list (it's near the bottom of the list).
  5. From the Save Changes In dropdown, choose the active document.
  6. Click inside the Press New Shortcut Key control and hold down the Alt key while you press the d key, for a shortcut combination of Alt + D (Figure B). Before you click Assign, always check Currently Assigned To control. In this case, the combination isn't in use. When the combination is in use, this control will alert you; you'll have to decide whether to use a different combination or to write over the current assignment.
  7. Click Assign, click Close, and then click OK to return to the document.

Figure A

wordvbadeletepage-a.jpg

  Access the DeleteCurrentPage procedure so you can assign a keyboard shortcut to it.

Figure B

wordvbadeletepage-b.jpg

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

Now that you have quick access to the procedure, let's run it. The demonstration file has five pages, as you can see in Figure C. The header displays page numbers, which will update accordingly when you delete a page. The larger number is actual text, so it's visually apparent that you really did delete a page. Click on any page but the last one and press Alt + D. As you can see in Figure D, I deleted page 3.

Figure C

wordvbadeletepage-c.jpg

   Let's see what happens to both sets of numbers when we run the procedure.

Figure D

wordvbadeletepage-d.jpg

  Delete a page; the numbers will indicate the page you deleted.

The procedure in Listing A won't delete the last page, but it will delete any content on the last page. If you don't want to have to remember that quirk, you can use Listing B instead. The two integer variables store the current page number and the last page number. When they're not equal, the current page is deleted. When they're equal, nothing happens.

Listing B

Sub DeleteCurrentPage()

    'Run procedure to delete the current page.

    'Keyboard shortcut Alt + d.

    Dim cur As Integer

    Dim last As Integer

    cur = Selection.Range.Information(wdActiveEndPageNumber)

   last = Selection.Range.Information( _

         wdNumberOfPagesInDocument)

    'Compares the current page number and the last page number.

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

    If cur <> last Then

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

    End If

End Sub

Both procedures are simple and probably have a limited audience. However, if you frequently delete pages, either will be useful. 

Also see

Adblock test (Why?)



"current" - Google News
September 21, 2021 at 03:14AM
https://ift.tt/2XA7IIt

How to use a VBA procedure that deletes the current page in a Word document - TechRepublic
"current" - Google News
https://ift.tt/3b2HZto
https://ift.tt/3c3RoCk

Bagikan Berita Ini

0 Response to "How to use a VBA procedure that deletes the current page in a Word document - TechRepublic"

Post a Comment


Powered by Blogger.