简体   繁体   English

使用 Excel VBA 打开 OneDrive 上的文件

[英]Open a file on OneDrive using Excel VBA

I have an Excel file (File #1) in which I would like the VBA code to open a file on my OneDrive (File #2).我有一个 Excel 文件(文件 #1),我希望使用 VBA 代码在我的 OneDrive 上打开一个文件(文件 #2)。

Many people will use File #1, so I want it to open File #2 in the background.许多人会使用 File #1,所以我希望它在后台打开 File #2。

I have created a shared link for File #2.我为文件 #2 创建了一个共享链接。 With this link anyone should be able to access the file.通过此链接,任何人都应该能够访问该文件。

Link: https://1drv.ms/x/s?AmdCer05hKV4geAnbDaOSX9UrTfLHQ?e=5Pm7dL链接: https://1drv.ms/x/s?AmdCer05hKV4geAnbDaOSX9UrTfLHQ?e=5Pm7dL

How do I open this File #2 via VBA code in File #1?如何通过文件 #1 中的 VBA 代码打开文件 #2? I want it to open in Excel, not Excel on-line .我想让它在 Excel 打开,而不是 Excel 在线

I used this code.我使用了这段代码。

Set wb = Workbooks.Open("https://1drv.ms/x/s!AmdCer05hKV4geAnbDaOSX9UrTfLHQ?e=5Pm7dL")

When this line of code runs, Excel shuts down and a window opens stating Microsoft Error Reporting.当这行代码运行时,Excel 关闭并打开 window,说明 Microsoft 错误报告。

It then says然后它说

"There was a problem with Microsoft Excel and we apologize for the inconvenience. Microsoft Excel will attempt to recover your work, but recent changes might be lost." “Microsoft Excel 出现问题,对于给您带来的不便,我们深表歉意。Microsoft Excel 将尝试恢复您的工作,但最近的更改可能会丢失。”

Try the following:尝试以下操作:

Sub open_excel_from_one_drive()

sfilename = "https://1drv.ms/x/s!AmdCer05hKV4geAnbDaOSX9UrTfLHQ?e=5Pm7dL"
Set xl = CreateObject("Excel.Sheet")
Set xlsheet = xl.Application.Workbooks.Open(Filename:=sfilename, ReadOnly:=True)

End sub

The above code works fine for me, but maybe you should change your OneDrive path to be something like that: " https://yourcorp.sharepoint.com/sites/yoursite/Shared%20Documents/1stfolder/myfile.xlsx "上面的代码对我来说很好,但也许你应该将你的 OneDrive 路径更改为:“ https://yourcorp.sharepoint.com/sites/yoursite/Shared%20Documents/1stfolder/myfile.xlsx

I hope it helps:)我希望它有帮助:)

I ended up using the below since no other answer on here worked.我最终使用了以下内容,因为这里没有其他答案。

ActiveWorkbook.FollowHyperlink Address:="https://1drv.ms/x/s?AmdCer05hKV4geAnbDaOSX9UrTfLHQ?e=5Pm7dL"

  • Go to the sharepoint site where it is saved. Go 到保存它的 sharepoint 站点。 Example:例子:
    https://dieboldnixdorf-my.sharepoint.com/ https://dieboldnixdorf-my.sharepoint.com/
  • Locate the file Select the 3 dots next to the file找到文件 Select 文件旁边的 3 个点
  • Select Details Select 详细信息
  • Scroll down to the "Path" section向下滚动到“路径”部分
  • Select the copy option Select 复制选项
  • This will copy a clear path to the file.这将复制一个清晰的文件路径。

If I open it from VBA using the 1drv.ms/... link that it gives to me when I "Share" it, it asks me for a Microsoft login/password that is not my usual Microsoft login.如果我使用 1drv.ms/... 链接从 VBA 打开它,当我“分享”它时,它会要求我提供一个 Microsoft 登录名/密码,这不是我通常的 Microsoft 登录名。 I never found the right password and it's not recoverable.我从未找到正确的密码,而且无法恢复。

I solved by using the https://d.docs.live.net/.. .我通过使用https://d.docs.live.net/..解决了。 URL I obtain with a?ThisWorkbook.fullname in the VBA window. This works without asking a login/password, likely using the account used by the Excel application. URL 我在 VBA window 中使用 a?ThisWorkbook.fullname 获得。这无需询问登录名/密码即可工作,可能使用 Excel 应用程序使用的帐户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM