Excel Sheet export to PDF and attachment to email not working

kitsa

Board Regular
Joined
Mar 4, 2016
Messages
111
Office Version
  1. 365
  2. 2016
Hi,
Can anyone tell me why this code isn't working? I have an excel sheet export to PDF and send as attachment to email. The problem is it's working on my pc but not working on my college pc. On my College pc, the PDF is save to his desktop as on mine, I think it saves to a temp folder? Does anyone know why or what I can do to fix this?

VBA Code:
Private Sub CommandButton1_Click()
'Sub SendExcelFilesAsPDFtoEmail()
  
   Dim OutApp As Object
   Dim OutMail As Object
   Dim Strbody As String, Fname As String
   Dim x As Date
       x = Format(Now() + 1, "MMMM dd, yyyy")
  
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    Fname = "EQ" & Range("F19") & ".pdf"

 Strbody = Range("K14")
 
 On Error Resume Next

 ' Create the PDF attachment
  ActiveSheet.ExportAsFixedFormat Type:=x1TypePDF, Filename:=Fname

With OutMail
        .Display
        .To = Range("K11").Value
        .Subject = Range("K13") & " " & x
        
        .HTMLBody = "<p style='font-family:calibri;font-size:14.5'>" & Replace(Strbody, ", ", ",<br/>") & "<br/>" & "" & "</p>" & .HTMLBody
        
        .Attachments.Add Fname
        .Importance = 2 'Or olImprotanceHigh Or olImprotanceLow
        .Display
    End With
    
  On Error GoTo 0
   Set OutMail = Nothing
   Set OutApp = Nothing
   Set OutLookMess = Nothing
   Set OutLookNSpace = Nothing
  
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
The file name is not qualified with a path, so the Attachments.Add method has to guess where the file originates.
You could use something like this:

VBA Code:
Fname = Environ("tmp") & "\" &  "EQ" & Range("F19") & ".pdf"
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,626
Members
449,094
Latest member
bsb1122

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