Macro Help required

Arts

Well-known Member
Joined
Sep 28, 2007
Messages
782
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi guys

I have the following code which was done by a former colleague

'
' Save Macro
Public Sub CommandButton1_Click()
ThisFile = Range("D18").Value
MyDir = Range("I1").Value
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=MyDir & ThisFile & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub

What the above does is create a pdf document and save it down to a location which works perfectly

Is it possible to build on this ?

What I would like to happen if possible is that I have an e mail address in cell A10 of the "Service Invoice" sheet

Would it be possible to attach the saved pdf and attach it to outlook and send it to the e mail address in cell A10

If also possible in the subject field of outlook once the email is aatched and ready to send is it possible to have the value in cell A5 of the "Service Invoice" Sheet and then the value in D18 which is the invoice number so the subject field reads "Palace Place, Inv No. 1003"

Any help on this would be most appreciated

 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Thanks Alphafrog

I shall take a look, is what I'm after seen as a big job or is it relatively simple, I only ask I have no macro experience whats so ever apart from knowing Alt+F11 opens the editor
 
Last edited:
Upvote 0
I managed to find this code (online from a link from AlphaFrog) which was code to send a pdf as an e mail,

Function RDB_Mail_PDF_Outlook(FileNamePDF As String, StrTo As String, _<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
StrSubject As String, StrBody As String, Send As Boolean)<o:p></o:p>
Dim OutApp As Object<o:p></o:p>
Dim OutMail As Object<o:p></o:p>
<o:p> </o:p>
Set OutApp = CreateObject("Outlook.Application")<o:p></o:p>
Set OutMail = OutApp.CreateItem(0)<o:p></o:p>
<o:p> </o:p>
On Error Resume Next<o:p></o:p>
With OutMail<o:p></o:p>
.To = StrTo<o:p></o:p>
.CC = ""<o:p></o:p>
.BCC = ""<o:p></o:p>
.Subject = StrSubject<o:p></o:p>
.Body = StrBody<o:p></o:p>
.Attachments.Add FileNamePDF<o:p></o:p>
If Send = True Then<o:p></o:p>
.Send<o:p></o:p>
Else<o:p></o:p>
.Display<o:p></o:p>
End If<o:p></o:p>
End With<o:p></o:p>
On Error GoTo 0<o:p></o:p>
<o:p> </o:p>
Set OutMail = Nothing<o:p></o:p>
Set OutApp = Nothing<o:p></o:p>
End Function<o:p></o:p>

so i thought all i would do is paste below my previous code which saves it down as a pdf (earlier post 1.17pm) but wasnt as quite straight forward as that....
 
Upvote 0
I have admitted defeat in this after hours of trying if anyone is able to give me a push in the right direction would be most helpful
 
Upvote 0
You need to fill in the Email address, Subject text, and Body text in Red
This is not tested.

Code:
Public Sub CommandButton1_Click()

    Dim ThisFile As String, MyDir As String

    MyDir = Range("I1").Value
    ThisFile = MyDir & Range("D18").Value & ".pdf"
    
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisFile, _
                                    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                                    IgnorePrintAreas:=False, OpenAfterPublish:=True
                                    
    RDB_Mail_PDF_Outlook FileNamePDF:=ThisFile, _
                         StrTo:="[COLOR="Red"]Email@Blah.com[/COLOR]", _
                         StrSubject:="[COLOR="Red"]Subject of the email goes here[/COLOR]", _
                         StrBody:="[COLOR="Red"]Text for the body of the email" & vbLf & "goes here.[/COLOR]", _
                         Send:=True
End Sub

Function RDB_Mail_PDF_Outlook(FileNamePDF As String, StrTo As String, _
                              StrSubject As String, StrBody As String, Send As Boolean)
    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .To = StrTo
        .CC = ""
        .BCC = ""
        .Subject = StrSubject
        .Body = StrBody
        .Attachments.Add FileNamePDF
        If Send = True Then
            .Send
        Else
            .Display
        End If
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Function
 
Upvote 0
Thank you so much for that AlphaFrog...it works it works !!!! Lol so much relief right now I cant explain

Just a quick question would I need to fill in the e mail address each time myself

I have an e mail address in cell A10 is it possible to pick it up from there as the address is always changing
 
Last edited:
Upvote 0
Code:
    RDB_Mail_PDF_Outlook FileNamePDF:=ThisFile, _
                         StrTo:=[COLOR="Red"]Range("A10").Value[/COLOR], _
                         StrSubject:="Subject of the email goes here", _
                         StrBody:="Text for the body of the email" & vbLf & "goes here.", _
                         Send:=True
 
Upvote 0
Thank you so much works exactly how I would have liked it to
 
Upvote 0
One minor minor point

On the body of the email i have put

StrBody:="Please find attached copy of invoice" & vbLf & "goes here.", _

On the body of the actual e mail it writes

Please find attched copy of invoice
goes here.



Is there any way i can get rid of the "goes here" part
 
Upvote 0

Forum statistics

Threads
1,224,617
Messages
6,179,914
Members
452,949
Latest member
beartooth91

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