VBA 检查 Sharepoint 文件夹是否存在

问题描述 投票:0回答:2

我正在尝试使用 URL 路径确定 Excel VBA 中是否存在 Sharepoint 文件夹,如果不存在则创建该文件夹。如果我映射网络驱动器,我可以轻松做到这一点:

 myWorkbookBasePath = "Z:Documents\Reports\2013\"

 If Dir(myWorkbookBasePath, vbDirectory) = "" Then
        MkDir myWorkbookBasePath
 End If

但是,我不知道如何使用 URL 路径来做到这一点。如果我使用

myWorkBookBasePath= "http://sharepoint/Documents/Reports/2013/"

我收到错误代码 52。谁能告诉我如何使其与 URL 路径一起使用?

excel vba sharepoint
2个回答
5
投票

尝试一下

    myWorkBookBasePath= "\\sharepoint\Documents\Reports\2013\"

    myWorkBookBasePath = "http://sharepoint/Documents/Reports/2013/"
    myWorkBookBasePath = Replace(Replace(myWorkBookBasePath, "http:", ""), "/", "\")
    MsgBox (myWorkBookBasePath)

如果是使用

https

托管的 Sharepoint 网站
    myWorkBookBasePath = "https://sharepoint/Documents/Reports/2013/"
    myWorkBookBasePath = Replace(Replace(myWorkBookBasePath, "https:", ""), "/", "\")
    myWorkBookBasePath = Replace(myWorkBookBasePath, Split(myWorkBookBasePath, "\")(2), Split(myWorkBookBasePath, "\")(2) & "@SSL")
    MsgBox (myWorkBookBasePath)

VBA 中的 MkDir 只能访问文件系统并且不理解 URL,因此您可以在资源管理器中打开的任何内容都可以使用 MkDir 访问。


0
投票

我也遇到了同样的问题,Adrian 的方法对我不起作用。

我在 Dighi 的评论中找到了解决方案here。它将文件夹下载到 TEMP 并通过 Windows API 检查文件是否存在。

从 Microsoft 论坛复制的代码(Dighi 的帖子),略有更改(弹出窗口立即打印):

Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _
    Alias "URLDownloadToFileA" (ByVal pCaller As LongPtr, ByVal szURL As String, _
    ByVal szFileName As String, ByVal dwReserved As LongPtr, ByVal lpfnCB As LongPtr) As LongPtr

Sub CheckSharePointFile()
    Dim fileURL As String
    Dim tempFile As String
    Dim result As LongPtr
    
' Set the file URL and temporary file path
    fileURL = "https://ABC.sharepoint.com/sites/Johnson/Macros/MyFile.csv"
    tempFile = Environ("TEMP") & "\MyFile.csv"
    
' Attempt to download the file
    result = URLDownloadToFile(0, fileURL, tempFile, 0, 0)
    
' Check if the download was successful
    If result = 0 Then
        debug.Print "File exists!"
    Else
        debug.Print "Error: File not found!"
    End If
    
' Delete the temporary file
    On Error Resume Next
    Kill tempFile
    On Error GoTo 0
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.