Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2006

Tip: Looking for answers? Try searching our database.

Delete contents of document

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rhino - 31 Jan 2006 22:22 GMT
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?

Signature

Rhino

Greg Maxey - 31 Jan 2006 22:51 GMT
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?

Signature

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

> I'm looking for the "best" way of deleting all of the current
> contents of a document from within a macro.
[quoted text clipped - 26 lines]
> Does a simple, direct method exist or will I keep having to refine my
> "eraseContents" routine as my document changes?
Rhino - 01 Feb 2006 05:07 GMT
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

> Rhino,
>
[quoted text clipped - 38 lines]
>> Does a simple, direct method exist or will I keep having to refine my
>> "eraseContents" routine as my document changes?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.