gpt4 book ai didi

sharepoint - 使用 XMLHTTP 删除 Sharepoint 文件

转载 作者:行者123 更新时间:2023-12-02 12:43:21 25 4
gpt4 key购买 nike

继这篇优秀的作品之后,这里: Batch copy files to SharePoint site

现在,我只需单击一个按钮即可将压缩文件上传到 Sharepoint。

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

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

 
Public Sub CopyToSharePoint()<p></p>

<p>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</p>

<p>'Parent Sharepoint
sharepointUrl = "[SHAREPOINT PATH HERE]"</p>

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

<p>'File Path
strFilePath = "[ARCHIVE DRIVE]" & strMonthYear</p>

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

<p>Set LobjXML = CreateObject("Microsoft.XMLHTTP")</p>

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

<p>For Each f In fldr.Files</p>

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

<pre><code>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
</code></pre>

<p>sharepointFileName = sharepointUrl & sharepointFolder & f.Name</p>

<pre><code>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
</code></pre>

<p>' Send the file in.
LobjXML.Send LvarBinData</p>

<p>End If</p>

<p>NextF:
Next f</p>

<p>Set LobjXML = Nothing
Set fso = Nothing</p>

<p>End Sub
</p>

最佳答案

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

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

感谢 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

关于sharepoint - 使用 XMLHTTP 删除 Sharepoint 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12955831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com