SaveToPDF & Mail

fenrisn1

New Member
Joined
Sep 28, 2022
Messages
4
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Hi! I am having trouble with saving a range as PDF and mail it right after. It works sometimes and sometimes it does not. I am not sure what the issue is. Also on a side note I cannot save those file in subfolders on my desktop even if I change the paths. Does anyone have an idea why it works sometimes and not consistently? :)



VBA Code:
Sub SaveAsPDF()

'Create and assign variables
Dim saveLocation As String
Dim rng As Range

saveLocation = "C:\Users\Username\Desktop\"
Set rng = Sheets("SheetName").Range("G1:S46")

'Save a range as PDF
rng.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Worksheets(6).Range("U1").Value & ".pdf"

   'Create Outlook email
    Set xOutlookObj = CreateObject("Outlook.Application")
    Set xEmailObj = xOutlookObj.CreateItem(0)
    With xEmailObj
        .Display
        .To = ""
        .CC = ""
        .Subject = Range("U1").Value & ".pdf"
       [COLOR=rgb(209, 72, 65)] .Attachments.Add "C:\Users\UserName\Desktop\" & Worksheets(6).Range("U1").Value & ".pdf"[/COLOR]
        If DisplayEmail = False Then
            '.Send
            Range("Q2") = Range("Q2") + 1
        End If
    End With
  Exit Sub
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I noticed the highlighting did not work so [Color][/color] is not in my code. I wanted to highlight where the error was coming from. Also on a note, sometimes it creates the file and sometimes it does not and when it does not save/create the file I am getting an error "Run-time error '-2147024894 (80070002)'"
 
Upvote 0
Solved the issue, it actually saved in excel default location. Saw the mistake in the code aswell where I had to use: Filename:=saveLocation & Worksheets(6).Range("U1").Value & ".pdf"
 
Upvote 0
Solution

Forum statistics

Threads
1,215,219
Messages
6,123,688
Members
449,117
Latest member
Aaagu

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