attach file and send vba email

Asked

Viewed 2,174 times

2

Good morning guys, I need some help.. I have this macro she’s working perfectly but I need to add 1 things to it.

When she run and generate the new file with the tabs I selected, I need you to open the outlook, that new file gets attached to it.

someone can help me?

follows the Cod.

' MACRO PARA CRIAR PASTA DE ANO/MES/DIA

Public Const sCaminho = "pasta local para salvar"
Dim Pasta As New FileSystemObject

Public Function fnccriardiretorio(data As Date)
If Pasta.FolderExists(sCaminho & "\" & Format(data, "yyyy")) Then
    fncmes (data)
Else
    Pasta.CreateFolder (sCaminho & "\" & Format(data, "yyyy"))
    fncmes (data)
End If


End Function

Public Function fncmes(data As Date)
If Pasta.FolderExists(sCaminho & "\" & Format(data, "yyyy") & _
"\" & Format(data, "mmmm")) Then
    Call fncdia(data)
Else
    Pasta.CreateFolder (sCaminho & "\" & Format(data, "yyyy") & _
"\" & Format(data, "mmmm"))
    Call fncdia(data)
End If
End Function

Public Function fncdia(data As Date)
If Pasta.FolderExists(sCaminho & "\" & Format(data, "yyyy") & _
"\" & Format(data, "mmmm") & "\" & Format(data, "dd")) Then

Else
    Pasta.CreateFolder (sCaminho & "\" & Format(data, "yyyy") & _
"\" & Format(data, "mmmm")) & "\" & Format(data, "dd")
End If


End Function

Sub salvareenviaremail()

Dim data As Date
data = Now()
Call fnccriardiretorio(data)
 Dim ws3 As Worksheet
 Dim UltimaLinhaE As Long


fname1 = (sCaminho & "\" & Format(data, "yyyy") & _
"\" & Format(data, "mmmm")) & "\" & Format(data, "dd") & _
"\" & "nome do arquivo"


    Worksheets(Array("Planilha1", "Planilha2", "Planilha3")).Copy

    Set ws3 = ActiveWorkbook.Worksheets("Planilha3")
    With ws3
        'Limpa os Autofiltros da Planilha para evitar erros
        If .FilterMode Then
            .ShowAllData
        End If
        'Última Linhada colunaE
        UltimaLinhaE = .Cells(.Rows.Count, "E").End(xlUp).Row
        'Autofiltro
        .Range("E1:E" & UltimaLinhaE).AutoFilter Field:=5, Criteria1:="Cell 01"
    End With


Set ws4 = ActiveWorkbook.Worksheets("Planilha2")
    With ws4

        'Limpa os Autofiltros da Planilha para evitar erros
        If .FilterMode Then
            .ShowAllData
        End If

        'Última Linhada colunaE
        UltimaLinhaE = .Cells(.Rows.Count, "E").End(xlUp).Row

        'Autofiltro
        .Range("E1:E" & UltimaLinhaE).AutoFilter Field:=5, Criteria1:="Cell 02"

    End With

    With ActiveWorkbook
        ActiveWorkbook.SaveAs Filename:=fname1, FileFormat:=xlOpenXMLWorkbookMacroEnabled

    End With
End Sub

1 answer

2


First place the following line before the End Sub of its first function

Call Abrir_outlook

Then add this second function. Note that I have commented on some fields that may not be useful to you.

Sub Abrir_outlook()

Shell ("OUTLOOK")

Dim objMsg As MailItem
Dim intChoice As Integer
Dim strPath As String

Set objMsg = Application.CreateItem(olMailItem)

 With objMsg
  .To = "endereço de destino"
  .CC = "endereços copiados"
  .BCC = "endereços copisados ocultos"
  .Subject = "Assunto"
  .Categories = "Teste"
  '.VotingOptions = "sim, não, talvez"
  .BodyFormat = olFormatPlain
  '.Importance = olImportanceHigh
  '.Sensitivity = olConfidential

Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
intChoice = Application.FileDialog(msoFileDialogOpen).Show

If intChoice <> 0 Then
    strPath = Application.FileDialog( _
        msoFileDialogOpen).SelectedItems(1)
End If

  .Attachments.Add (strPath)

  .Display
End With

Set objMsg = Nothing

End Sub
  • Show @max thanks now leaves me a doubt, you know the new file that was generated? how do I attach, it does not have a default folder, because the macro creates a daily folder for it, have any idea?

  • Obs: It does not have a default folder

  • You have a few options; you can set a specific location to save these sheets; or you can adapt the attachment row if you follow a logical order (for example, every day increases the final value by 1, or the date is added); or when you run the macro you can call a window that asks which file will be attached

  • how it would look... when you run the macro you can call a window that asks which file will be attached?

  • I did the code editing. Check if it works right as I do not have Outlook configured here on the machine to do the tests

  • 1

    perfect, thank you so much for your help

  • @Johnhebert If you helped, don’t forget to vote yes and validate the answer as "Accept" by clicking on the Check sign. This will help other people who arrive here with the same doubt. And if you need, dispose.

  • 1

    done, thank you!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.