title | keywords | f1_keywords | ms.prod | api_name | ms.assetid | ms.date | ms.localizationpriority |
---|---|---|---|---|---|---|---|
Range.Delete method (Word) |
vbawd10.chm157155455 |
vbawd10.chm157155455 |
word |
Word.Range.Delete |
066b6dda-db9c-43aa-b65c-556b06b5b445 |
06/08/2017 |
medium |
Range.Delete method (Word)
Deletes the specified number of characters or words.
Syntax
expression.Delete([Unit], [Count])
expression Required. A variable that represents a Range object.
Parameters
Name | Required/Optional | Data type | Description |
---|---|---|---|
Unit | Optional | Variant | The unit by which the collapsed range is to be deleted. Can be one of the WdUnits constants. |
Count | Optional | Variant | The number of units to be deleted. To delete units after the range, collapse the range and use a positive number. To delete units before the range, collapse the range and use a negative number. |
Return value
Long
Remarks
This method returns a Long value that indicates the number of items deleted, or it returns 0 (zero) if the deletion was unsuccessful.
Example
This example selects and deletes the contents of the active document.
Sub DeleteSelection() Dim intResponse As Integer intResponse = MsgBox("Are you sure you want to " & _ "delete the contents of the document?", vbYesNo) If intResponse = vbYes Then ActiveDocument.Content.Select Selection.Delete End If End Sub
See also
Range Object
[!includeSupport and feedback]
- Remove From My Forums
-
Question
-
I am trying to code a macro that will select all the text/pages from page 2 to the end of the document, and delete it, leaving the cursor in the home position on pg 2. I have this code which will delete only page 2.
I am stuck, please help!
‘Selection.GoTo wdGoToPage, wdGoToAbsolute, 2
‘ActiveDocument.Bookmarks(«page»).Range.DeleteI works perfectly, but only clears the 2nd page. I need it to be able to clear all pages from page 2 to the
end of the document.Thanks, Sean
Answers
-
Hi,
Based on my understanding, you want to clear all pages from specified page index to last page of the document. You can use the
Range.End to set the end of the region.
Here is a sample code for you reference:
Sub Clearpages() Dim rgePages As Range Dim PageCount As Integer PageCount = ActiveDocument.ComputeStatistics(wdStatisticPages) Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=2 Set rgePages = Selection.Range Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=PageCount rgePages.End = Selection.Bookmarks("Page").Range.End rgePages.Delete End Sub
Regards,
Marvin
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click
HERE to participate the survey.-
Proposed as answer by
Saturday, April 12, 2014 3:14 PM
-
Marked as answer by
glyder50
Monday, April 14, 2014 1:06 PM
-
Proposed as answer by
-
Thank you Marvin! It works, exactly as I need it too! Much appreciated.
SEan
-
Marked as answer by
glyder50
Monday, April 14, 2014 1:07 PM
-
Marked as answer by
2006 г. Word и его объекты
|
Delete
Method deletes specific range text which is under selection. In this article we will try to delete selection using code.
Syntax
Selection.Delete(unit, count)
- Unit: An optional parameter, The unit by which the collapsed selection is to be deleted. Can be one of the WdUnits constants as wdItem, wdLine, wdParagraph etc.
- Count: An optional variable, determines number of units to be deleted from selected range.
Example
Consider following line in a word document where we will delete first three characters from the selection:
Code example
Sub DeleteSelection() 'bind selection Dim oSelection As Selection Set oSelection = Selection 'Perform delete first 3 chars Dim oResult As Long oResult = oSelection.Delete(unit:=wdCharacter, Count:=3) MsgBox "You have delete " & oResult & " Characters from current selection" 'Memory cleanup Set oSelection = Nothing End Sub
Output
Next >> Understand Custom Dictionary in Word using VBA code example
If you work with long Microsoft Word documents and frequently have to delete pages, you’ll appreciate this simple VBA procedure.
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:
- Click the Quick Access Toolbar dropdown and choose More Commands.
- In the left pane, click Customize Ribbon.
- Click the Customize button (Figure A) to create the shortcut.
- In the resulting dialog, choose Macros from the Categories list (it’s near the bottom of the list).
- From the Save Changes In dropdown, choose the active document.
- 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.
- Click Assign, click Close, and then click OK to return to the document.
Figure A
Figure B
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
Figure D
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.