将标签复制到新未打开的文件中,并发送包含正文和主题的电子邮件 - VBA - Macro

时间:2017-04-27 11:57:01

标签: excel-vba email outlook tabs attachment

有一段代码,用于附加文档中的复制标签并将其粘贴到新文档中。然后只将新文档发送给预期的收件人。

有没有办法可以更改我打算发送的文件的名称?它只是以Book1发送。

此外,我还想在电子邮件的正文和主题标题中添加文字。我怎么能这样做呢?

Sub Sendtabonemail()

Dim wb As Workbook
Dim strbody As String


   Set wb = Workbooks.Add
   ThisWorkbook.Sheets("sheet6").Copy After:=wb.Sheets(1)
   ThisWorkbook.Sheets("sheet3").Copy After:=wb.Sheets(1)
   wb.Application.Dialogs(xlDialogSendMail).Show "" & "emailreceiver1@domain.com" & "; " & "emailreceiver2@domain.com"

End Sub

2 个答案:

答案 0 :(得分:0)

它称之为Book1,因为它是新工作簿的默认名称,直到它被保存。要命名它,只需在发送之前将文件保存到临时位置(使用您选择的名称)。

wb.SaveAs "C:\temporary folder location\filename_to_use.xlsx"

答案 1 :(得分:0)

Sub dfjero()
Dim newWBname As String

    newWBname = Sheets(1).Name & "_" & Month(Date) & "_" & Day(Date) & "_" & Year(Date)
    Workbooks.Add
    If Len(Dir("c:\newFile", vbDirectory)) = 0 Then ' create and delete temporary directory and file
        MkDir "c:\newFile"
        ActiveWorkbook.SaveAs Filename:="C:\newFile\" & newWBname & ".xls", FileFormat:=xlExcel8
        ' This is where you send the book via email
        ActiveWorkbook.Close
        On Error Resume Next
        Kill "C:\newFile\" & newWBname & ".xls"
        RmDir "C:\newFile\"
        On Error GoTo 0
    Else    '  or add file to already created directory
       ActiveWorkbook.SaveAs Filename:="C:\newFile\" & newWBname & ".xls", FileFormat:=xlExcel8
       ' or alternatively this is where you send the workbook via email
    End If

End Sub