I'm trying to open an Excel file from SharePoint using VBA. Because the file I'm looking for might be different each time I run the macro, I want to be able to view the SharePoint folder and select the file I need.
The code below works fine when I want to look for a file on a network drive, however when I replace that with a SharePoint address I get "run-time error 76: Path not found".
Sub Update_monthly_summary()
Dim SummaryWB As Workbook
Dim SummaryFileName As Variant
ChDir "http://sharepoint/my/file/path"
SummaryFileName = Application.GetOpenFilename("Excel-files,*.xls", _
1, "Select monthly summary file", , False)
If SummaryFileName = False Then Exit Sub
Set SummaryWB = Workbooks.Open(SummaryFileName)
End Sub
When I paste this address into Windows Explorer I have no problems accessing the SharePoint folder, so I know the path is correct.
Why doesn't VBA like it?
There are several possible reasons: The file name or path does not exist. The file is being used by another program. The workbook you are trying to save has the same name as a currently open workbook.
Open the Excel file in the browser, then select Info to click Open in Desktop App. The Excel file will be opened in Desktop App, select Open to copy path to clipboard. Remove the ?web=1 string at the end of the link so that Power BI Desktop can properly navigate to your file, and then select OK.
Select Open in Desktop App at the top of your workbook. If you don't see it, there should be a search bar along the top of your workbook. In that search bar, type open, and then select Open in Desktop App.
Try this code to pick a file from a SharePoint site:
Dim SummaryWB As Workbook
Dim vrtSelectedItem As Variant
With Application.FileDialog(msoFileDialogOpen)
.InitialFileName = "https://sharepoint.com/team/folder" & "\"
.AllowMultiSelect = False
.Show
For Each vrtSelectedItem In .SelectedItems
Set SummaryWB = Workbooks.Open(vrtSelectedItem)
Next
End With
If SummaryWB Is Nothing then Exit Sub
If I remember correctly, the Microsoft Scripting Runtime
reference must be enabled. Also, your site may use backslashes, mine uses forward slashes.
I transform the URL into a WebDAV address using the following function I created. This function also returns regular system paths and UNC paths unscathed.
Call this function by adding it into a module in your VBA project and entering MyNewPathString = Parse_Resource(myFileDialogStringVariable)
just after your file dialog command and before using the path selected by the file dialog. Then reference "MyNewPathString" when using the target file location.
Public Function Parse_Resource(URL As String)
'Uncomment the below line to test locally without calling the function & remove argument above
'Dim URL As String
Dim SplitURL() As String
Dim i As Integer
Dim WebDAVURI As String
'Check for a double forward slash in the resource path. This will indicate a URL
If Not InStr(1, URL, "//", vbBinaryCompare) = 0 Then
'Split the URL into an array so it can be analyzed & reused
SplitURL = Split(URL, "/", , vbBinaryCompare)
'URL has been found so prep the WebDAVURI string
WebDAVURI = "\\"
'Check if the URL is secure
If SplitURL(0) = "https:" Then
'The code iterates through the array excluding unneeded components of the URL
For i = 0 To UBound(SplitURL)
If Not SplitURL(i) = "" Then
Select Case i
Case 0
'Do nothing because we do not need the HTTPS element
Case 1
'Do nothing because this array slot is empty
Case 2
'This should be the root URL of the site. Add @ssl to the WebDAVURI
WebDAVURI = WebDAVURI & SplitURL(i) & "@ssl"
Case Else
'Append URI components and build string
WebDAVURI = WebDAVURI & "\" & SplitURL(i)
End Select
End If
Next i
Else
'URL is not secure
For i = 0 To UBound(SplitURL)
'The code iterates through the array excluding unneeded components of the URL
If Not SplitURL(i) = "" Then
Select Case i
Case 0
'Do nothing because we do not need the HTTPS element
Case 1
'Do nothing because this array slot is empty
Case 2
'This should be the root URL of the site. Does not require an additional slash
WebDAVURI = WebDAVURI & SplitURL(i)
Case Else
'Append URI components and build string
WebDAVURI = WebDAVURI & "\" & SplitURL(i)
End Select
End If
Next i
End If
'Set the Parse_Resource value to WebDAVURI
Parse_Resource = WebDAVURI
Else
'There was no double forward slash so return system path as is
Parse_Resource = URL
End If
End Function
This function will check if your file path is a URL and if it is secure (HTTPS) or not secure (HTTP). If it is a URL then it will build the appropriate WebDAV string so that you can link directly to the target file in SharePoint.
The user will likely be prompted for credentials each time the file is opened especially if they are not sitting on the same domain as your SharePoint farm.
PLEASE NOTE: I have not tested this with an http site, however I am confident that it will work.
From you script do not use http://sharepoint/my/file
as path but rather
\\sharepoint\my\file
and then is should work. It works fo my programs done in C#.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With