I have an application that generates documents.
However if a document is already open, then the generated new document cannot override the opened document, so no changes occur.
How can I properly check whether the document is already open or not? (And if open, then close it)
You may try like this:
If File.Exists(Application.StartupPath & "\~$MyWordDocument.doc") Then
MsgBox("File is open")
Exit Sub
End If
This code is working for me
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.