#1
|
|||
|
|||
simpler way to delete a particular page in a Word document???
Code:
Dim oRange As Range Dim iPage As Integer Dim iEnd1 As Integer, iEnd2 As Integer iPage = 2 With ActiveDocument Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=iPage Set oRange = .Range(Start:=Selection.Range.Start, End:=.Bookmarks("\Page").Range.End) oRange1.Delete End With Thanks in advance. |
#2
|
||||
|
||||
That's about it, though you don't need to use Selection:
Code:
Dim Rng As Range, iPage As Long iPage = 2 With ActiveDocument Set Rng = .GoTo(What:=wdGoToPage, Name:=iPage) Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\page") Rng.Delete End With
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Quote:
Yes, I didn't wanna use Selection all the time and that was why I was asking. I've thought of using Range.GoTo, but just didn't know to use wdGoToBookmark. Thanks for your example, always. |
#4
|
|||
|
|||
This is the same thing with an Input box to get the page number.
I changed iPage to an Integer because I didn't understand the need for a long variable there. There may be a type mismatch between the Input Box result and the variable but it seems to work. Code:
Dim Rng As Range, iPage As Integer On Error Resume Next iPage = InputBox("Which page do you want to delete.", "Delete Page") With ActiveDocument Set Rng = .GoTo(What:=wdGoToPage, Name:=iPage) Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\page") Rng.Delete End With |
#5
|
|||
|
|||
I found this post and it was very useful for what I was ding. I found an issue with deleting the last page of a document and also found a solution to it and thought I would share it as well.
Code:
Sub DeletePage() ' ' DeletePage Macro ' Requests the page number to delete and removes it ' Dim Message, Title, Default, Request Dim rgePages As Range Dim finalPage Dim iPage As Long 'Input Page Request Message = "Enter the page number to delete" Title = "Page Removal" Default = Selection.Information(wdActiveEndPageNumber) On Error Resume Next iPage = InputBox(Message, Title, Default) With ActiveDocument Set rgePages = .GoTo(What:=wdGoToPage, Name:=iPage) Set rgePages = rgePages.GoTo(What:=wdGoToBookmark, Name:="\page") rgePages.Delete End With 'Find Last page and remove it if selected for removal finalPage = ActiveDocument.Range.Information(wdActiveEndAdjustedPageNumber) If finalPage = CInt(iPage) Then ActiveDocument.Bookmarks("\Page").Range.Delete Selection.MoveLeft Extend:=wdExtend Selection.Delete End If End Sub |
#6
|
|||
|
|||
Then, how to delete several not continue page in word using Excel VBA. For example: I want to delete page 2,3, 7,9, 13 all at once.
|
#7
|
||||
|
||||
What have you tried? The code in the posts above show how to delete a particular page - simply repeat the process for each page you want to delete (remembering, of course, to work backwards).
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Thank you very much sir
|
#9
|
|||
|
|||
Quote:
I think in your code, you set the rng start point using: ActiveDocument.Goto(What:=wdGotopage, Name:iPage) you set the rng endpoint using rng.Goto(What:=wdGotoBookmark, Name:="\Page") Could you elaborate on this? And I write VBA code in Excel to manipulate Word behavior using late bind technology, I know I can not use any word object build in constant instead of using pure numerical value. Do you have any other caveat for me? Thanks!!!!!! |
#10
|
||||
|
||||
The line:
Set Rng = .GoTo(What:=wdGoToPage, Name:=iPage) points Rng to the start of a particular page, in the case the page represented by whatever iPage is. The line: Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\page") tells Rng to span the page it's pointing to.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Delete a page after Section Break Next Page | Aston | Word | 9 | 04-27-2022 07:38 AM |
Can't delete word document | Sparky | Word | 1 | 04-18-2012 12:20 AM |
600 page document in word 2007 | ggun123 | Word | 3 | 08-23-2011 06:54 AM |
How to delete one PAGE in word 2002? | Sleeper | Word | 2 | 06-28-2011 04:58 PM |
delete subtitle on a word document damn thing will not go | waynegr | Word | 0 | 07-09-2006 06:15 AM |