Skip to content

Instantly share code, notes, and snippets.

@ndthanh
Created December 31, 2018 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndthanh/c8277d80255238050de770806b8c9066 to your computer and use it in GitHub Desktop.
Save ndthanh/c8277d80255238050de770806b8c9066 to your computer and use it in GitHub Desktop.
Function GetConnXLS(ByVal cFileName As String, _
Optional ByVal InformErrMSG As Boolean = False)
On Error GoTo errHandling:
'Open ADO connection to excel workbook
Dim oConn As Object
Dim Ext As String, ConnStr As String
Set oConn = CreateObject("ADODB.Connection")
ConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & cFileName & ";" & _
"Extended Properties=""Excel 12.0;HDR=Yes;"";"
oConn.Open ConnStr
Set GetConnXLS = oConn
errHandling:
If Err.Number <> 0 Then
Set oConn = Nothing
If InformErrMSG Then
MsgBox "GetConnXLS" & ": " & Err.Number & " " & Err.Description, vbCritical
End If
End If
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment