使用 VBA 从 Sharepoint 删除文件
Deleting a file from Sharepoint using VBA
希望有人能提供帮助。我有以下代码生成 PDF 并将其通过电子邮件发送到某个电子邮件地址。但是,我遇到的问题是创建的文件始终是在 Sharepoint 中创建的,我在使用 kill
命令时遇到了一些困难。当使用 Kill PdfFile
时,因为它无法找到它首先创建的文件。谁能告诉我如何删除此文件?
Private Sub SendFile()
Dim IsCreated As Boolean
Dim i As Long
Dim PdfFile As String, Title As String
Dim OutlApp As Object
Sheet1.Visible = True
Sheet1.Activate
Title = Sheet1.Range("E8") & " " & Sheet1.Range("B20")
PdfFile = ActiveWorkbook.FullName
i = InStrRev(PdfFile, ".")
If i > 1 Then PdfFile = Left(PdfFile, i - 1)
PdfFile = PdfFile & "_" & ActiveSheet.Name & ".pdf"
With ActiveSheet
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End With
On Error Resume Next
Set OutlApp = GetObject(, "Outlook.Application")
If Err Then
Set OutlApp = CreateObject("Outlook.Application")
IsCreated = True
End If
OutlApp.Visible = True
On Error GoTo 0
With OutlApp.CreateItem(0)
.Subject = Title
.To = "email@email.com"
.Body = "Hi," & vbLf & vbLf _
& "The report is attached in PDF format." & vbLf & vbLf _
& "Regards," & vbLf _
& Application.UserName & vbLf & vbLf
.Attachments.Add PdfFile
On Error Resume Next
.Send
Application.Visible = True
If Err Then
MsgBox "E-mail was not sent", vbExclamation
Else
MsgBox "E-mail successfully sent", vbInformation
End If
On Error GoTo 0
End With
' Delete PDF file
Kill PdfFile 'This is where things go sideways
' Quit Outlook if it was created by this code
If IsCreated Then OutlApp.Quit
' Release the memory of object variable
Set OutlApp = Nothing
End Sub
失败的原因是您使用现有 filename/path 创建 PDF filename/path。一种创建临时文件的常用方法,但它可能存在问题。
看来 .ExportAsFixedFormat
和 .SaveAs
等人很乐意接受看起来像 URL 的 filename/path:
https://<sharepointsite>.sharepoint.com/sites/<site name>/<folder>/<folder>/<filename.xlsx>
但是,Kill
似乎不允许使用它。我相信 FSO.DeleteFile
也会有同样的问题。
相反,您需要将该站点 URL 转换为普通文件路径。找到所需路径的最简单方法是在 Windows 资源管理器中浏览到文件,检查文件夹路径并了解差异。
或者,为您的临时 pdf 文件选择其他位置。可以使用 tempfolder = Environ$("temp")
找到您的临时文件夹
希望有人能提供帮助。我有以下代码生成 PDF 并将其通过电子邮件发送到某个电子邮件地址。但是,我遇到的问题是创建的文件始终是在 Sharepoint 中创建的,我在使用 kill
命令时遇到了一些困难。当使用 Kill PdfFile
时,因为它无法找到它首先创建的文件。谁能告诉我如何删除此文件?
Private Sub SendFile()
Dim IsCreated As Boolean
Dim i As Long
Dim PdfFile As String, Title As String
Dim OutlApp As Object
Sheet1.Visible = True
Sheet1.Activate
Title = Sheet1.Range("E8") & " " & Sheet1.Range("B20")
PdfFile = ActiveWorkbook.FullName
i = InStrRev(PdfFile, ".")
If i > 1 Then PdfFile = Left(PdfFile, i - 1)
PdfFile = PdfFile & "_" & ActiveSheet.Name & ".pdf"
With ActiveSheet
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End With
On Error Resume Next
Set OutlApp = GetObject(, "Outlook.Application")
If Err Then
Set OutlApp = CreateObject("Outlook.Application")
IsCreated = True
End If
OutlApp.Visible = True
On Error GoTo 0
With OutlApp.CreateItem(0)
.Subject = Title
.To = "email@email.com"
.Body = "Hi," & vbLf & vbLf _
& "The report is attached in PDF format." & vbLf & vbLf _
& "Regards," & vbLf _
& Application.UserName & vbLf & vbLf
.Attachments.Add PdfFile
On Error Resume Next
.Send
Application.Visible = True
If Err Then
MsgBox "E-mail was not sent", vbExclamation
Else
MsgBox "E-mail successfully sent", vbInformation
End If
On Error GoTo 0
End With
' Delete PDF file
Kill PdfFile 'This is where things go sideways
' Quit Outlook if it was created by this code
If IsCreated Then OutlApp.Quit
' Release the memory of object variable
Set OutlApp = Nothing
End Sub
失败的原因是您使用现有 filename/path 创建 PDF filename/path。一种创建临时文件的常用方法,但它可能存在问题。
看来 .ExportAsFixedFormat
和 .SaveAs
等人很乐意接受看起来像 URL 的 filename/path:
https://<sharepointsite>.sharepoint.com/sites/<site name>/<folder>/<folder>/<filename.xlsx>
但是,Kill
似乎不允许使用它。我相信 FSO.DeleteFile
也会有同样的问题。
相反,您需要将该站点 URL 转换为普通文件路径。找到所需路径的最简单方法是在 Windows 资源管理器中浏览到文件,检查文件夹路径并了解差异。
或者,为您的临时 pdf 文件选择其他位置。可以使用 tempfolder = Environ$("temp")