Excel VBA : Saving and Attaching a worksheet as pdf

标签 excel vba email pdf attachment

我已经结合了来自几个不同示例的一些代码来使其工作,但我的解决方案似乎很笨拙,因为我正在创建 2 个 pdf。一个在临时文件夹中,一个在当前文件夹中。临时文件夹中的那个是附加到电子邮件的那个。我只想在当前文件夹中保存一个 pdf 并将该 pdf 附加到电子邮件中。
这是导出两个 pdf 的代码:

 Title = ActiveSheet.Range("B11").Value & " Submittal"

' Define PDF filename in TEMP folder
PdfFile = ActiveWorkbook.Name
i = InStrRev(PdfFile, ".xl", , vbTextCompare)
If i > 1 Then PdfFile = Left(PdfFile, i - 1)
PdfFile = Title
For Each char In Split("? "" / \ < > * | :")
PdfFile = Replace(PdfFile, char, "_")
Next
PdfFile = Left(CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2) & "\" & PdfFile, 251) & ".pdf"
With ActiveSheet
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End With
With ActiveSheet
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & "\" & ActiveSheet.Range("B11").Value & " Submittal", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
End With

出于某种原因,如果我添加 ThisWorkbook.Path & "\"到第一个导出文件的文件名,如下所示:Filename:=ThisWorkbook.Path & "\" & PdfFile ,所以它保存在当前文件夹而不是临时文件夹中,我得到一个运行时错误,即使这是将第二个 pdf 文件成功导出到当前文件夹的相同代码,它也不会保存。
这是完整的工作代码,但如果可能,我想消除临时 pdf:
Sub RightArrow2_Click()
  Dim IsCreated As Boolean
  Dim PdfFile As String, Title As String
  Dim OutlApp As Object
  Dim i As Long
  Dim char As Variant

Title = ActiveSheet.Range("B11").Value & " Submittal"

' Define PDF filename in TEMP folder
PdfFile = ActiveWorkbook.Name
i = InStrRev(PdfFile, ".xl", , vbTextCompare)
If i > 1 Then PdfFile = Left(PdfFile, i - 1)
PdfFile = Title
For Each char In Split("? "" / \ < > * | :")
PdfFile = Replace(PdfFile, char, "_")
Next
PdfFile = Left(CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2) & "\" & PdfFile, 251) & ".pdf"
'Debug.Print PdfFile

' Export activesheet as PDF to the temporary folder
With ActiveSheet
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End With
With ActiveSheet
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & "\" & ActiveSheet.Range("B11").Value & " Submittal", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
End With

' Use already open Outlook if possible
On Error Resume Next
Set OutlApp = GetObject(, "Outlook.Application")
If Err Then
Set OutlApp = CreateObject("Outlook.Application")
IsCreated = True
End If
On Error GoTo 0

' Prepare e-mail with PDF attachment
With OutlApp.CreateItem(0)

' Prepare e-mail
.Subject = Title
.To = ActiveSheet.Range("H12").Value 
.CC = "" 
.Body = "Please see the attached submittal for " & ActiveSheet.Range("B11").Value & "." & vbLf & vbLf _
      & "Thank you," & vbLf & vbLf _
      & vbLf
.Attachments.Add PdfFile

' Display email
On Error Resume Next
.Display ' or use .Send

' Return focus to Excel's window
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 the temporary PDF file
If Len(Dir(PdfFile)) Then Kill PdfFile

' Try to quit Outlook if it was not previously open
If IsCreated Then OutlApp.Quit

' Release the memory of object variable
' Note: sometimes Outlook object can't be released from the memory
Set OutlApp = Nothing
End Sub

最佳答案

在您的描述中,在代码行中Filename:=ThisWorkbook.Path & "\" & PdfFilePdfFile变量包含临时文件夹的路径,这就是您收到错误的原因。

关于Excel VBA : Saving and Attaching a worksheet as pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30384725/

相关文章:

excel - 如何在真正的空白单元格上进行查找和替换

vba - excel vba : line break in code

vba - 使用 VBA 在 Excel 中动态生成表单

excel - 出现错误时如何中断?

VBA - 隐藏行宏限制

security - 为什么不在取消订阅链接中使用电子邮件地址

VBA Drop Down 创建 Outlook 电子邮件 HTMLBody

mysql - 嵌套的 MySQL 在 Access VBA 中不起作用

email - 如果您为某个域启用了 DKIM,您可以使用不使用 DKIM 的邮件服务器发送邮件吗?

Python,在 smtplib 参数中添加逗号会导致错误