Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-29-2012, 12:53 AM
tinfanide tinfanide is offline simpler way to delete a particular page in a Word document??? Windows 7 64bit simpler way to delete a particular page in a Word document??? Office 2010 32bit
Expert
simpler way to delete a particular page in a Word document???
 
Join Date: Aug 2011
Posts: 312
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 simpler way to delete a particular page in a Word document??? Windows 7 64bit simpler way to delete a particular page in a Word document??? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
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
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 06-29-2012, 07:44 AM
tinfanide tinfanide is offline simpler way to delete a particular page in a Word document??? Windows 7 64bit simpler way to delete a particular page in a Word document??? Office 2010 32bit
Expert
simpler way to delete a particular page in a Word document???
 
Join Date: Aug 2011
Posts: 312
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 simpler way to delete a particular page in a Word document??? Windows Vista simpler way to delete a particular page in a Word document??? Office 2010 32bit
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,125
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
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 08-03-2018, 08:44 AM
Aliqux Aliqux is offline simpler way to delete a particular page in a Word document??? Windows 10 simpler way to delete a particular page in a Word document??? 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
  #6  
Old 02-14-2019, 12:51 PM
AntiqueWhale AntiqueWhale is offline simpler way to delete a particular page in a Word document??? Windows 10 simpler way to delete a particular page in a Word document??? 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
  #7  
Old 02-14-2019, 01:36 PM
macropod's Avatar
macropod macropod is offline simpler way to delete a particular page in a Word document??? Windows 7 64bit simpler way to delete a particular page in a Word document??? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
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
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 02-14-2019, 09:41 PM
AntiqueWhale AntiqueWhale is offline simpler way to delete a particular page in a Word document??? Windows 10 simpler way to delete a particular page in a Word document??? 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
  #9  
Old 02-15-2019, 11:56 AM
AntiqueWhale AntiqueWhale is offline simpler way to delete a particular page in a Word document??? Windows 10 simpler way to delete a particular page in a Word document??? 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
  #10  
Old 02-15-2019, 02:12 PM
macropod's Avatar
macropod macropod is offline simpler way to delete a particular page in a Word document??? Windows 7 64bit simpler way to delete a particular page in a Word document??? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
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
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
simpler way to delete a particular page in a Word document??? 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
simpler way to delete a particular page in a Word document??? 600 page document in word 2007 ggun123 Word 3 08-23-2011 06:54 AM
simpler way to delete a particular page in a Word document??? 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

Other Forums: Access Forums

All times are GMT -7. The time now is 02:58 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft