?
Solved

Word Macro remove First Page from doc

Posted on 2007-03-20
8
Medium Priority
?
986 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
7 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 100 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 100 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

831 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