Solved

Word Macro remove First Page from doc

Posted on 2007-03-20
8
667 Views
Last Modified: 2012-05-07
Hi,

Can someone tell me how to remove (Ideally the first page) or any page for that maater of a Word Document using word macros ??
0
Comment
Question by:itimes
8 Comments
 
LVL 11

Expert Comment

by:Louis01
ID: 18758196
Sorry - I could only find how to delete paragraphs..
Maybe it'l help. Good luck

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.Paragraphs(1).Range.Select
        Selection.Delete
    End If
End Sub
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 18760400
Pages are a problem for macros. There no Word object corresponding to the visible page. This is because the meaning of a page is volatile, varying with the font, the printer driver and any editing earlier in the document.

Is your first page limited by a hard (manual) page break or next page section break?

If not, this will work, but it takes a noticeable amount of time.

Sub DelPage1()
    Dim r As Range
    Set r = ActiveDocument.Range(0, 0)
    Do Until r.Information(wdActiveEndPageNumber) = 2
        r.MoveEnd wdCharacter, 1
    Loop
    r.MoveEnd wdCharacter, -1
    r.Text = ""
End Sub

0
 
LVL 24

Accepted Solution

by:
R_Rajesh earned 25 total points
ID: 18762315
Hi Graham, you could probably make use of the Predefined Bookmarks to get the active page.

Sub Test1()
sPageToDelete = 4
Selection.GoTo(What:=1, Which:=2, _
Name:=sPageToDelete).Bookmarks("\Page").Range.Delete
End Sub
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 18762638
Duh! Of course.

itimes,
please ignore my answer & accept Rajesh's

For the sake of understanding the code, we can use built-in constant names instead of the numbers.

Selection.GoTo(What:=wdGoToPage, Which:=wdGoToNext, _
Name:=sPageToDelete).Bookmarks("\Page").Range.Delete
0
 

Author Comment

by:itimes
ID: 18762647
GrahamSkan:

"Is your first page limited by a hard (manual) page break or next page section break?"

It says "Section Break (Next Page) does thsi help ?? Your code works by teh way but it is a little slow .....

R_Rajesh: This code Failed im afrid !
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 25 total points
ID: 18762693
Oh yes. A lot easier.

Sub DeleteFirstSection()
    ActiveDocument.Sections(1).Range.Text = ""
End Sub


I don't know why Rajesh's code didn't work for you. I'll test it here.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 18762713
If you have Option Explicit set, you will need to declare the variable. Otherwise Rajesh's code works.

Sub Test1()
    Dim sPageToDelete As Integer
   
    sPageToDelete = 1
    Selection.GoTo(What:=1, Which:=2, _
    Name:=sPageToDelete).Bookmarks("\Page").Range.Delete
End Sub

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now