skip to Main Content

We have designed Microsoft Addins for MS Excel and Word 2019 written in VB.net.
There we have designed a tab, on clicking this tab, we open a Task Pane.
On loading this task pane, we execute a code to launch another Excel File/Word file.

So when I delete a file in MS Word that is already open it shows an exception The file ‘Filename’ already exists.
Given below is the code snippet I am using to delete an already existing open file named processFile

My.Computer.FileSystem.DeleteFile(processFile)

Now when I run the same code snippet in MS Excel it does not show this exception and deletes the file.

I am not able to understand this behavior.
Kindly suggest if anyone has understanding on it

Here is some more information about which environment I’m working in:

Operating System : Microsoft Windows 10 Pro

Code Editor : Visual Studio 2019

Technology : Vb.net(.Net Framework 4.8)

MS Office Version : 2019(32 bit) : Microsoft Windows 10 Pro

3

Answers


  1. The IOException should be thrown if the file is in use. You need to close any editors first and then delete files.

    Login or Signup to reply.
  2. Check if the file is open. I have a code below that may help.

    As import sfile use the fullpath with filename. (eg. "C:Windowstest.txt")
    returns True when file it’s open and false when it isn’t.

    Yu will need "Imports System.IO"

     Public Function IsFileInUse(sFile As String) As Boolean
            Try
                If File.Exists(sFile) Then
                    Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
                    End Using
                End If
            Catch Ex As Exception
                Return True
            End Try
            Return False
        End Function
    
    Login or Signup to reply.
  3. As per the information in the comment section, the file is opened in "protected mode" and you are saying that it is deleting while it is still open to the user.

    So yes, if the file is engaged and deleted by some program, it should throw an exception and not delete. But if no exception is coming, i.e. some specific handling has been done by Windows/MS Excel so that file has been deleted while it is still kept on buffer and open. Its answer can be given by Microsoft only.

    One solution, I can suggest is to close the "protected object" and then delete the file so that no file will be opened to the user and it will be safely deleted.

    Snippet to open the file for Add-ins(VSTO):

    Dim objProtectedMode As String objProtectedMode = Globals.Connect.Application.ProtectedViewWindows.Open(fileLocation, , False, False)

    Snippet to delete the file safely:

    If Not (objProtectedMode Is Nothing) Then
    objProtectedMode.Close()
    End If
    
    My.Computer.FileSystem.DeleteFile(objProtectedMode)
    

    Note: – Solution may vary for other add-ins. Code snippet provided for VSTO Add-ins.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search