2014-12-07 36 views
0

使用下面的代碼來檢查文件是否在SharePoint網站上存在我一直:搜索在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網站,也許,通配符搜索。

+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 – 2014-12-07 22:53:25

回答

0

我想我有它已經摸索出:

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 

然後用下面的代碼中的主要功能:

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) 

如果有人能使其更有效率,然後還要好,但它的工作對我的時刻。