How do I upload a file to OneDrive Business (Sharepoint) with VBA - excel

I'm trying to find a method to upload a file to Onedrive from VBA in Excel.
I've done a lot of searching for acceptable methods, but most methods will not work for my scenario or the proposed solution will give an error.
I can upload files just fine if I'm using UNC paths or OneDrive locations that are synced locally (e.g. "C:\Users(username)\OneDrive\File Share") but I need a method that lets me push(upload) files to a shared URL location (e.g. "https://my.sharepoint.com/:f:/r/personal/(email_address)/Documents/SharedFiles?csf=1&e=6WmUIO"). All the users that will need to use the tool will have access to this shared location in OneDrive.
I tried the normal "SaveAs" method in VBA but that won't work.
Set Excelwb = ThisWorkbook
Excelwb.SaveAs fileName:="https://my.sharepoint.com/:f:/r/personal/(email_address)/Documents/SharedFiles?csf=1&e=6WmUIO" _
, FileFormat:=xlOpenXMLWorkbook, ConflictResolution:=xlLocalSessionChanges
Excelwb.Saved = True
Excelwb.Close SaveChanges:=False
Application.DisplayAlerts = True
I expected this to save the file, but I understand that since I'm saving to a URL and not a local file that another method is probably required, but I can't find one that will work.
The error I'm getting is:
Run-time error '1004'
Method 'SaveAs' of object '_Workbook' failed

I just tried this and it worked fine:
Activeworkbook.SaveAs "https://myComany.sharepoint.com/Departments/dept1/Documents/FGH/Text.xlsx"
To get the required path, browse to the destination in IE, then use Library tab >> Open with Explorer to open the destination in Windows Explorer: you can copy the path from the address bar there.

Maybe this will help someone pulling their hair out. Make sure and spaces in your SharePoint URL are replaced by %20. Example "New Folder" should be "New%20Folder, or remove the space from the folder name.

Related

Saving Excel Files to Sharepoint using VBA

I am attempting to run a query in Excel and update the data to Sharepoint (accessible via Teams) on a set schedule.
Desired folder destination:
https://rxsafeway.sharepoint.com/:f:/r/sites/EXELiveProductionTool/Shared%20Documents/General/Portland/Individual%20Performance%20Update?csf=1&web=1&e=oFwj6i
VBA code used:
ThisWorkbook.SaveAs Filename:="https://rxsafeway.sharepoint.com/sites/EXELiveProductionTool/Shared%20Documents/General/Portland/Individual%20Performance%20Update/" & ".xlsm"
Details:
-The above VBA code has been used successfully while connected to VPN and on the company network.
-It works inconsistently... Potentially an upload limit?
-Notice that the "/:f:/r/" has been removed, a "/" has been added after "Update", and everything passed "Update" has been removed in the working code
Question:
Sometimes the code works and sometimes I get a Run-time error '1004': Method 'SaveAs' of object '_Workbook' failed
Is there a syntax error, or a smarter way to save the file?
You probably getting this error due to the MAX_PATH 256 limitation.
Your filename is too long and therefore will have a longer Windows path as well.
I suggest you to view your SharePoint files in File Explorer (see the documentation here). By doing so, every file/folder in your sharepoint will have a normal path and you'll not have anymore to use your file's url.
After that, you can use this formula to save your xlsm in some location in sharepoint :
Path="C:\Users\AKow\Sharepoint_Name\Path_to_your_file"
Filename = "Individual Performance Update"
ThisWorkbook.SaveAs Filename:= Path & Filename, FileFormat:=xlOpenXMLWorkbookMacroEnabled

SaveAs to SharePoint

I am using the following to save a file to s SP folder
wbCO.SaveAs Filename:=strFile, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
The path and file name are all valid and correct so this is not the issue.
strFile = "https://xxx.sharepoint.com/sites/xxx/Shared Documents/Properties/PropertyName/PropertyName_16_Apr.xlsx"
I am getting an odd issue where the file saves with the stated filename but I get a 1004 error saying document not saved.
I have had thsi file working at times and then sometimes it doesn't and I'm guessing it's because it is saving to SP which I always have issues with when using.
I am running this on a Windows platform but it ultimately needs to work on a Mac platform. I know the majority of the code is working but the SaveAs line falls over so help with this would be useful too!
TIA

'Save As' of object '_workbook' failed

