question

PinoCarafa-9556 avatar image
0 Votes"
PinoCarafa-9556 asked AlbertKallal-4360 commented

After 11 years Word is still holding a lock on the original .doc after using .SaveAs (.SaveAs2)

I see that this issue was already raised at least 11 years ago:

https://social.msdn.microsoft.com/Forums/en-US/ab69e025-8aac-4349-8aef-703c4608b3eb/cant-delete-file-after-renaming-due-to-word-file-handle-still-present-error-staes-document-is?forum=isvvba

My own situation is slightly different, but it's the same underlying issue.

I open a Word.Document in a .NET application. The Word.Document happens to be referencing an old style ".doc" rather than a ".docx"

Once it's opened, I use .SaveAs2 to save a copy of this document to a new location.

At that point I need to copy another file to the original location.

This fails because even after the .SaveAs2 which saved the document to a new location, Word still holds a lock on the original .doc

The problem doesn't arise with .docx documents. Nor does it arise with .xls workbooks (using the equivalent Excel objects). Only in Word.

And no, I can't code around it. The .doc is opened by one application, while our Add-in is trying to then use SaveAs2 to copy it to another location. At this point the document is already open and I cannot close it because the calling application holds a handle to the Word.Document.

Yes, I can code around it by modifying the original application. Simply let it convert the .doc to a .docx and then present it to the user. But that requires a new version of the original application. Our users are fickle beasts. Some of them want to be able to use the OLD application with the NEW addin. And the only thing preventing us from giving them that option is this issue.

Like I said. This was already reported to Microsoft at least 11 years ago. Who knows how long people have faced similar issues before that.

office-vba-devoffice-itprooffice-word-itprooffice-vsto-com-dev
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


If you do not call SaveAs2 in your Add-In, are you able to copy another file to the original location if the file is opened by one original application?

0 Votes 0 ·

1 Answer

StefanBlom-6438 avatar image
0 Votes"
StefanBlom-6438 answered AlbertKallal-4360 commented

When you use Save As on a document in *.doc format, the owner/lock file (whose name starts with ~$) will be retained even after the save. That is why Word thinks the file is in use. This can easily be reproduced even without code, using the Save As interface in Word. See attached screen shot. 81990-retainedlockfileexample.png

You can report it to Microsoft, but my guess is that it will have low priority, because we are expected to use the *.docx format.



· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I think the lock is at OS level. When I went looking for these "tilde" files I didn't find them. Also, I wasn't able to perform any action on the original .doc even through the file system.

I'll have a look again but I'm pretty sure that that's how that cookie crumbled....

0 Votes 0 ·

Word is definitely creating the "lock" files, as well as other temporary files; for an overview, see https://support.microsoft.com/en-us/topic/description-of-how-word-creates-temporary-files-66b112fb-d2c0-8f40-a0be-70a367cc4c85.

Note that you will have to display hidden files and folders in the operating system for the temporary files to be visible.

0 Votes 0 ·

Gaaahhhhh I feel stupid now; I had forgotten to uncheck the option to hide protected os files and of course the ~ file is there.

That's where the good news ends though. Once I can see the ~ file beside the actual .doc file I can delete it no problem.

So I tried the following:

1) Open a whatever.doc file. The ~$atever.doc file appears.
2) Use Save As from Word's own menu to save the file as a .docx
3) use the file system to look at the folder, The ~$atever.doc file is still there, delete it
4) still in the file system, try to rename the original whatever.doc file

Still not working.

If there is a way to programmatically remove the remaining lock on the original .doc file, please share.

0 Votes 0 ·
Show more comments