+ Reply to Thread
Results 1 to 5 of 5

Create Outlook mail with dynamic .Body and static .HTMLBody

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-24-2009
    Location
    Vietnam
    MS-Off Ver
    Excel 2010
    Posts
    382

    Create Outlook mail with dynamic .Body and static .HTMLBody

    Hi,

    I am using the below code to generate an email from excel in outlook and add in some recipient details from the currently activate column. The email will be generated after clicking on a shape on top of the column and the recipients data should be taken from several rows in that column:

    Sub Mail_ThankYouNote()
    'Working in 2000-2010
        Dim OutApp As Object
        Dim outMail As Object
        Dim rng As Range
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        ActiveWorkbook.ActiveSheet.Select
        
        Set rng = Sheets("ThankYouNote_Format").Range("A1:B28").SpecialCells(xlCellTypeVisible)
        
        Set OutApp = CreateObject("Outlook.Application")
        Set outMail = OutApp.CreateItem(0)
    
            With outMail
                .To = ActiveSheet.Columns(ActiveCell.Column).Rows("12")
                .CC = ""
                .BCC = ""
                .Subject = "Your stay with us"
                .body = "Dear " & ActiveSheet.Columns(ActiveCell.Column).Rows("8") & " " & ActiveSheet.Columns(ActiveCell.Column).Rows("10") & ","
                .HTMLBody = RangetoHTML(rng)
                '.Attachments.Add ("C:\test.txt")           'Add attachments via direct path
                .Display                                    'Either use ".Send" for immediate mailing or ".Display" to show mail
            End With
    
        ' Clean up
        Set outMail = Nothing
        Set OutApp = Nothing
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
        
    End Sub
    Now my issue is following:
    1. When clicking on the shape, the cell is not activated, therefore excel doesnt know in which column I am and which rows it should take the data from.
    2. The body message is taken from my "ThankYouNote_Format" sheet and is then converted to HTML. The message body is generic, but I would need the first line of the body to be a dynamic "Dear Mr. / Ms. XXX," with the data from the column's rows.

    Suggestions are welcome
    A2k

  2. #2
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    2,060

    Re: Create Outlook mail with dynamic .Body and static .HTMLBody

    see if it helps you, add the rest of yr code ...html and attachment
    Sub Button1_Click()
    
      Dim olApp As Object
      Dim olMail As Object
      Dim rw As Integer
      Dim StringTo As String, StringSubject  As String, StringCC As String
      Dim StringBCC As String, StringBody As String
      Dim wksht As Worksheet
    
       'Set reference to Outlook and turn off ScreenUpdating and Events
        Set olApp = CreateObject("Outlook.Application")
        
        Set wksht = Worksheets("Sheet1") 'change for yr needs
        
        rw = ActiveCell.Row
    
        StringTo = wksht.Cells(rw, "a").Value
        StringCC = wksht.Cells(rw, "B").Value
        StringSubject = wksht.Cells(rw, "c").Value
        StringBody = "Dear " & wksht.Cells(rw, "h").Value & vbCrLf & vbCrLf & _
                    wksht.Cells(rw, "j").Value & vbCrLf _
                   & "Here is my own text" & wksht.Cells(rw, "G").Value & vbCrLf
    
       'Set email parts to variables
       'On Error Resume Next
        Set olMail = olApp.CreateItem(0)
    
        With olMail
          .To = StringTo
          .CC = StringCC
          .BCC = StringBCC
          .Subject = StringSubject
          .Body = StringBody
          .Display
         '.Send
        End With
    
    End Sub
    Regards, John55
    If you have issues with Code I've provided, I appreciate your feedback.
    In the event Code provided resolves your issue, please mark your Thread as SOLVED.
    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

    ...enjoy -funny parrots-

  3. #3
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Create Outlook mail with dynamic .Body and static .HTMLBody

    part 1
    ActiveSheet.Shapes("Rectangle 1").TopLeftCell.Address
    rename to whatever the shape is called
    will give address of the top left corner of your shape
    Last edited by humdingaling; 08-05-2014 at 03:31 AM. Reason: re worded
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  4. #4
    Forum Contributor
    Join Date
    03-24-2009
    Location
    Vietnam
    MS-Off Ver
    Excel 2010
    Posts
    382

    Re: Create Outlook mail with dynamic .Body and static .HTMLBody

    Nope, sorry guys but all this wont work.

    For one, I have about 200 copy&paste shapes, I dont want to go through all of them and assign individual macros where I have to define the shape name. When copying this 200 times I would have to rename from "shape1" to "shape200"... No thanks

    For John55's sample, your date is in one row and then split among coumns, my data is in a column and then split across rows. Even when adjusting that, I run into a problem when trying to send both a HTML body as well as a regular outlook body where I just want to greet the recipient ("Dear Mr. / Ms. XXX") with data from the worksheet. Its either normal body or HTMLbody...

    Thanks though

    EDIT: just played around a bit with application.caller and got a good result with this code:
    ActiveSheet.Shapes(Application.Caller).TopLeftCell.address
    Last edited by Armitage2k; 08-05-2014 at 04:44 AM.

  5. #5
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    2,060

    Re: Create Outlook mail with dynamic .Body and static .HTMLBody

    sorry, I am not a mind reader!

+ 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. [SOLVED] Change Font formatting mid-sentence of HTMLBody Outlook mail
    By askpcguy in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-31-2014, 09:47 PM
  2. Format Outlook Mail Body using VBA
    By prabhubox@yahoo.com in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-27-2014, 07:02 AM
  3. Excel VBA outlook - Range in mail body
    By dphadke@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-25-2013, 06:33 AM
  4. sending mail in outlook with image in Top center of body .
    By yogananda.muthaiah in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-25-2012, 05:06 PM
  5. Send e-mail with image on body (.html) on e-mail manager <> outlook
    By mariotnc in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-05-2012, 09:28 PM

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