How to send only a spreadsheet by email in VBA and not the whole file using the code below

Asked

Viewed 8,849 times

2

Option Compare Database
Option Explicit
Public Const C_Title  As String = "ef3.Cinco"
'Metodos
Public Sub SendMail(obj As MailClass) 'Nome da classe...

    Dim appOutLook As Outlook.Application '"Importando" Outlook
    Set appOutLook = CreateObject("Outlook.Application")

    Dim MailOutLook As Outlook.MailItem 'Propriedades de mensagem
    Set MailOutLook = appOutLook.CreateItem(olMailItem)

    With MailOutLook
        .To = obj.Destinatario 'Destinatários (separados por ';')
        .Subject = obj.Assunto 'Assunto
        .Body = obj.Detalhe 'Corpo do eMail
        .Attachments.Add (obj.Anexo) 'Caminho do anexo
        .Send 'Enviar
    End With
    Set MailOutLook = Nothing

End Sub
  • I will send the code, but basically if you copy the sheet with the tab you want to send and attach this file is easier.

1 answer

3


Ideal would be to save in a new file the sheet you want to send and pass the full path as parameter to this function, follows generic example I did as a model:

Sub enviaPlanilhaAtiva()

Dim oOutlook As Object
Dim oEmail As Object
Dim wbAtual As Workbook
Dim sNomeArquivo As String
Dim sLocalTemp As String
Application.ScreenUpdating = False

Set oOutlook = CreateObject("Outlook.Application")
Set oEmail = oOutlook.CreateItem(0)
sLocalTemp = "C:\temp\"

' Copia a planilha ativa e salva em local temporário
ActiveSheet.Copy
Set wbAtual = ActiveWorkbook

' Aqui você define qual planilha deve ser gravada
sNomeArquivo = wbAtual.Worksheets(1).Name

On Error Resume Next
Kill sLocalTemp & sNomeArquivo
On Error GoTo 0
wbAtual.SaveAs FileName:=sLocalTemp & sNomeArquivo

With oEmail
    '.To = "[email protected]"
    '.Subject = "Assunto"
    '.body = "Corpo do e-mail"
    .Attachments.Add wbAtual.FullName
    .Display
End With

'Deleta o arquivo temporário
wbAtual.ChangeFileAccess Mode:=xlReadOnly
Kill wbAtual.FullName
wbAtual.Close SaveChanges:=False

Set oEmail = Nothing
Set oOutlook = Nothing
End Sub

I hope I’ve helped!

  • thank you very much friend. It worked. Thank you. You wouldn’t know how to tell me how to make sure my macros don’t use so much processing right? Because I have this spreadsheet and each time q I use a macro it hangs, qnd the opened ta file it occupies 1.6GB in Ram and the ta size of 350mb and each time q saves it increases, being q I am updating data, I do not get to add.

  • @Alexandre would have to see the code to improve some points of memory utilization, variable release, code division with very long functions in new functions, But from what I’ve seen, you’re already using classes and I imagine you’d have to dig around to see which class is taking a lot of memory, but you’d have to debug the code and see point by point what might be building up the memory. If I can assess the function that’s causing this overuse of memory post in the question you asked about it so we can see and see if I can help. Abs!

  • @Alexandre in the case of saving and increasing the size of the spreadsheet, I need to ask which version of office is using, because from 2007 Office works well the memory and size of the files, but every time saved (with the file open) it uses some extra spaces to ensure future editions and save drafts, when closing the spreadsheet these spaces are emptied. I hope I contributed something else. Abs

  • So buddy, I can’t post here because the code has security, eh from the company I work for. However I use the 2013 version, and there comes a time when the program gets so heavy that it no longer opens the VBE. It says that the memory is insufficient.

  • @Lexandre in 2013 should theoretically perform better than others, if in 2013 it’s causing this, likely he needs to scan his code in some way. Try to review your loops and variables, try to use more WITH instead of setting auto this helps in the optimization of memory and performance. If I could, I would like to help resolve that issue. Abs

  • Evert, thank you so much for your help, it was extremely important for my project, I managed to optimize it and now it’s a beauty, fast. Thank you very much for the help. And I believe that will not be the last, because I am learned VBA last Wednesday, so I will have a lot to see. But thank you very much.

Show 1 more comment

Browser other questions tagged

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