我有以下代码保存到当前文件夹并打开文件:
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上具有指定标题的某些电子邮件地址的附件。
我该怎么做?
答案 0 :(得分:2)
如果您不想打开该文件,那么当您保存该文件时,您应该将其设置为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。