【问题标题】:Creating text file on OneDrive from Excel VBA从 Excel VBA 在 OneDrive 上创建文本文件
【发布时间】:2021-03-13 23:40:25
【问题描述】:

我需要使用活动工作簿的名称创建一个文本文件,但扩展名为 .txt。我使用以下代码:

Public Sub textfile()
Dim fso As Variant, ts As Variant
Dim tempFileName As String
   Set fso = CreateObject("Scripting.FileSystemObject")
   tempFileName = ActiveWorkbook.FullName + ".txt"
   Debug.Print tempFileName
   Set ts = fso.CreateTextFile(tempFileName)
   ts.WriteLine "Hello"
   ts.Close
End Sub

使用本地文件,它可以完美运行。但如果文件在 OneDrive 上,则会给出错误 52“错误的文件名或编号”。文件路径为:

xxx-my.sharepoint.com/personal/yyy_xxx_onmicrosoft_com/Documents/test.xlsx.txt

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

【问题讨论】:

    标签: excel vba onedrive


    【解决方案1】:

    文件系统对象比 onedrive 旧得多,它不理解它的路径。但是,如果您将 onedrive 文件夹同步到本地文件系统,例如:c:\users\name\onedrive... 您可以改用此本地路径,它将同步回 onedrive。

    还有其他方法可以使用 onedrive api 将文件上传到 onedrive。然而,这些要复杂得多,需要一些身份验证知识 (https://docs.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/authentication?view=odsp-graph-online)。 如果你能做到,请看这里:https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_put_content?view=odsp-graph-online

    【讨论】:

    • 感谢您的评论。同时我发现像 ActiveWorkbook.SaveAs Filename:= _ tempFileName, FileFormat:= _ xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False 这样的命令可以使用这些 xxx-my.sharepoint.com 路径访问 OneDrive。但它会创建 XLSX 文件。可惜他们没有提供与 CreateTextFile 类似的功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多