Is there a best practice for opening an excel workbook? I have this function
Function openWorkbook(workbookpath As String, Optional worksheetname As String)
If IsMissing(worksheetname) Then worksheetname = "Sheet1"
'Test workbookpath
Dim wb As Workbook
Dim ws As Worksheet
Dim fso As Scripting.FileSystemObject
Dim wbname As String
wbname = fso.GetFileName(workbookpath)
Dim thisIsSoStupid As Boolean
thisIsSoStupid = True
'test if workbook is open
For Each wb In Workbooks
If wb.Name = wbname Then
Set openWorkbook = wb
thisIsSoStupid = False
break
End If
Next
If thisIsSoStupid And Dir(workbookpath) <> 0 Then
openWorkbook = Workbooks.Open(workbookpath)
Else
openWorkbook = False
End If
End Function
I'd like to do something like g = openWorkBook(path)||false
which is a javascript shortcut or $g = fopen($path, 'w')
or die('bugger');
but of course, if it can't find the file, it will return false and I can't evaluate a false if my code is expecting an object.
Do I have to on error resume and then catch the error? I hate the stupid error crap. It's an invitation to spaghetti code