This project has moved and is read-only. For the latest updates, please go here.

Can't delete file after renaming due to Word file handle still present

Aug 14, 2013 at 6:29 AM
Edited Aug 14, 2013 at 6:30 AM
Hi All,

I have some code that allows the user to open a file, edit it, then save it with a new name (SaveAs). The code saves the file with the new name & then deletes that older file.
This code used to work properly on Word 2003 & 2007, but not 2010 BEFORE I switched to NetOffice.

On Word 2010, the code fails at the delete function because the newer version of word is not dropping the file handle from the first named file when user saves it to the new filename. In older versions (2003 & 2007) this handle was released as soon as file was saved to a different name. Using NetOffice, the problem now occurs with all versions.

I can't seem to figure out how to release the old document filename so the old document file can be deleted.

Any ideas?

Thanks & regards,
Aug 14, 2013 at 1:27 PM
do you have a code snippet for me for testing?

Aug 15, 2013 at 8:09 AM
The code I'm referring to is part of a rather lengthy code. I'll try to simplify the code & get back to you.

Aug 18, 2013 at 6:29 AM
Here's the code snippet:
Private wordApp As NetOffice.WordApi.Application
Private wordDoc As NetOffice.WordApi.Document

Private Sub btnSaveAs_Click(sender As Object, e As EventArgs) Handles btnSaveAs.Click
        ' Save As
            Dim oldFileName As String = Me.wordDoc.FullName
            Dim newFileName As String = Me.txtNewFileName.Text
            IO.File.Delete(oldFileName)   'Throws Exception

        Catch ex As Exception
            MsgBox(ex.Message & vbCrLf & ex.StackTrace)
        End Try
End Sub
& the exception is:
The process cannot access the file 'oldFileName' because it is being used by another process.
Stack Trace:    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.InternalDelete(String path, Boolean checkHost)
   at System.IO.File.Delete(String path)
Please note that for Word 2003 & 2007, I can delete the oldFileName file from Windows Explorer after I SaveAs, while the file will be locked to Word process in Word 2010:

The documents I'm referring to can be either .doc or .docx

Your help is really appreciated, thanks.
Aug 18, 2013 at 7:44 AM
my first test works fine ( i use SaveAs instead of SaveAs2 because i use OfficeXP)
i do a new test on my workplace with office 2010 (monday)

i have modified the example01.vb from the NetOffice examples.
    Public Sub RunExample() Implements ExampleBase.IExample.RunExample

        ' start word and turn off msg boxes
        Dim wordApplication As New Word.Application
        wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone

        ' add a new document
        Dim newDocument As Word.Document
        newDocument = wordApplication.Documents.Add()

        ' more code here !!!

        ' we save the document as .doc for compatibility with all word versions
        Dim documentFile As String = String.Format("{0}\Example01{1}", _hostApplication.RootDirectory, ".doc")
        Dim documentFile2 As String = String.Format("{0}\Example01{1}", _hostApplication.RootDirectory, "2.doc")

        Dim wordVersion As Double = Convert.ToDouble(wordApplication.Version, CultureInfo.InvariantCulture)
        If (wordVersion >= 12.0) Then
            newDocument.SaveAs(documentFile, WdSaveFormat.wdFormatDocumentDefault)
        End If

        SaveNew(newDocument, documentFile, documentFile2)

        ' close word and dispose reference

        ' show dialog for the user(you!)
        _hostApplication.ShowFinishDialog(Nothing, documentFile)

    End Sub

    Private Sub SaveNew(ByVal document As Word.Document, ByVal oldFileName As String, ByVal newFileName As String)
            IO.File.Delete(oldFileName)   'Throws Exception

        Catch ex As Exception
            MsgBox(ex.Message & vbCrLf & ex.StackTrace)
        End Try
    End Sub
Aug 18, 2013 at 8:37 AM
Edited Aug 18, 2013 at 9:19 AM
btw: please tell me your operating system (windows version, looks like window fuckyou8)
Aug 18, 2013 at 4:13 PM
Edited Aug 18, 2013 at 4:13 PM
What's the difference between SaveAs & SaveAs2? I tried both & got the same results.

As for the document format, the original code expects multiple formats so it looks something like this:
Dim Word_Format As WdSaveFormat
Select Case filExt.ToLower     
        Case ".rtf"
                Word_Format = WdSaveFormat.wdFormatRTF
        Case ".doc"
                Word_Format = WdSaveFormat.wdFormatDocument
        Case ".docx"
                Word_Format = WdSaveFormat.wdFormatXMLDocument
        Case ".txt"
                Word_Format = WdSaveFormat.wdFormatText
        Case ".xml"
                Word_Format = WdSaveFormat.wdFormatXML
        Case ".dot"
                Word_Format = WdSaveFormat.wdFormatTemplate
        Case ".html"
                Word_Format = WdSaveFormat.wdFormatHTML
        Case Else
                Word_Format = WdSaveFormat.wdFormatRTF
End Select

Me.wordDoc.SaveAs2(newFileName, Word_Format)
I tried the code on Windows 7 (Word 2003, 2007, & 2010), & Windows 8 (Word 2010), & got the exception in all cases.
Aug 28, 2013 at 6:16 AM

Any updates about this issue?

Best regards
Oct 22, 2014 at 10:35 AM
Edited Oct 22, 2014 at 10:36 AM
Before the statement
you have to close the document which can be done by
document.Close(WdSaveOptions.wdDoNotSaveChanges, oMissing, oMissing);
Aeter this, make a delete call.

Hope this helps.