Generate a file and attach in email with vba

1

I made a macro where it opens the Outlook, up to that beauty. now I need to supplement it with the following items.

I need to get 4 tabs from my spreadsheet, generate a new file and then attach it to the email and send it.

Can anyone help me?

follow the code (this code only opens excel)

Sub MandaEmail()

    Dim EnviarPara As String
    Dim Mensagem As String
    For i = 1 To 1
        EnviarPara = Worksheets("Tabela").Cells(5, 35)
        If EnviarPara <> "" Then
            Mensagem = Worksheets("Tabela").Cells(6, 35)
            Texto = Worksheets("Tabela").Cells(7, 35)
            Envia_Emails EnviarPara, Mensagem
        End If
    Next i
End Sub
Sub Envia_Emails(EnviarPara As String, Mensagem As String)
    Dim OutlookApp As Object
    Dim OutlookMail As Object
    Set OutlookApp = CreateObject("Outlook.Application")
    Set OutlookMail = OutlookApp.CreateItem(0)
    With OutlookMail
        .To = EnviarPara
        .CC = ""
        .BCC = ""
        .Subject = Mensagem
        .Body = "Bom dia"
        .Display ' para envia o email diretamente defina o código  .Send
    End With
    Set OutlookMail = Nothing
    Set OutlookApp = Nothing
End Sub
    
asked by anonymous 21.05.2018 / 15:03

1 answer

1

Code

Sub MandaEmail()

    Dim EnviarPara As String
    Dim Mensagem As String, caminho As String, Texto As String
    Dim wb As Workbook
    'Criar Arquivo
    caminho = ThisWorkbook.Path & "\" & "temp.xlsx"
    Application.DisplayAlerts = False
    ThisWorkbook.Sheets(Array("Planilha1", "Planilha2", "Planilha3", "Planilha4")).Copy
    ActiveWorkbook.SaveAs Filename:=caminho, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWindow.Close

    'Enviar Email
    For i = 1 To 1
        EnviarPara = Worksheets("Tabela").Cells(1, "A")
        If EnviarPara <> "" Then
            Mensagem = Worksheets("Tabela").Cells(2, "A")
            Texto = Worksheets("Tabela").Cells(3, "A")
            Envia_Emails EnviarPara, Mensagem, caminho, Texto
        End If
    Next i

    Kill caminho 'Deleta o arquivo

    Application.DisplayAlerts = True
End Sub

Sub Envia_Emails(EnviarPara As String, Mensagem As String, caminho As String, Texto As String)
    Dim OutlookApp As Object
    Dim OutlookMail As Object
    Set OutlookApp = CreateObject("Outlook.Application")
    Set OutlookMail = OutlookApp.CreateItem(0)
    With OutlookMail
        .Display
        Signature = .HTMLbody
        .To = EnviarPara
        .CC = ""
        .BCC = ""
        .Subject = Mensagem
        .Attachments.Add caminho
        .HTMLbody = "Bom dia" &  "<br>"& Texto &  "<br>" & Signature
        .Display                                 ' para envia o email diretamente defina o código  .Send
    End With
    Set OutlookMail = Nothing
    Set OutlookApp = Nothing
End Sub

Explanation

You add the attachment in Outlook with .Attachments.Add

And create a new Excel file with the desired worksheets with: ThisWorkbook.Sheets(Array("Planilha1", "Planilha2", "Planilha3", "Planilha4")).Copy

Then save the worksheet in the same folder as the excel file: ActiveWorkbook.SaveAs Filename:=caminho, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False and at the end of the program delete this Excel file Kill caminho

For other ways to insert tables into the body of the email, see this answer

    
21.05.2018 / 16:58
How do I search for something in html css js? ______ qstntxt ___

Calling priority Nav, a lib that places the executing items inside a new drop-down, called "More", this dropdown is dynamically created in pure javascript as soon as it's needed. In it a button appears with class js-nav-toggle . I'm trying out some things, and I would like to change some things on that button, with pure javascript, I'm doing a test trying to find the js-nav-toggle class that is definitely in the document but only gives undefined.

link

I use this code that works fine on other elements that are written manually from the document. %code%

What could be happening?

    
______ azszpr310823 ___

The variable %code% is a list of nodes of class %code% :

%pre%

You can not change the list at once. It is necessary to iterate every element of the list, and you can do this %code% :

%pre%

But if it is only 1 element, you can use the index %code% :

%pre%

You can also use %code% (if it is only 1 element) or %code% (1 or more):

%pre%

While %code% also creates a list of nodes equally to %code% , and if it is only 1 element, you also use the %code% index:

%pre%

Or:

%pre%

However, if it is only 1 element, it makes more sense to use %code% :

%pre%

If it's just 1 element, it does not make sense to create a list of nodes.

Example:

%pre% %pre% %pre%
    
______ ___ azszpr310805

Hello @John,

Do you wanna change the color of priority nav class, it?

Via css u can do:

%pre%

or

If u can use jquery, try the following:

%pre%     
___