I am trying to save a workbook as xlsm, this is the code I use:
ThisWorkbook.SaveAs Filename:="path\workbook_name.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
I have done some testing, and It does in fact save in both a private folder and a public or shared folder
however when I save it in a shared folder I also get this:
Run-time error '1004' Method 'Save As' of object '_workbook' failed
why do I get error message when it is saved in a public/shared folder, or onedrive?
In your string containing path there is variable "path" i suppose. Then you should write it like this:
Dim path as String: path = "C:\Users\xxx\Desktop"
ThisWorkbook.SaveAs Filename:=path & "\workbook_name.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
Or you can hardcode full path (but i recommend using variables - first example).
ThisWorkbook.SaveAs Filename:="C:\Users\xxx\Desktop\file.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
Then when you can save it manually and you have permissions then i have no idea since i normally save files to shared folders and network drives... Maybe your specific path is wrong. It is best to save using servername and folders...
path = "\\servername\folder1\folder2\workbook_name.xlsm"
Because sometimes when you refefer to mapped drive for example X:\folder2 then you must ensure that all users have it mapped in same way (someone uses X someone uses R etc.). And if path is correct then only other way i can think of it is permission issue, but if you can save it manually then i have sadly no idea... :)

Constantly getting Err 1004 when trying to using Application.AddIns.Add

I'm trying to implement a bootstrap installer for my add-ins workbook, such that I can easily install the add-in for new users and send out updates. It works fine on my machine, but when having others test it, I get a runtime error when I try to call Set AI = Application.AddIns.Add(fileName:=fullPath, copyfile:=True). Specifically, the error is "1004: Unable to get the Add property of the AddIns class". I thought this was because the user needed to have "Trust access to the VBA project object model" enabled, but the error seems to occur even after they've toggled that box.
Other things I've checked:
The fullPath to the add-in is valid and the user can access the directory and the file
The user has the folder located at Application.UserLibraryPath
Any ideas?
Figured it out. It appears that the issue isn't one of permissions, but rather of whether a workbook is already open. Opening any workbook before running the Addins.Add prevented the error from occurring so I've simply added that into the program:
If Application.Workbooks.Count = 0 then Set wb = Application.Workbooks.Add()
Set AI = Application.AddIns.Add(fileName:=fullPath, copyfile:=True)
If not wb is nothing then wb.Close
Duke, perhaps it's the Trust Center settings on the recipients' machines. I have found this and may be helpful.
Best,
Danny
Check out VBA videos on ExcelVBADude on YouTube.

How to open Locked for Editing file as Read Only?

I have a macro that opens multiple files. If it comes to a file "Locked for Editing" it will give me an error saying
FileName is currently in use. Try again later.
How can I make it open said file as read only? I tried:
Workbooks.Open FileName:=Selected_EOS_Report_File, ReadOnly:=True
and
Workbooks.Open FileName:=Selected_EOS_Report_File, ReadOnly:=True, IgnoreReadOnlyRecommended:=True
Update: The first method does work. My code runs on multiple files that pass through the "Selected_EOS_Report_File" variable. At some point a file passed through that was an Excel temp file (begins the filename with "~$"). I created an if/then statement to skip over any such files.
As far as I know, you need Notify:= True
MSDN link
Notify
If the file cannot be opened in read/write mode, this argument is True
to add the file to the file notification list. Microsoft Excel will
open the file as read-only, poll the file notification list, and then
notify the user when the file becomes available. If this argument is
False or omitted, no notification is requested, and any attempts to
open an unavailable file will fail.
The code below worked for a similar Problem I had. This will set the ReadOnly and IgnoreReadOnlyRecommended parameters.
I tested this for Excel 365.
ReadOnly: True to open the workbook in read-only mode.
IgnoreReadOnlyRecommended: True to have Microsoft Excel not display the read-only recommended message (if the workbook was saved with the Read-Only Recommended option).
dim wbReadOnly as Workbook
Set wbReadOnly = Workbooks.Open(strXLSFileName, , True, , , , True)
link to VBA Documentation
Try this?
Dim wb As Workbook
Set wb = GetObject(Selected_EOS_Report_File)
wb.Open 'ReadOnly:=True (removed the readonly part)
Derived from this post: Opening .xlsx with VBA, File in Use error. Read-only not working
edit
A post here indicates a similar issue for older versions, and that if you undate to xlsx then it goes away:
https://social.technet.microsoft.com/Forums/en-US/5c9f7444-a2c7-4598-beca-21a6d5575d94/excel-file-currently-in-use

Resources