簡體   English   中英

將選項卡復制到一個新的未打開的文件中,並在正文和主題中包含文本的電子郵件中發送 - VBA - 宏

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

有一段代碼附加文檔中的復制選項卡並將它們粘貼到新文檔中。 然后只將新文檔發送給預期的收件人。

有什么方法可以更改我打算發送的文件的名稱? 它只是作為 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

它稱它為Book1 ,因為這是新工作簿保存前的默認名稱。 要命名它,只需在發送前將文件保存到一個臨時位置(使用您選擇的名稱)。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM