Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 06-29-2012, 12:53 AM
tinfanide tinfanide is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2011
Posts: 308
tinfanide is on a distinguished road
Default 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
Just wonder if there's a simpler way to delete a particular page in a Word document.


Thanks in advance.
Reply With Quote
  #2  
Old 06-29-2012, 01:05 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,010
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #3  
Old 06-29-2012, 07:44 AM
tinfanide tinfanide is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2011
Posts: 308
tinfanide is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
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
A nifty one.
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.
Reply With Quote
  #4  
Old 07-03-2012, 03:36 AM
Charles Kenyon Charles Kenyon is online now Windows Vista Office 2010 32bit
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 5,630
Charles Kenyon is a glorious beacon of lightCharles Kenyon is a glorious beacon of lightCharles Kenyon is a glorious beacon of lightCharles Kenyon is a glorious beacon of lightCharles Kenyon is a glorious beacon of lightCharles Kenyon is a glorious beacon of light
Default

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
One other thought: While deleting a page this way will work, "page" is seldom a logical part of a Word document unless you are using manual page breaks. Inserting manual page breaks is generally not a good formatting practice.
Reply With Quote
  #5  
Old 07-03-2012, 04:14 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,010
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Hi Charles,

VBA no longer uses Integer variables. If you use an Integer variable, VBA converts it to a Long - then back again if you're updating it. Specifying the Long form simply cuts out the time-wasting conversions.

Besides, it takes fewer keystrokes to define a Long variable ...
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #6  
Old 07-17-2014, 06:40 PM
Charles Kenyon Charles Kenyon is online now Windows 7 64bit Office 2010 32bit
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 5,630
Charles Kenyon is a glorious beacon of lightCharles Kenyon is a glorious beacon of lightCharles Kenyon is a glorious beacon of lightCharles Kenyon is a glorious beacon of lightCharles Kenyon is a glorious beacon of lightCharles Kenyon is a glorious beacon of light
Default

Thank you for the update.
Reply With Quote
  #7  
Old 08-03-2018, 08:44 AM
Aliqux Aliqux is offline Windows 10 Office 2016
Novice
 
Join Date: Aug 2018
Posts: 1
Aliqux is on a distinguished road
Default

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
Reply With Quote
  #8  
Old 02-14-2019, 12:51 PM
AntiqueWhale AntiqueWhale is offline Windows 10 Office 2016
Novice
 
Join Date: Nov 2018
Posts: 11
AntiqueWhale is on a distinguished road
Default

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.
Reply With Quote
  #9  
Old 02-14-2019, 01:36 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,010
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #10  
Old 02-14-2019, 09:41 PM
AntiqueWhale AntiqueWhale is offline Windows 10 Office 2016
Novice
 
Join Date: Nov 2018
Posts: 11
AntiqueWhale is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
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).
Thank you very much sir
Reply With Quote
  #11  
Old 02-15-2019, 11:56 AM
AntiqueWhale AntiqueWhale is offline Windows 10 Office 2016
Novice
 
Join Date: Nov 2018
Posts: 11
AntiqueWhale is on a distinguished road
Smile

Quote:
Originally Posted by macropod View Post
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
Still little confused

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!!!!!!
Reply With Quote
  #12  
Old 02-15-2019, 02:12 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,010
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
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
Delete a page after Section Break Next Page Aston Word 5 07-14-2011 05:43 PM
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


All times are GMT -7. The time now is 05:15 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft