3

继这项出色的工作之后,请点击此处: 将文件批量复制到 SharePoint 网站

我现在可以通过单击按钮将我的压缩文件上传到 Sharepoint。

我现在的问题是:如何删除使用相同方法上传的文件?

我稍微修改了代码以将不同的文件保存到不同的 SharePoint 文件夹。下面的示例:

 
Public Sub CopyToSharePoint()

Dim xmlhttp Dim sharepointUrl Dim sharepointFolder Dim sharepointFileName Dim LstrFileName, strFilePath, strMonthYear, PstrFullfileName, PstrTargetURL As String Dim LlFileLength As Long Dim Lvarbin() As Byte Dim LvarBinData As Variant Dim fso, LobjXML As Object Set fso = CreateObject("Scripting.FileSystemObject") Dim fldr As folder Dim f As File

'Parent Sharepoint sharepointUrl = "[SHAREPOINT PATH HERE]"

'Sets the Month%20Year strMonthYear = Format(Now(), "mmmm yyyy") & "\"

'File Path strFilePath = "[ARCHIVE DRIVE]" & strMonthYear

'Check to see if DRA for current month%20year exists If Len(Dir(strFilePath, vbDirectory)) = 0 Then MkDir "strFilePath" End If

Set LobjXML = CreateObject("Microsoft.XMLHTTP")

'Where we're uploading files from Set fldr = fso.GetFolder(strFilePath)

For Each f In fldr.Files

If Format(f.DateCreated, "dd/mm/yyyy") = Format(Now(), "dd/mm/yyyy") Then

If InStr(1, f.Name, "[FILESTRING1]", vbTextCompare) > 0 Then
sharepointFolder = "[SHAREPOINTSTRING1]/"
    ElseIf InStr(1, f.Name, "[FILESTRING2]", vbTextCompare) > 0 Then
    sharepointFolder = "[SHAREPOINTSTRING2]"
        ElseIf InStr(1, f.Name, "[DONOTUPLOADTHISFILE]", vbTextCompare) > 0 Then
        GoTo NextF:
            Else
            sharepointFolder = "[SHAREPOINTMAINFOLDER]"
End If

sharepointFileName = sharepointUrl & sharepointFolder & f.Name

PstrFullfileName = strFilePath & f.Name
LlFileLength = FileLen(PstrFullfileName) - 1

' Read the file into a byte array.
ReDim Lvarbin(LlFileLength)
Open PstrFullfileName For Binary As #1
Get #1, , Lvarbin
Close #1

' Convert to variant to PUT.
LvarBinData = Lvarbin
PstrTargetURL = sharepointUrl & sharepointFolder & f.Name

' Put the data to the server, false means synchronous.
LobjXML.Open "PUT", PstrTargetURL, False

' Send the file in. LobjXML.Send LvarBinData

End If

NextF: Next f

Set LobjXML = Nothing Set fso = Nothing

End Sub

4

1 回答 1

2

我没有关闭对服务器的请求,哦!在单独的实例中设置它为我解决了它。

我没有将文件名转换为二进制然后再转换为变体,只是将其保存为字符串。您必须省略LastCoder 示例NOTHING中最后给出的 。LobjXML.SEND添加这个会重现我上面给出的运行时错误。

感谢您的帮助,LastCoder。这是修改后的代码:


Public Sub DeleteFromSharePoint()

Dim xmlhttp
Dim sharepointUrl, sharepointFolder, sharepointFileName
Dim f, strZip As String
Dim LobjXML As Object

' Parent Sharepoint
sharepointUrl = "[SHAREPOINT URL]"

' In this test module, we're just deleting from the parent directory
sharepointFolder = ""

' Sets the report name we want to remove
f = "test"

' Sets the full .ZIP filename
' This is how reports are archived by date
strZip = f & "%20-%20" & Format(Now() - 1, "YYYY.MM.DD") & ".zip"

Set LobjXML = CreateObject("Microsoft.XMLHTTP")

    sharepointFileName = sharepointUrl & sharepointFolder & strZip

    ' Removes the data from the server, false means synchronous
    LobjXML.Open "DELETE", sharepointFileName, False

    ' Sends the request to remove the file
    LobjXML.Send

  Set LobjXML = Nothing

End Sub
于 2012-10-19T07:40:55.920 回答