Since a couple of days, I have a crash on the instruction Workbooks.Open with Excel 365. This instruction is part of a macro I developed to open excel file, checking first if the file exist or is already open and then calling the Workbooks.Open instruction.
The specific code is the following:
On Error Resume Next
Workbooks.Open FileName:=Full_File_Name, Updatelinks:=0 -> excel 365 exit here
-> no errors despite the On Error Resume Next
errnum = Err
Select Case errnum
Case 0
' Ok continue
Case 40040
'Ignore this error - continue
Case Else
MsgBox ("Check pathname and file name of your file" & File_Name)
End Select
The same code works also perfectly on Excel 2016
I changed the syntax of the line:
Workbooks.Open FileName:=Full_File_Name, Updatelinks:=0
to
set wb = Workbooks.Open (FileName:=Full_File_Name, Updatelinks:=0)
and declared at the top the function: Dim wb As Workbook
With this new syntax there is no crash but now at the end of the exit of some macros calling this function, the macro is reaching its end but Excel freezes:
- behavior on an HP 15 laptop Windows 11 - 16gb (bought in 2021): in the menu Files entries like close are greyed and the only way to exit from Excel is to click on the red cross at the top right of the Excel window and close excel thus generating a report to Microsoft.
- behavior on an HP DV7 laptop Windows 10 - 8gb (bought in 2010): none of the menu entries are responsive and the only way to exit is to click on the red cross at the top right of the Excel window and close excel thus generating a report to Microsoft.
The whole function code is the following:
Function OpenFileIfClosed(FilePathName As String, FileName As String)
'open a closed excel file, let it open if already open
' reply code:
' returns 1 (constant FileisOpen = 1 in the definitions) if the File is open
' or retunrs the VBA error code
Dim errnum As Variant
Dim rc As Boolean
Dim wb As Workbook
Dim Full_File_Name As String
Full_File_Name = FilePathName & "\" & FileName
'Call function to check if the file is open
rc = IsFileOpen(FileName)
Debug.Print ("IsFileOpen reply = " & rc)
If rc Then
Debug.Print ("Already open")
OpenFileIfClosed = 1 'rc = 0 means no errors, therefore file closed -> open the file
Else
On Error Resume Next
'Workbooks.Open FileName:=Full_File_Name, Updatelinks:=0
Set wb = Workbooks.Open(FileName:=Full_File_Name, Updatelinks:=0)
errnum = Err
Select Case errnum
Case 0
' OK continue
Case 40040
'Ignore this error - continue
Case Else
MsgBox ("Check Path name and File Name of your file" & FileName)
End Select
rc = IsFileOpen(FileName)
Debug.Print ("IsFileOpen reply = " & rc)
If rc Then
OpenFileIfClosed = 1
Else
' erreur à l'ouverture du fichier
OpenFileIfClosed = Err
End If
End If
End Function
Function IsFileOpen(FileName As String) As Boolean
Dim IsWorkBookOpen As Variant
Dim xWb As Workbook
On Error Resume Next
Set xWb = Application.Workbooks.Item(FileName)
IsFileOpen = (Not xWb Is Nothing)
'disable "On Error" (en principe automatique)
On Error GoTo 0
End Function
I would like to know:
- why the syntax Workbooks.Open FileName:=Full_File_Name, Updatelinks:=0 is not working anymore but still working on Excel 2016
- is there a way to analyze why Excel freezes (not sure it has a link with the new syntax?)