The workbook I'm using is Workbook Y - one of several workbooks within a given tool. The macro in question is designed to extract data from Workbook X into Workbook Y. The following is an error handler within that macro that is designed to physically open Workbook X if normal extraction of data into Workbook Y does not succeed. My question is, how does it accomplish this if the filepath of Workbook X is not explicitly mentioned?
The code calls upon the function 'SafeOpenFile', which is here:
Again, SafeOpenFile does not specify the path of Workbook X. I think I'm missing something really obvious here, like I need to look at the definition of an additional function that contains the filepath. Can anyone identify something in the code that is present in place of a filepath? It might be the missing link.
I know this is a long post, so thanks a lot in advance! :D
Code:
'Open Workbook X if necessary
On Error Resume Next
Set wbook = Workbooks(sHistFile)
On Error GoTo 0
IsOpen = False
If wbook Is Nothing Then
'open the file if not already open
sHistFile = SafeOpenFile(ActiveWorkbook.Path & "\" & sHistFile)
Else
'make a note so we don't close Workbook X.
IsOpen = True
End If
The code calls upon the function 'SafeOpenFile', which is here:
Code:
Function SafeOpenFile(sFilePath As String)
'===============================================================
' Opens a file, and offers a dialog box to choose one you find yourelf if it can't find them
Dim sFoundFile As String
Dim z As Variant
sFoundFile = Dir(sFilePath) 'Find file
'Check that it finds a file, if not asks to specify
If Len(sFoundFile) = 0 Then
z = MsgBox("File " & sFilePath & " not found, do you want to manually specify?", vbYesNo)
If z = vbYes Then
sFoundFile = Application.GetOpenFilename("Stocklists (*.xls; *.xlsx; *.csv),*.xls,*.xlsx,*.csv", , "Specify the KB Stock List...")
Workbooks.Open Filename:=sFoundFile, ReadOnly:=True 'Open it Read Only
sFoundFile = Right(sFoundFile, Len(sFoundFile) - InStrRev(sFoundFile, "\")) 'trims the filepath off so we can use the name to reference it
Else
SafeOpenFile = "" 'Returns a zero length string on fail
Exit Function
End If
Else
Workbooks.Open Filename:=sFilePath, ReadOnly:=True 'Open it Read Only
End If
SafeOpenFile = sFoundFile 'Returns the name of the file to use
End Function
Again, SafeOpenFile does not specify the path of Workbook X. I think I'm missing something really obvious here, like I need to look at the definition of an additional function that contains the filepath. Can anyone identify something in the code that is present in place of a filepath? It might be the missing link.
I know this is a long post, so thanks a lot in advance! :D