【问题标题】:Search for a file on Sharepoint and return filename在 Sharepoint 上搜索文件并返回文件名
【发布时间】:2014-12-07 22:49:15
【问题描述】:

我一直在使用以下代码检查 SharePoint 网站上是否存在文件:

Function URLExists(url As String) As Boolean
    Dim oXHTTP As Object
    Set oXHTTP = CreateObject("MSXML2.XMLHTTP")
    If Not UCase(url) Like "HTTP:*" Then
        url = "http://" & url
    End If
    On Error GoTo haveError
        oXHTTP.Open "HEAD", url, False
        oXHTTP.send
        URLExists = IIf(oXHTTP.Status = 200, True, False)
    Exit Function

haveError:
        URLExists = False End Function

现在的问题是我以前下载的文件现在格式如下:

old url = teams.sharepoint.xyz.com\Daily Report - DDMMYYYY.XLS
new url = teams.sharepoint.xyz.com\Daily Report - v2 YYYY-MM-DD-HH-MM-SS.XLS.XLS

我希望能够从服务器获取最新文件,但我不确定如何使用通配符来实现。它曾经可以很好地处理旧网址,因为我可以轻松地格式化日期,但现在新网址已经添加了时间,我无法找到一种使用通配符搜索来搜索 SharePoint 网站的方法.

【问题讨论】:

标签: vba excel sharepoint


【解决方案1】:

我想我现在已经解决了:

   Function GetFullFileName(strfilepath As String, strFileNamePartial As String) As String
      Dim objFS As Variant
      Dim objFolder As Variant
      Dim objFile As Variant
      Dim intLengthOfPartialName As Integer
      Dim strfilenamefull As String

      Set objFS = CreateObject("Scripting.FileSystemObject")
      Set objFolder = objFS.GetFolder(strfilepath)

      'work out how long the partial file name is intLengthOfPartialName = Len(strFileNamePartial)

      'Instead of specifying the starting characters of the file you can directly loop through all files in the folder
      For Each objFile In objFolder.Files 

         'Test to see if the file matches the partial file name
         If Left(objFile.Name, intLengthOfPartialName) = strFileNamePartial Then
            'get the full file name
            strfilenamefull = objFile.Name
            Exit For
         Else

         End If
      Next objFile

      Set objFolder = Nothing
      Set objFS = Nothing

      'Return the full file name as the function's value
      GetFullFileName = strfilenamefull

   End Function

   Function URLExists(url As String) As Boolean
      Dim oXHTTP As Object
      Set oXHTTP = CreateObject("MSXML2.XMLHTTP")
      If Not UCase(url) Like "HTTP:*" Then
         url = "http://" & url
      End If
      On Error GoTo haveError
      oXHTTP.Open "HEAD", url, False
      oXHTTP.send
      URLExists = IIf(oXHTTP.Status = 200, True, False)
   Exit Function

   haveError:
      URLExists = False 
   End Function

然后在main函数中使用如下代码:

PrtFileName = "\\sharepointsite.com\path to folder"
PrtFileName2 = "sharepointsite.com/path to folder"
' ---------------------------------------
' Check source file exists using a loop
' to keep going back until a valid file
' is found within last 7 days.
' ---------------------------------------
Dim fileExists, a As Boolean
fileExists = False
Dim dateOffset As Integer
dateOffset = 0

Do While ((fileExists = False) And (dateOffset < 14))
    FileDate = "Daily Report - Remedy v2 " + Format(Now() - dateOffset, "YYYY-MM-DD")

    Filename1 = GetFullFileName(PrtFileName, FileDate)
    MsgBox PrtFileName + "\" + Filename1

    a = URLExists(PrtFileName2 + "/" + Filename1)

    If a = True Then
        'FileDate = Now()
        Filename = PrtFileName + "\" + Filename1
        MsgBox Filename
        fileExists = True
    Else
        a = False
        fileExists = False
        dateOffset = dateOffset + 1
    End If
Loop

像魅力一样工作。要打开 excel 工作簿,我使用以下命令:

Dim wb2 As Workbook
Set wb2 = Workbooks.Open(Filename)

如果有人可以让它更高效,那就更好了,但它暂时对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    相关资源
    最近更新 更多