2021年2月10日星期三

Add an excel sheet as attachment on outlook email with vba

I'm trying to send an entire Sheet ("SEND") from my excel file as an attachment using VBA, I have used the following code to accomplish it:

I'm getting the following error: The variable of the object or the variable 'with' was not defined, what is wrong with the code?

Sub Sendemail()  Dim xOutlookObj As Object  Dim xEmailObj As Object  Dim tempFile As String  Dim strbodymsg As String  Dim wb As Workbook  Dim tempWB As Workbook    tempFile = ActiveWorkbook.Path & "\sheets_copy.xlsx"    Set wb = wb.Sheets("SEND")  wb.Save  wb.Sheets(Array("SEND")).Copy  Set tempWB = ActiveWorkbook    If Len(Dir(tempFile)) <> 0 Then      Kill tempFile  End If    ' Save & close the tempFile  tempWB.SaveAs tempFile  tempWB.Close    strbody = "Hello"     'Create Outlook email  Set xOutlookObj = CreateObject("Outlook.Application")  Set xEmailObj = xOutlookObj.CreateItem(0)        On Error Resume Next  With xEmailObj      .Display      .To = "email1@gmail.com"      .Cc = "email2@gmail.com"      .Subject = "Email Test"      .Attachments.Add tempFile      .HTMLBody = strbody      If DisplayEmail = False Then          '.Send      End If  End With        On Error GoTo 0    Set xEmailObj = Nothing  Set xOutlookObj = Nothing    End Sub()  
https://stackoverflow.com/questions/66147229/add-an-excel-sheet-as-attachment-on-outlook-email-with-vba February 11, 2021 at 08:42AM

没有评论:

发表评论