简体   繁体   中英

Copy tabs in to a new unopened file and send in email with text in body and subject - VBA - Macro

have a segment of code that attaches copy tabs from a document and pastes them into a new document. Then sends only the new document to the intended addressees.

Is there a way I can change the name of the file I'm intending to send? It just sends as Book1.

Additionally, I'd like to add text in the body and the subject header of the email too. How can I go about doing this?

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

It's calling it Book1 because that's the default name for a new workbook until it's saved. To name it, just save the file to a temporary location (with a name of your choosing) before sending.

wb.SaveAs "C:\temporary folder location\filename_to_use.xlsx"
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM