Deleting the second page of a Word document (if it exists)

D

dr mabuse

Hi -- I'm trying to figure out how to solve this problem: I have some
code that runs from Excel that inserts (pastes) an Excel range into a
Word document. However, because I have to email the document out each
month, I'd like to delete the previous months range before inserting
the current month. Here's the code that gets the Excel range into
Word:

Wkb.Sheets("Summary").Range(stRangeName).Copy
On Error Resume Next
Set wdApp = New Word.Application
With wdApp
.Documents.Open Filename:=stDocName
With .Selection
.EndKey Unit:=wdStory
.TypeParagraph
.Paste
End With
.ActiveDocument.Close SaveChanges:=True
End With

What I think should happen is that the previous months insertion should
be deleted before the current months range (stRangeName) is copied.
Any help or suggestions about how to do that would be appreciated.
 
G

Greg Maxey

Does all of the old text in the document get deleted? If so then use:

ActiveDocument.Range.Delete
Selection.Paste

Instead of:
With .Selection
.EndKey Unit:=wdStory
.TypeParagraph
.Paste
End With
 
H

Helmut Weber

Hi,

like this,
which deletes the second page,
not necessarily last month's insertion,
as there is no clue what that could be.

Sub deletepage2()
Dim lngNumPages As Long
With ActiveDocument.BuiltInDocumentProperties
lngNumPages = .Item("number of pages")
End With
If lngNumPages > 1 Then
With selection
.Collapse
.ExtendMode = False
.GoTo what:=wdGoToPage, _
which:=wdGoToAbsolute, _
Count:=2
.Bookmarks("\page").Range.Select
.Delete
End With
End If
End Sub

To select .Bookmarks("\page").Range
isn't required. Its there for ease
of understanding what's going on.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
D

dr mabuse

Greg said:
Does all of the old text in the document get deleted? If so then use:

ActiveDocument.Range.Delete
Selection.Paste

Instead of:


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


Greg -- Yes, I could do it this way, if I recreated the letter each
month from a template. I might do that if the other posted solution
doesn't work. Thanks for your suggestion.
 
D

dr mabuse

Helmut said:
Hi,

like this,
which deletes the second page,
not necessarily last month's insertion,
as there is no clue what that could be.

Sub deletepage2()
Dim lngNumPages As Long
With ActiveDocument.BuiltInDocumentProperties
lngNumPages = .Item("number of pages")
End With
If lngNumPages > 1 Then
With selection
.Collapse
.ExtendMode = False
.GoTo what:=wdGoToPage, _
which:=wdGoToAbsolute, _
Count:=2
.Bookmarks("\page").Range.Select
.Delete
End With
End If
End Sub

To select .Bookmarks("\page").Range
isn't required. Its there for ease
of understanding what's going on.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Helmut -- Thanks for the helping hand. I'll give it a try!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top