Aretradeser
Board Regular
- Joined
- Jan 16, 2013
- Messages
- 176
- Office Version
- 2013
- Platform
- 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