I'm trying to use the following VBA code snippet to open an Excel file as a data source:
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & path & "\" & VFile & ";" & _
"Extended Properties=""Excel 8.0; HDR=No; IMEX=1;"""
.Open
End With
This works as expected when the path
variable is of the type C:\Folder
. However, if I want to reference an excel file in the same folder as the current file, and use
path = ThisWorkbook.path
where the current workbook is in a folder stored on SharePoint, path
will then contain something of the form
https://sp.foobar.com/folder
Trying to connect using this path results in a Method 'Open' failed
error. Hacking the path into a WebDAV path tells me that the file is either locked or I do not have permission to read it.
Can anyone help?
When I manually set path
to the Drive:\Folder
form, all is well, but I can't automatically generate this path using ThisWorkbook.path
or similar if the file is stored on SharePoint, and I'd like the solution to be 'portable' in the sense of working anywhere on a drive so long as all the relevant files are in the same folder.