【问题标题】:VBA and Sharepoint filesVBA 和 Sharepoint 文件
【发布时间】:2021-10-12 10:49:28
【问题描述】:

我越来越清楚 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 上。有什么建议吗?

【问题讨论】:

  • 不确定,但我猜Open 方法只能从文件系统打开文件,而不能从 sharpoint/https。
  • @Pᴇʜ 那么我可以使用其他方法吗?
  • 这只是一个猜测,也许有人可以证实这一点,或者你可以做一些研究来证实这一点。但无论如何,您可以将文件从 SharePoint 签出到本地临时文件夹,使用您的 VBA 代码和upload it again to sharepoint 对其进行编辑。 • 或者Map a network drive to a SharePoint library 可以解决您的问题。

标签: vba sharepoint save onedrive


【解决方案1】:

我能弄清楚如何做到这一点的唯一方法是:

  1. 将我的输出文件保存到非同步位置(在我的情况下为Environ("temp")
  2. 当我写完它并完成Close #FileNumber 之后,我从临时文件夹Documents.Open filename:=TmpFile 重新打开我的文件
  3. 然后我在再次关闭文件之前执行ActiveDocument.SaveAs2 filename:=OutputFile, FileFormat:=wdFormatText

有点笨拙,当 .txt 打开、保存和关闭时,我最终会在处理结束时出现屏幕闪烁(是的,我确实尝试在打开时设置 visible:=false,但这也会影响 ActiveDocument) .

但是……它有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 2020-01-15
    相关资源
    最近更新 更多