Document Versions Aren't Removed by VBA

I

Ishmael

I've sent this to Microsoft Support but they suggested I
ask here. I want to programmatically remove Document
Versions in code. I have the following macro defined:

Public Sub RemoveVersions()
With ActiveDocument.Versions
Do While .Count > 0
.Item(1).Delete
Loop
End With
End Sub

I created a document with some versions in it and then
removed those versions using the UI (File | Versions...).
I opened the document again using the "Recover Text from
any File" option in the "Files of Type" dropdown. The
document had no trace of the old versions that I could
see. I opened the original copy of the document that
contained versions and ran the RemoveVersions macro. I
opened the Versions dialog and it did not show any
versions. I saved this document to a new filename then
used the "Recover Text from any File" option to open it
again. I could read the previous versions, they were not
removed.

I first noticed this problem when I was using VB to
automate Word. In that case calling the Save method (or
SaveAs) of the document object caused the Versions
collection to re-populate itself as if they had never
been removed. In my opinion, the simplified version of
the problem above (using a macro) is more severe since it
falsely gives the impression that the versions were
successfully removed.

I'm using Word XP SP-2.

Has anyone else seen this problem? How about in Word 2003?

Thanks,
Ishmael Turner.
 
D

DA

Hi Ishmael

After your RemoveVersion() routine, try turning off the
versioning prior to your save. e.g.:

ActiveDocument.Versions.AutoVersion = wdAutoVersionOff
ActiveDocument.Save

I've just tried it in Word2003 and it worked, so I hope
you have the same luck.
Dennis
 
I

Ishmael

I tried setting AutoVersion to wdAutoVersionOff, without
any difference in behaviour. AutoVersion was off in any
case and turning it on then off didn't help either. I
briefly got the impression that it made a difference if
the macro was in an addin template or if it was in
ThisDocument (it looked as if it worked when it was in
ThisDocument). On further testing that proved to be
illusory.

I should be able to get some time on a machine with Word
2003, so I'll try it there.

Thanks!
Ishmael Turner
 
I

Ishmael

I tried it with Word 2003 but I can still see the old
versions if I open the document with the "Recover Text
from Any File" option. Also if I call the Save method the
versions reappear in the list and the "Automatically save
a version on close" setting is not sticky (it remains on,
even though it was turned off in code). Using SaveAs the
Auto Version setting is kept and the version list is
empty but the versions are still in the document.

This suggests that there is some 'dirty' flag that isn't
set so I'm going to try again but I'll insert some text
before I call Save.
 

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