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.
Greg Maxey - 18 Aug 2006 01:12 GMT
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

Signature
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
> 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
[quoted text clipped - 20 lines]
> copied. Any help or suggestions about how to do that would be
> appreciated.
dr mabuse - 18 Aug 2006 17:07 GMT
> Does all of the old text in the document get deleted? If so then use:
>
[quoted text clipped - 39 lines]
> > copied. Any help or suggestions about how to do that would be
> > appreciated.
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.
Helmut Weber - 18 Aug 2006 01:29 GMT
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.

Signature
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
dr mabuse - 18 Aug 2006 17:08 GMT
> Hi,
>
[quoted text clipped - 32 lines]
> Win XP, Office 2003
> "red.sys" & Chr$(64) & "t-online.de"
Helmut -- Thanks for the helping hand. I'll give it a try!