检查word文件是否已用VB.net打开

检查word文件是否已用VB.net打开,vb.net,ms-word,Vb.net,Ms Word,我有一个生成文档的应用程序 但是,如果文档已打开,则生成的新文档无法覆盖打开的文档,因此不会发生更改 如何正确检查文档是否已打开?(如果打开,然后关闭)您可以这样尝试: If File.Exists(Application.StartupPath & "\~$MyWordDocument.doc") Then MsgBox("File is open") Exit Sub End If 另外,请勾选您可以这样尝试: If File.Exists(Application.

我有一个生成文档的应用程序

但是,如果文档已打开,则生成的新文档无法覆盖打开的文档,因此不会发生更改

如何正确检查文档是否已打开?(如果打开,然后关闭)

您可以这样尝试:

 If File.Exists(Application.StartupPath & "\~$MyWordDocument.doc") Then
   MsgBox("File is open")
   Exit Sub
End If
另外,请勾选您可以这样尝试:

 If File.Exists(Application.StartupPath & "\~$MyWordDocument.doc") Then
   MsgBox("File is open")
   Exit Sub
End If

同时检查此代码是否适用于我

Public Function FileInUse(ByVal sFile As String) As Boolean
     Dim thisFileInUse As Boolean = False
     If System.IO.File.Exists(sFile) Then
         Try
           Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
                    ' thisFileInUse = False
           End Using
         Catch
           thisFileInUse = True
         End Try
     End If
     Return thisFileInUse
    End Function

这个代码对我有用

Public Function FileInUse(ByVal sFile As String) As Boolean
     Dim thisFileInUse As Boolean = False
     If System.IO.File.Exists(sFile) Then
         Try
           Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
                    ' thisFileInUse = False
           End Using
         Catch
           thisFileInUse = True
         End Try
     End If
     Return thisFileInUse
    End Function

谢谢你,几乎成功了!检查临时word文件的~$是一个明智的选择!我只是不需要应用程序。StartupPath&来控制它。谢谢,它几乎成功了!检查临时word文件的~$是一个明智的选择!我只是不需要Application.StartupPath&来控制它。这也是一个不错的选择!这也是一个不错的选择!