Delete contents of document

R

Rhino

I'm looking for the "best" way of deleting all of the current contents of a
document from within a macro.

Originally, I was doing this and it worked fine:

'Erase all other content
For Each Wd In ActiveDocument.Words
Wd.Delete
Next Wd

However, as soon as my document got some tables added to it, they weren't
being deleted so I had to add this:

'Erase the existing tables
Dim oTable As Table
For Each oTable In ActiveDocument.Tables
oTable.Delete
Next oTable

I can just imagine having to add different loops to delete graphics or
tables of contents or whatever else the first loop doesn't touch.

I've got to believe there is a simple, direct way to delete the entire
contents of the document but I wasn't able to find one. For example, I'd
hoped that something like "ActiveDocument.Delete" might exist and do the job
but that particular method does not exist and I haven't found anything
analogous.

Does a simple, direct method exist or will I keep having to refine my
"eraseContents" routine as my document changes?
 
G

Greg Maxey

Rhino,

Try:
Sub DeleteAll()
Dim oStory As Range
For Each oStory In ActiveDocument.StoryRanges
oStory.Delete
Next
End Sub

Why don't you just open a new blank document based on your template?
 
R

Rhino

Thanks for the suggestion, Greg! Your code works fine for me.

As for opening a new blank document using my template: I don't have a
template for this document and I'm not sure I see the point in learning how
to make one in my case. But I'll think about it. Maybe it would make some
things easier....

Rhino
 

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