我越来越清楚,VBA不能很好地处理Sharepoint/OneDrive的文件操作。我有以下代码:
Sub TestSaves()
Dim fName, fNameNoEx, SaveName As String
Dim fldr As FileDialog
CurrPath = ActiveDocument.Path
'CurrPath = "C:\temp"
If Left(CurrPath, 4) = "http" Then
PathSep = "/"
Else
PathSep = "\"
End If
FileNumber = FreeFile
OutputFile = CurrPath & PathSep & "fred.txt"
MsgBox OutputFile
Open OutputFile For Output As #FileNumber
Print #FileNumber, "Hello World"
Close #FileNumber
End Sub当我的CurrPath = ActiveDocument.Path处于活动状态时,我得到一个运行时错误52 (错误的文件名或编号)。在本例中,CurrPath如下所示:

当我的CurrPath = "C:\temp"处于活动状态时,它工作得很好。
我希望能够将另一个文件保存在与原始文档相同的位置,无论该文件是本地文件还是OneDrive/Sharepoint上的文件。有什么建议吗?
发布于 2021-10-13 10:17:47
我唯一能弄明白怎么做的方法是:
Environ("temp"))Documents.Open filename:=TmpFileActiveDocument.SaveAs2 filename:=OutputFile, FileFormat:=wdFormatText重新打开文件,然后再次关闭该文件。有点笨拙,当.txt打开、保存和关闭时,我在处理结束时会看到屏幕闪烁(是的,我确实尝试在打开时设置visible:=false,但这也会影响ActiveDocument)。
But...it起作用了。
https://stackoverflow.com/questions/69539245
复制相似问题