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: 18,671
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 offline Windows Vista Office 2010 32bit
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 5,447
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: 18,671
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 offline Windows 7 64bit Office 2010 32bit
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 5,447
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
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 06:58 PM.


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