Jugulator
8/12/2018 - 12:12 PM

Get Workbook

Function GetWorkbook(ByVal sFullName As String) As Workbook
'   Description: checks if workbook is open, opens if not
'   Inputs:      path and filename (string)
'   Outputs:     workbook object if path/filename was valid, otherwise Nothing

    Dim sFile As String
    sFile = Dir(sFullName)

    On Error Resume Next
        Dim wbReturn As Workbook
        Set wbReturn = Workbooks(sFile)
        
        If wbReturn Is Nothing Then
            Set wbReturn = Workbooks.Open(sFullName)
        End If
    On Error GoTo 0
    
    'RETURN
    Set GetWorkbook = wbReturn

End Function


''
' Checks if workbook is open, opens if not.
'
' @param ExcelInstance      Application instance
' @param sFullName          path and filename (string)
' @param pwd                password to open file
' @param ReadOnly           true if read-only workbook is needed
' @return:                  workbook object if path/filename was valid, otherwise Nothing
Public Function GetWorkbook(ByRef ExcelInstance As Excel.Application, ByVal sFullName As String, _
                             ByVal pwd As String, ByVal ReadOnly As Boolean) As Workbook

    Dim sFile As String
    sFile = Dir(sFullName)

    On Error Resume Next
        Dim wbReturn As Workbook
        Set wbReturn = Workbooks(sFile)

        If wbReturn Is Nothing Then
            Set wbReturn = ExcelInstance.Workbooks.Open(filename:=sFullName, Password:=pwd, ReadOnly:=ReadOnly)
        End If
    On Error GoTo 0

    'RETURN
    Set GetWorkbook = wbReturn

End Function