Solved

Word Macro remove First Page from doc

Posted on 2007-03-20
8
679 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

920 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

14 Experts available now in Live!

Get 1:1 Help Now