Hi,
I use Word macro to check if an Excel file is alreay opened.
If Not FileOpened(ActiveDocument.Path & "\myFile.xls") Then
Set objExcel = Nothing
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.workbooks.Open(ActiveDocument.Path & "\myFile.xls", 0, True)
Set objWorksheet = objWorkbook.sheets("Sheet1")
objExcel.Visible = True
End If
Function FileOpened(strFileName As String) As Boolean
On Error Resume Next
FileOpened = False
Open strFileName For Binary Access Read Write Lock Read Write As #1
Close #1
If Err.Number <> 0 Then
FileOpened = True
Err.Clear
End If
End Function
The problem are:
1. if myFile is not opened, the code will open it. However, if the code is run again and again, it fails to see the file is already opened so it creates a new Excel object and open the file again and again. How is that?
2. if the file is already opened, how to set the Excel, workbook and worksheet objects to the file and worksheet?
Please help.
Jeff
I use Word macro to check if an Excel file is alreay opened.
If Not FileOpened(ActiveDocument.Path & "\myFile.xls") Then
Set objExcel = Nothing
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.workbooks.Open(ActiveDocument.Path & "\myFile.xls", 0, True)
Set objWorksheet = objWorkbook.sheets("Sheet1")
objExcel.Visible = True
End If
Function FileOpened(strFileName As String) As Boolean
On Error Resume Next
FileOpened = False
Open strFileName For Binary Access Read Write Lock Read Write As #1
Close #1
If Err.Number <> 0 Then
FileOpened = True
Err.Clear
End If
End Function
The problem are:
1. if myFile is not opened, the code will open it. However, if the code is run again and again, it fails to see the file is already opened so it creates a new Excel object and open the file again and again. How is that?
2. if the file is already opened, how to set the Excel, workbook and worksheet objects to the file and worksheet?
Please help.
Jeff