以下代码生成 PDF 并将其通过电子邮件发送到某个电子邮件地址。
文件是在 Sharepoint 中创建的。
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 protected]"
.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
失败的原因是您使用现有的文件名/路径来创建 PDF 文件名/路径。创建临时文件的常见方法,但可能存在问题。
看起来
.ExportAsFixedFormat
和 .SaveAs
等很乐意采用看起来像 URL 的文件名/路径:
https://<sharepointsite>.sharepoint.com/sites/<site name>/<folder>/<folder>/<filename.xlsx>
但是,
Kill
似乎不允许使用此功能。我相信FSO.DeleteFile
也会有同样的问题。
相反,您需要将该站点 URL 转换为普通文件路径。查找所需路径的最简单方法是在 Windows 资源管理器中浏览到该文件,检查文件夹路径并了解差异。
或者,为临时 pdf 文件选择另一个位置。您可以使用
tempfolder = Environ$("temp")
找到您的 Temp 文件夹