迁移至SharePoint的Excel宏运行报‘路径未找到’,如何修改路径?
原代码使用本地映射盘符路径(S:\local\...),在SharePoint环境下无法识别,需要替换为SharePoint的Web路径或文档库URL。
修改步骤
1. 获取SharePoint文件的有效路径
打开你的SharePoint站点,找到存放PL.txt的文件夹:
- 右键点击
PL.txt选择「复制链接」,复制直接访问的URL(不要选择需要权限验证的共享链接,需确保链接可直接访问文件) - 示例路径格式:
https://your-site.sharepoint.com/sites/YourTeam/Shared%20Documents/DOWNLOADS/data/PL.txt
2. 修改宏代码
移除无效的ChDir语句(Web路径不支持该命令),替换Workbooks.OpenText的文件路径:
Sub PROFLOSTN() ' ' PROFLOSTN Macro ' ' 替换为你实际的SharePoint文件URL Dim spFileUrl As String spFileUrl = "https://your-site.sharepoint.com/sites/YourTeam/Shared%20Documents/DOWNLOADS/data/PL.txt" Workbooks.OpenText Filename:=spFileUrl End Sub
3. 额外注意(如果宏文件也在SharePoint)
如果LIBRO MACROS LB.xlsm同样存储在SharePoint,需确保Application.Run的引用路径正确:
- 同文档库下可使用相对路径拼接:
Sub Proceso_diarioLB() ' PROCESO DIARIO MACRO Dim macroBookPath As String ' 获取当前文件所在的SharePoint文件夹路径,拼接宏文件名称 macroBookPath = ThisWorkbook.Path & "\LIBRO MACROS LB.xlsm" Application.Run "'" & macroBookPath & "'!PROFLOSTN" Application.Run "'" & macroBookPath & "'!STOFCONDN" Application.Run "'" & macroBookPath & "'!COPIASTOFCONDN" End Sub
关键注意事项
- 确保运行宏的用户拥有该SharePoint文件/文档库的读取权限
- 首次运行前需通过浏览器登录SharePoint站点,确保Excel能自动获取身份验证信息
- 路径中的空格需转译为
%20,或直接保留空格(VBA会自动处理)
内容的提问来源于stack exchange,提问作者user14750193
相关产品推荐
相关产品推荐

