How to optimise this VBA code

Aretradeser

Board Regular
Joined
Jan 16, 2013
Messages
176
Office Version
  1. 2013
Platform
  1. Windows
I would like to optimise this VBA code, if possible.
VBA Code:
Sub SendPDFmail()
    Dim a As Worksheet, b As Worksheet
    Dim OApp As Object, OMail As Object, sbdy As String    
    Dim RutaTemporal As String, NombreFicheroTemporal  As String, RutaCompleta As String
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    'we disable screen refreshing
    'and very important the events!
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    
    'We define a variable that will be the Path where we will save,
    'before sending as an attachment, the pdf that we will generate...
    RutaTemporal = Environ$("temp") & "\"
    
    'We generate the name of the temporary .Pdf file.
    NombreFicheroTemporal = ActiveSheet.Name & ".pdf"
    
    'Combining the two variables above, we will have the Full Path of our .pdf
    RutaCompleta = RutaTemporal & NombreFicheroTemporal
    
    'We debug possible errors when Exporting
    'to the above path, the active sheet as PDF   
    ActiveSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=RutaCompleta, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
    
    'It's time to open the Outlook application.
    Set olApp = CreateObject("Outlook.Application")

    'and generate a new email to send...
    Set olMail = olApp.CreateItem(0)
    
    On Error Resume Next
    ChDir "C:\Users\00000000\Pictures"
    Firma = Application.GetOpenFilename("Archivos Excel (*.pn*), *.pn*")
    If VarType(Firma) = vbBoolean Then
        MsgBox ("Operation cancelled"), vbCritical, "AVISO"""
        Exit Sub
    End If
    Dest = "raul.fuente@policia.es"
    Asun = "Servicio de tardes"
    Set OApp = CreateObject("Outlook.Application")
    Set OMail = OApp.CreateItem(0)
    
    sini = "<P><FONT FACE= ""Calibri""><FONT SIZE=4 pto.>En archivo adjunto, te remito el servicio de tardes. <P></FONT>"
    stbl = "<Div> <IMG SRC=""" & Firma & """><br><br></Div>"
    ‘stbl = TableHTML
   
    sbdy = sini & vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine
    sbdy = sbdy & vbNewLine & vbNewLine & stbl & vbNewLine & vbNewLine
    sbdy = sbdy & spie
    
    With OMail
        .To = Dest
        '.CC = Cop
        '.BCC = SCop
        .Subject = Asun
        .Attachments.Add RutaCompleta
        .Display
        .HTMLBody = sbdy
        '.Send  'Activate to send automatically or deactivate for manual sending.
    End With
    On Error GoTo 0
    
    'Since the email has been sent (or displayed) with the pdf attached
    'we can delete the pdf that we have saved (in the temporary folder)...
    Kill RutaCompleta
    
    'we clean up the variables created.
    Set OMail = Nothing
    Set OApp = Nothing
    
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    MsgBox ("El mensaje se envió con éxito"), vbInformation, "AVISO"
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Since you seem to have not set the OptionExplizit, you can get rid of the Dim-commands.
Please check if below is working for you.

VBA Code:
Sub SendPDFmail()

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .DisplayAlerts = False
    End With

    RutaCompleta = Environ$("temp") & "\" & ActiveSheet.Name & ".pdf"
    
    ActiveSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=RutaCompleta, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
    
    On Error Resume Next
    ChDir "C:\Users\00000000\Pictures"
    Firma = Application.GetOpenFilename("Archivos Excel (*.pn*), *.pn*")
    If VarType(Firma) = vbBoolean Then
        MsgBox ("Operation cancelled"), vbCritical, "AVISO"""
        Exit Sub
    End If

    Set OApp = CreateObject("Outlook.Application")
    Set OMail = OApp.CreateItem(0)
    
    sini = "<P><FONT FACE= ""Calibri""><FONT SIZE=4 pto.>En archivo adjunto, te remito el servicio de tardes. <P></FONT>"
    stbl = "<Div> <IMG SRC=""" & Firma & """><br><br></Div>"
    ‘stbl = TableHTML
   
    sbdy = sini & vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine
    sbdy = sbdy & vbNewLine & vbNewLine & stbl & vbNewLine & vbNewLine
    sbdy = sbdy & spie ' ???--> variable "spie" not defined in this code?
    
    With OMail
        .To = "raul.fuente@policia.es"
        '.CC = Cop
        '.BCC = SCop
        .Subject = "Servicio de tardes"
        .Attachments.Add RutaCompleta
        .Display
        .HTMLBody = sbdy
        '.Send  'Activate to send automatically or deactivate for manual sending.
    End With
    On Error GoTo 0
    
    Kill RutaCompleta
    
    Set OMail = Nothing
    Set OApp = Nothing
    
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .DisplayAlerts = False
    End With

    MsgBox ("El mensaje se envió con éxito"), vbInformation, "AVISO"

End Sub
 
Upvote 0
Solution
Since you seem to have not set the OptionExplizit, you can get rid of the Dim-commands.
Please check if below is working for you.

VBA Code:
Sub SendPDFmail()

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .DisplayAlerts = False
    End With

    RutaCompleta = Environ$("temp") & "\" & ActiveSheet.Name & ".pdf"
   
    ActiveSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=RutaCompleta, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
   
    On Error Resume Next
    ChDir "C:\Users\00000000\Pictures"
    Firma = Application.GetOpenFilename("Archivos Excel (*.pn*), *.pn*")
    If VarType(Firma) = vbBoolean Then
        MsgBox ("Operation cancelled"), vbCritical, "AVISO"""
        Exit Sub
    End If

    Set OApp = CreateObject("Outlook.Application")
    Set OMail = OApp.CreateItem(0)
   
    sini = "<P><FONT FACE= ""Calibri""><FONT SIZE=4 pto.>En archivo adjunto, te remito el servicio de tardes. <P></FONT>"
    stbl = "<Div> <IMG SRC=""" & Firma & """><br><br></Div>"
    ‘stbl = TableHTML
  
    sbdy = sini & vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine
    sbdy = sbdy & vbNewLine & vbNewLine & stbl & vbNewLine & vbNewLine
    sbdy = sbdy & spie ' ???--> variable "spie" not defined in this code?
   
    With OMail
        .To = "raul.fuente@policia.es"
        '.CC = Cop
        '.BCC = SCop
        .Subject = "Servicio de tardes"
        .Attachments.Add RutaCompleta
        .Display
        .HTMLBody = sbdy
        '.Send  'Activate to send automatically or deactivate for manual sending.
    End With
    On Error GoTo 0
   
    Kill RutaCompleta
   
    Set OMail = Nothing
    Set OApp = Nothing
   
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .DisplayAlerts = False
    End With

    MsgBox ("El mensaje se envió con éxito"), vbInformation, "AVISO"

End Sub
Thank you very much, Zyndstoff, it works perfectly.
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top