VBA:从保存在本地的 Excel 文件打开放置在 SharePoint 中的 .csv 或 .txt 文件时出现问题

VBA : Problem opening .csv or .txt files placed in SharePoint from Excel file saved on local

我正在尝试通过 Excel 本地笔记本电脑上存储的文件中的 VBA 函数访问存储在公司共享点中的 .csv 文件:

FilePath = "https://[company].sharepoint.com/Shared%20Documents/[folder]/testCSV.csv"
Open FilePath For Input As #1

如果 .csv 文件本地保存在我的笔记本电脑中,我编写的函数工作正常,但在尝试通过 SharePoint 时出现此错误:

Run-Time Error '52':
Bad file name or number

有趣的是,我实际上可以使用以下代码打开与 Excel 工作簿相同的 .csv 文件:

Dim FilePath As String
FilePath = "https://[company].sharepoint.com/Shared%20Documents/[folder]/testCSV.csv"
Dim wb As Workbook
Set wb = Workbooks.Open(FilePath, , , 2)

我也尝试过使用以下代码打开 .txt 文件:

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("https://[company].sharepoint.com/Shared%20Documents/[folder]/testfile.txt", ForAppending)
f.Write "Hello world!"
f.Close

如果文件保存在我的电脑上,但如果保存在共享点上,则不能正常工作

关于如何解决问题的任何想法?

提前致谢

首先下载文件确实解决了那个特定错误:

'functions for downloading
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, _
ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Function DownloadFileFromWeb(strURL As String, strSavePath As String) As Long 'strSavePath includes filename
DownloadFileFromWeb = URLDownloadToFile(0, strURL, strSavePath, 0, 0)
End Function


DownloadFileFromWeb(webPath, localPath)

希望这对您有所帮助