How to open SharePoint File from one Excel Workbook and then edit opened file using VBA

Lexcon07

New Member
Joined
Apr 21, 2020
Messages
36
Office Version
  1. 365
Platform
  1. Windows
I am trying to use VBA to open a SharePoint File and write data to it and then save and close the file.

The code below works when it is a traditional file path. By defining the term Destination, it allows me to differentiate between the original workbook that I am copying data from and the "Destination" workbook that I am pasting data to.

VBA Code:
        Dim Destination As Workbook, dPath, Source As Range
        
        dPath = [filepath]
        
        Set Destination = Workbooks.Open(dPath)

I can open the SharePoint file using the following code:

VBA Code:
        Dim Destination As Workbook, dPath, Source As Range
        
            dPath = [sharepointfilepath]
            
            ActiveWorkbook.FollowHyperlink Address:=(dPath)

However, I cannot figure out how to set the term "Destination" to be the opened SharePoint file, so that the rest of my copying and pasting code works. The following code results in a "Compile Error: Expected End of Statement"...

VBA Code:
Set Destination = ActiveWorkbook.FollowHyperlink Address:=(dPath)

I've tried making the newly opened SharePoint File the "Active" Workbook, but it doesn't work with all of the copying and pasting between files.

Any advice or guidance would be greatly appreciated.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Is there a particular reason you are using FollowHyperlink instead of Workbook.Open?

VBA Code:
Dim wb As Workbook
Set wb = Application.Workbooks.Open("https://sharepointcompanyname.sharepoint.com/pathtofile/filename.xlsx")

The documentation is not super clear about this, but it appears that FollowHyperlink does not actually return an object - it just follows a hyperlink. This seems to be confirmed with testing with VarType. VarType(Application.Workbooks.Open(...)) returns 9 (object), while VarType(ThisWorkbook.FollowHyperlink(...)) results in a compile error.
 
Upvote 0
Is there a particular reason you are using FollowHyperlink instead of Workbook.Open?

VBA Code:
Dim wb As Workbook
Set wb = Application.Workbooks.Open("https://sharepointcompanyname.sharepoint.com/pathtofile/filename.xlsx")

The documentation is not super clear about this, but it appears that FollowHyperlink does not actually return an object - it just follows a hyperlink. This seems to be confirmed with testing with VarType. VarType(Application.Workbooks.Open(...)) returns 9 (object), while VarType(ThisWorkbook.FollowHyperlink(...)) results in a compile error.
Hi Severynm,

Thanks for the comment.

Unless I am doing something wrong, the Workbooks.Open function doesn't appear work with a SharePoint file path. It works fine with a traditional file path, but I've tried using a SharePoint (i.e. URL) file path (that works when I use it as a hyperlink), but I get a "Method 'Open' of object 'Workbooks' failed" error. Other than my SharePoint file path being much longer than in your example, I don't see any other differences between the two lines of code.

When I use FollowHyperlink, I can get the file to open, but I can't seem to figure out how to then make it the "Destination" workbook.
 
Upvote 0
How are you finding the SharePoint URL? If you open the file with the desktop app, go to File → Info → Copy Path, and remove the ?web=1 from the end. This is the URL to use with Workbooks.Open.

If you are syncing the SharePoint location to your computer, you can also refer to the C: based path of the file, but there's a couple of things to set up to get that to work correctly; I've learned about the URL method recently and find its a much better solution (for me at least).
 
Upvote 0
Solution
Hi Severynm,

Thanks you again for the reply. It turns out that there is a character limit (255 characters) on hyperlink formulas in Excel/VBA that was creating the error. My SharePoint File Path was longer than the limit. Once I renamed a few folders, it worked fine.
 
Upvote 0

Forum statistics

Threads
1,215,486
Messages
6,125,070
Members
449,205
Latest member
Healthydogs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top