ted.martin
Registered User.
- Local time
- Today, 15:23
- Joined
- Sep 24, 2004
- Messages
- 743
I am trying to check whether a Word File is already open before running the Word Object model. VB Code found elsewhere says this should work BUT when the Word file is open, I cannot trap the resulting VB Error 70. It seems that the usual Access Error trapping code only works for Access generated errors; not those generated by VB.
Any thoughts would be appreciated. My code is below:
Public Function IsFileLocked(sFile As String) As Boolean
On Error GoTo Err_FileLocked
'Below is the bit that determines if the file is open.
Open sFile For Binary Access Read Write Lock Read Write As #1
Close #1
Err_FileLocked:
If Err.Number <> 0 Then
MsgBox "E R R O R - The file that your are trying to access," & vbCrLf & "is already open." & vbCrLf & vbCrLf & _
"Please close the file and try again", vbCritical, "IsFileLocked"
IsFileLocked = True
Err.Clear
End If
End Function
Public Sub OpenFile()
On Error GoTo ErrorHandler
If Not IsFileLocked("c:\temp\This is a test.docx") Then
Else
Exit Function
End If
Dim objWord As Word.Application
Dim myDoc As Word.Document
' close any previous links - just housekeeping
Set objWord = Nothing
Set myDoc = Nothing
Set objWord = CreateObject("Word.Application")
Set myDoc = objWord.Documents.Open("c:\temp\This is a test.docx")
objWord.Visible = True
End Sub
+++++++++++++++
I have even tried this code variant but the same VB Error 70 runs and I cannot trap it
Function fIsDocFileOpen(ByVal strDocFileName As String) As Boolean
'**************************************************
'Name: fIsDocFileOpen (Function)
'Purpose: Checks to see if the Word document is already open
'Author: Dev Ashish
'Date: February 11, 1999, 05:50:58 PM
'Called by: Any
'Calls: None
'Inputs: strDocFileName: Full path to the Word document
'Output: True if file is open, false otherwise
'*******************************************
On Error GoTo ErrHandler
Dim intFree As Integer
intFree = FreeFile()
Open strDocFileName For Input Lock Read As intFree
fIsDocFileOpen = False
ExitHere:
On Error Resume Next
Close #intFree
Exit Function
ErrHandler:
fIsDocFileOpen = True
Resume ExitHere
End Function
Any thoughts would be appreciated. My code is below:
Public Function IsFileLocked(sFile As String) As Boolean
On Error GoTo Err_FileLocked
'Below is the bit that determines if the file is open.
Open sFile For Binary Access Read Write Lock Read Write As #1
Close #1
Err_FileLocked:
If Err.Number <> 0 Then
MsgBox "E R R O R - The file that your are trying to access," & vbCrLf & "is already open." & vbCrLf & vbCrLf & _
"Please close the file and try again", vbCritical, "IsFileLocked"
IsFileLocked = True
Err.Clear
End If
End Function
Public Sub OpenFile()
On Error GoTo ErrorHandler
If Not IsFileLocked("c:\temp\This is a test.docx") Then
Else
Exit Function
End If
Dim objWord As Word.Application
Dim myDoc As Word.Document
' close any previous links - just housekeeping
Set objWord = Nothing
Set myDoc = Nothing
Set objWord = CreateObject("Word.Application")
Set myDoc = objWord.Documents.Open("c:\temp\This is a test.docx")
objWord.Visible = True
End Sub
+++++++++++++++
I have even tried this code variant but the same VB Error 70 runs and I cannot trap it
Function fIsDocFileOpen(ByVal strDocFileName As String) As Boolean
'**************************************************
'Name: fIsDocFileOpen (Function)
'Purpose: Checks to see if the Word document is already open
'Author: Dev Ashish
'Date: February 11, 1999, 05:50:58 PM
'Called by: Any
'Calls: None
'Inputs: strDocFileName: Full path to the Word document
'Output: True if file is open, false otherwise
'*******************************************
On Error GoTo ErrHandler
Dim intFree As Integer
intFree = FreeFile()
Open strDocFileName For Input Lock Read As intFree
fIsDocFileOpen = False
ExitHere:
On Error Resume Next
Close #intFree
Exit Function
ErrHandler:
fIsDocFileOpen = True
Resume ExitHere
End Function
Last edited: