希望有人可以幫忙。我有下面的代碼,它生成一個 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 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
uj5u.com熱心網友回復:
失敗的原因是您使用現有檔案名/路徑來創建 PDF 檔案名/路徑。創建臨時檔案的常用方法,但它可能有問題。
看起來.ExportAsFixedFormat和.SaveAsetc. 很樂意采用看起來像 URL 的檔案名/路徑:
https://<sharepointsite>.sharepoint.com/sites/<site name>/<folder>/<folder>/<filename.xlsx>
但是,似乎Kill不允許使用它。我相信FSO.DeleteFile會有同樣的問題。
相反,您需要將該站點 URL 轉換為普通檔案路徑。查找所需路徑的最簡單方法是在 Windows 資源管理器中瀏覽到檔案,檢查檔案夾路徑并了解差異。
或者,為您的臨時 pdf 檔案選擇一個替代位置。可以使用以下命令找到您的 Temp 檔案夾tempfolder = Environ$("temp")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/314264.html
上一篇:在訪問中創建查詢
