+ Reply to Thread
Results 1 to 5 of 5

Dynamic referencing in VBA to send an email with attachment

  1. #1
    Registered User
    Join Date
    02-14-2017
    Location
    Hilversum, Netherlands
    MS-Off Ver
    2010
    Posts
    92

    Dynamic referencing in VBA to send an email with attachment

    Dear all,

    I am experiencing difficulties with Excel VBA! Below is what I have typed in VBA. I want to reference the file name, body, subject and attachment to a cell if possible. Please help!

    Sub Print_en_save()


    Dim saveCell As Range
    Dim savepathCell As Range


    Set saveCell = ActiveSheet.Range("T17")
    SaveAs = saveCell.Value

    Set savepathCell = ActiveSheet.Range("T16")
    savepath = savepathCell.Value


    ' Save as PDF
    ChDir savepath
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    SaveAs & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False


    Dim OutLookApp As Object
    Dim OutLookMailItem As Object
    Dim myAttachments As Object


    Set OutLookApp = CreateObject("Outlook.application")
    Set OutLookMailItem = OutLookApp.CreateItem(0)
    Set myAttachments = OutLookMailItem.Attachments


    With OutLookMailItem
    .To = ("T8")
    .Subject = ("T19")
    .Body = ("T20")
    myAttachments ("T21")
    .Send


    End With


    End Sub

  2. #2
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Dynamic referencing in VBA to send an email with attachment

    Welcome to the forum
    Please put the code between code tags
    Give this a try
    Please Login or Register  to view this content.
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  3. #3
    Registered User
    Join Date
    02-14-2017
    Location
    Hilversum, Netherlands
    MS-Off Ver
    2010
    Posts
    92

    Re: Dynamic referencing in VBA to send an email with attachment

    @Yasser thanks for your quick reply! Now it works, however it does not send the attachment with the mail!

    The T20 that myAttachments sh.Range refers to is the filename +".pdf"

    Do you know what is going wrong?

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Dynamic referencing in VBA to send an email with attachment

    If your cell T20 has the full path to the .pdf file it would be sent.
    Can you upload sample of your workbook to see what's wrong with it?

  5. #5
    Registered User
    Join Date
    02-14-2017
    Location
    Hilversum, Netherlands
    MS-Off Ver
    2010
    Posts
    92

    Re: Dynamic referencing in VBA to send an email with attachment

    Thanks! Hope the attachment works! I altered the T20 to T21 btw!
    Attached Images Attached Images

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro to send email with attachment for each unique email Ids
    By vijanand1279 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-06-2014, 07:12 AM
  2. [SOLVED] Savecopyas then send as email attachment
    By trisoldee in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-15-2014, 11:52 AM
  3. [SOLVED] send email with attachment
    By ks1102 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-06-2013, 02:29 AM
  4. Modify VBA to send email with attachment
    By dip11 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-06-2012, 02:27 PM
  5. How to send email with attachment
    By staciam in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-04-2011, 12:19 PM
  6. How to send the doc to email as an attachment
    By mdkaye in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-08-2009, 06:25 AM
  7. [SOLVED] cant send email attachment from excel
    By DKT in forum Excel - New Users/Basics
    Replies: 8
    Last Post: 11-21-2005, 09:30 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1