Email from Excel with attachment

brianfosterblack

Active Member
Joined
Nov 1, 2011
Messages
251
I have this code to save the invoice in my Excel spreadsheet as a pdf document
VBA Code:
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\User\Documents\Clay\2021 SA Grand\Entries.pdf", Quality:= _
        xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
This code then attaches the document to the mail when I send it
VBA Code:
emailItem.Attachments.Add ("C:\Users\User\Documents\Clay\2021 SA Grand\Entries.pdf")

It works perfectly but obviously if someone else uses the workbook the directory changes
The Entries Workbook and Entries.pdf will always be in the same directory

Is there a way to change this code that it saves the pdf to the same directory the workbook is in (the workbook from which this code is being run)
And then picks up the attachment from the same directory?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try:
VBA Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    ThisWorkbook.Path & "\Entries.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

emailItem.Attachments.Add (ThisWorkbook.Path & "\Entries.pdf")
 
Upvote 0
Solution
Try:
VBA Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    ThisWorkbook.Path & "\Entries.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

emailItem.Attachments.Add (ThisWorkbook.Path & "\Entries.pdf")
Thanks you this works perfectly
 
Upvote 0
@brianfosterblack - I just switched the marked solution with the correct one as you confirmed. Next time, that would be great if you could mark the solution post that answered the problem instead of your final post. It will help future readers to find the solution post easier.
 
Upvote 0
@brianfosterblack - I just switched the marked solution with the correct one as you confirmed. Next time, that would be great if you could mark the solution post that answered the problem instead of your final post. It will help future readers to find the solution post easier.
thank you smozgur. I am still learning but I cannot learn if no-one points out my errors. Thank you.
 
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,205
Members
448,874
Latest member
Lancelots

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