Word2000: Delete a Page with VBA

T

Tony Bonacci

I have a Word 2000 Template that has a bunch of bookmarks and a Data Entry
Form, that the user creates a New Document off of. When the user creates a
new document based on this template, there is a Macro that the user can run
that loads a user entry Form allowing the user has to enter data. When the
user clicks the OK button on the form the following 2 things happen:
1. All the bookmarks within the document get updated based on data from
the User Entry Form
2. At the end of the document, a particular Word document (Test.doc) is
inserted at the "Attachment" bookmark

Everything works fine the first time the user clicks the OK button. However,
if the user reruns the same macro within the same document, re-enters
different data on the user entry form and clicks the OK button, all the
bookmarks update properly, but the inserted file gets added after the 1st
one (the 1st one does not get cleared out) - so the user has 2 inserted
files. For instance, the first time the user runs the letter there is 3
pages (2 pages + 1 File), and if the user runs it again, there will be 4
pages (2 pages + 2 Files - 1st one and now the 2nd one), and so on...
Basically, each time the user clicks the OK button, the appropriate file
keeps getting appended to the end.

My problem is, how can I remove the file (Test.doc) that was inserted on the
1st run before we insert the file on the 2nd run? Is there a way to delete
the entire page before we insert the file, that way all the data is cleared
leaving me with only 2 pages, before I do my insert? My sample code of
inserting the file is below:

Dim bmRange As Range
' This is my bookmark where I want to add the file
Set bmRange = ActiveDocument.Bookmarks("Attachment").Range
With bmRange
.Collapse Direction:=wdCollapseEnd
' Insert a Page Break after the bookmark so that the file being
inserted is on it's own page
.InsertBreak Type:=wdPageBreak
' Insert the Test Document
.InsertFile FileName:="C:\Test.doc", Range:="", _
ConfirmConversions:=False, Link:=False, Attachment:=False
.Collapse Direction:=wdCollapseEnd
End With
 
J

Jezebel

Here's one approach: Make sure the Attachment bookmark points either to the
end of document (ie the attachment has not been added) or to the section of
document that includes the attachment. When you run the code a) delete the
range if not empty; then b) redefine the bookmark to include the entire
attachment range --

Dim pStartPoint as long
With MyDoc.Bookmarks("Attachment").Range
pStartpoint = .Start 'Remember the range start
position
.Delete 'Delete anything the
range may contain (does nothing if empty)
... insert page break and attach file
End with

'Reset the bookmark
MyDoc.Bookmarks("Attachment").Delete
MyDoc.Bookmarks.Add "Attachment", MyDoc.Range(pStartPoint, pDoc.Range.End
 
V

VBA Coder

Thank you for the suggestion, it worked great with a little tweaking.
Apparently when you issue the .Delete command on the Range object, the
Bookmark is DELETED, therefore, we cannot issue the .Delete command when you
try to Reset the Bookmark because the Bookmark no longer exists.

Anyway, here is the modified code that actually works the way I wanted it
to, thanks to your helpful suggestion:

Dim lStartPoint As Long
With ActiveDocument.Bookmarks("Attachment").Range
' Remember the range start Position
lStartPoint = .Start
' Delete anything the range may contain (does nothing if empty)
.Delete
' Insert page break and attach file
.InsertBreak Type:=wdPageBreak
.InsertFile FileName:="C:\Test.doc", Range:="", _
ConfirmConversions:=False, Link:=False,
Attachment:=False
End With

' Re-Add the Attachment bookmark at the same position it was before we
deleted it
With ActiveDocument
'.Bookmarks("Attachment").Delete --- We cannot issue this command
since the Bookmark was deleted above and no longer exists
.Bookmarks.Add "Attachment", .Range(lStartPoint, .Range.End)
.Bookmarks("Attachment").Range.Collapse Direction:=wdCollapseEnd
End With
 

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