VBA:保存为 PDF 并将其与特定标题一起发送到 pre-selected 个电子邮件地址

VBA: save to PDF and send it with a specific title to pre-selected email addresses

我有以下代码保存到当前文件夹并打开文件:

Dim myFile As Variant
Dim strFile As String
On Error GoTo errHandler

Set ws = Worksheets("mysheet")

'enter name and select folder for file
' start in current workbook folder
strFile = Replace(Replace(ws.Name, " ", ""), ".", "_") _
            & "_" _
            & Format(Now(), "yyyymmdd\_hhmm") _
            & ".pdf"
strFile = ThisWorkbook.Path & "\" & strFile

ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    strFile, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, From:=1, To:=3, OpenAfterPublish:=True

exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler
End Sub

但是,我不想打开该文件,而是想将该文件作为附件发送到 Outlook 上具有指定标题的某些电子邮件地址。

我该怎么做?

如果您不想打开该文件,那么当您保存它时,您应该将其设置为 false:

OpenAfterPublish:=False '<-- in your code is now True

要将其作为附件发送,您只需附加您创建的字符串:

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.to = "test@gmail.com"
.Subject = "Testfile"
.Body = "Hi"     
.Attachments.Add strFile
.Send  'Or use .Display to see the mail and send it manually
End With

通过 Excel VBA here.

查找更多关于 Outlook 用法的信息