无法使用 VBA 将文件保存到 Sharepoint Online(权限错误)
这是我第一次使用 VBA.我有一个命令按钮,应该将文件另存为我的 sharepoint 在线文档页面.我可以使用excel 服务"并手动从 excel 保存到该文档页面,但是当我在 VBA 中尝试相同时,它说我没有权限.以下是我正在使用的代码,任何建议将不胜感激!
This is one of my first times using VBA. I have a command button that is supposed to be saving a file as, to my sharepoint online documents page. I can use the "excel services" and save to that documents page from excel manually, but when i try the same in VBA it is saying I do not have permission. Below is the code I am using, any advice would be much appreciated!
Private Sub CommandButton1_Click()
Dim path as string
dim filename1 as string
path = "https://xxxxxx.sharepoint.com/sites/xxxxx/Shared Documents"
filename1 = Range("B3").text
activeworkbook.SaveAs FileName:=path & filename1 & ".xlsx", _
FileFormat:=xlopenxmlworkbook
End Sub
如果当前文件与要保存的目标位于同一文件夹中,请尝试对 path
的定义进行此更改>:
If your current file is in the same folder as the destination you would like to save in, try this change for the definition of path
:
path = "https://xxxxxx.sharepoint.com/sites/xxxxx/Shared Documents/"
代码中缺少最后的 /
导致 FileName
参数无效,因为 path &文件名1 &.xlsx"
评估为
The missing final /
in your code is causing the FileName
argument to be invalid, because path & filename1 & ".xlsx"
evaluates to
https://xxxxxx.sharepoint.com/sites/xxxxx/Shared Documents[filename1].xlsx
这意味着如果在 /xxxxx
文件夹中没有限制权限,您就会在该位置编写一个名称不正确的 Excel 工作簿.
Which means if permissions weren't restricted on the /xxxxx
folder you would have written a badly named Excel workbook in that location.
替代解决方案
可能是您问题的另一种解决方案.在您希望保存新 Excel 文件的位置保存一个空工作簿.打开空的 Excel 文件,然后在那里运行宏.将 path
行更改为:
Potentially another solution for your problem. Save an empty workbook in the location you wish to save your new Excel files. Open the empty Excel file, and run your macro there. Change the path
line to:
path = ActiveWorkbook.Path & "\"
试试这个,看看它是否有效.这就是我解决 Sharepoint 权限问题的方法.
Try this to see if it works instead. This is how I got around Sharepoint permissions problems.