+ Reply to Thread
Results 1 to 14 of 14

Outlook Mail Automation Modification

Hybrid View

  1. #1
    Registered User
    Join Date
    12-19-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    53

    Outlook Mail Automation Modification

    Hi All,

    I have modified a VBA code from Ron de Bruin Excel Automation.

    Its working fine but I want to add something more in this. Please help me to add below steps.

    ".to" is taking value from column B of excel Sheet 1.
    1) So I want ".cc" should take value from column D,
    2) ".bcc" should take value from column E and ".
    3) ".Subject" should take value from column F.

    Below is modified code from http://www.rondebruin.nl/win/s1/outlook/amail6.htm

    Sub Mail_Outlook_With_Signature_Html()
    
        Dim OutApp As Object
        Dim OutMail As Object
        Dim strbody As String
        Dim sh As Worksheet
        Dim cell As Range
        Dim FileCell As Range
        Dim rng As Range
        
        With Application
            .EnableEvents = False
            .ScreenUpdating = False
        End With
        
        Set sh = Sheets("Sheet1")
        
        Set OutApp = CreateObject("Outlook.Application")
            
        For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)
    
            'Enter the path/file names in the C:Z column in each row
            Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")
    
            If cell.Value Like "?*@?*.?*" And _
               Application.WorksheetFunction.CountA(rng) > 0 Then
                Set OutMail = OutApp.CreateItem(0)
        
        strbody = "<br>Hello,</br>" & _
                  "<p>My name is Liz.<br></p>" & _
                  "<p></p>"
    
        On Error Resume Next
    
        With OutMail
            .Display
            .to = cell.Value
            .CC = ""
            .BCC = ""
            .Subject = ""
            .HTMLBody = strbody & "<br>" & .HTMLBody
        
        For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                        If Trim(FileCell) <> "" Then
                            If Dir(FileCell.Value) <> "" Then
                                .Attachments.Add FileCell.Value
                            End If
                        End If
                    Next FileCell
    
                    .Send  'Or use .Display
                End With
                
                On Error GoTo 0
                Set OutMail = Nothing
                Set OutApp = Nothing
            End If
        Next cell
    
        Set OutApp = Nothing
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
        
    End Sub
    Thanks,
    Liz.
    Last edited by Liz_Biz; 04-15-2014 at 08:46 AM.

  2. #2
    Forum Contributor
    Join Date
    02-28-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    187

    Re: Outlook Mail Automation Modification

    Hi there, how to answer your question depends on whether or not there are equal numbers of entries in columns B,D,E? It will also depend on whether these are the only entries on that worksheet and that you want to send the same e-mail to all recipients.

    If so, then I would use:

    Dim Tomail,CCmail,BCCmail,Subjectmail as String
    Worksheets("YOURWORKSHEETNAME").Activate
    
    lastrow = Activesheet.UsedRange.Rows.Count
    
    For i = 2 to lastrow 'Assume you have headers in row 1
    Tomail = Tomail & Cells(i,2).Value & ";"
    CCmail = CCmail  & Cells(i,4).Value & ";"
    BCCmail = BCCMaill & Cells(i,5).Value & ";"
    Next i
    
    Subjectmail = Cells(2,6).Value 'Assuming only one subject
    
    ' . . . . . Skip to e-mail section . . .
         .To = Tomail
         .CC = CCmail
         .BCC = BCCMail
         .Subject = Subjectmail
    Hope this helps.
    Last edited by TKCZBW; 04-14-2014 at 08:43 AM. Reason: Fat finger error

  3. #3
    Registered User
    Join Date
    12-19-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    53

    Re: Outlook Mail Automation Modification

    Hi TKCZBW,

    Its not working. Nothing is happening here. No error no results.

    Thanks,
    Liz

  4. #4
    Forum Contributor
    Join Date
    02-28-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    187

    Re: Outlook Mail Automation Modification

    Hmm. Could you paste your new code in and I will have a look?

  5. #5
    Registered User
    Join Date
    12-19-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    53

    Re: Outlook Mail Automation Modification

    Please find attachment with code.
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    02-28-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    187

    Re: Outlook Mail Automation Modification

    Sorry I can't download sheets whilst at the office, if you post the code like you did first time I'll do my best to fix.

  7. #7
    Forum Contributor
    Join Date
    02-28-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    187

    Re: Outlook Mail Automation Modification

    You haven't defined your range (rng).

    Also, you appear to be using a 'dir', a file reference and (presumably although I can't see it) a folder reference without any reference to the File System Object. The result of this is that when you append 'FileCell.Value', FileCell has no value.

    Either of the above should fix the problem, although for a better understanding of using files/folders I'd go for the second one.

    I'd recommend going back to Ron de Bruin's website or the MSDN page on FSOs. His code is very good, but can't be just copy and pasted without making sure all variables are correctly populated and all the objects are properly set.

    I'd be obliged if you could accept my answer as that has dealt with the To/CC/BCC/Subject of the e-mail itself, per the initial question.

    Best,
    TKCZBW

  8. #8
    Registered User
    Join Date
    12-19-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    53

    Re: Outlook Mail Automation Modification

    Thanks TKCZBW!! I have reached almost at result. Below code is working but with some issues. Please look into this.

    1) Code is working only for 2nd row.
    2) CC is taking all values in that column except single cell value.
    3) Some times getting error 91, "object variable or with variable not set" for code "Set OutMail = OutApp.CreateItem(0)"

    Sheet's structure -
    Names of the people E-mail addresses Filenames CC BCC Subject

    
    Sub Mail_Outlook_With_Signature_Html()
    
        Dim OutApp As Object
        Dim OutMail As Object
        Dim strbody As String
        Dim sh As Worksheet
        Dim cell As Range
        Dim FileCell As Range
        Dim rng As Range
        Dim Tomail, CCmail, BCCmail, Subjectmail As String
        
        With Application
            .EnableEvents = False
            .ScreenUpdating = False
        End With
        
        Set sh = Sheets("Sheet1")
        
        Set OutApp = CreateObject("Outlook.Application")
            
        For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)
    
            'Enter the path/file names in the C:Z column in each row
            Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")
    
            If cell.Value Like "?*@?*.?*" And _
               Application.WorksheetFunction.CountA(rng) > 0 Then
                Set OutMail = OutApp.CreateItem(0)
        
        lastrow = ActiveSheet.UsedRange.Rows.Count
    
        For i = 2 To lastrow 'Assume you have headers in row 1
        CCmail = CCmail & Cells(i, 4).Value & ";"
        BCCmail = BCCMaill & Cells(i, 5).Value & ";"
        Next i
    
    Subjectmail = Cells(2, 6).Value 'Assuming only one subject
    
        strbody = "<br>Hello,</br>" & _
                  "<p>My name is Liz.<br></p>" & _
                  "<p></p>"
    
        On Error Resume Next
    
        With OutMail
            .Display
            .To = cell.Value
            .CC = CCmail
            .BCC = BCCmail
            .Subject = Subjectmail
            .HTMLBody = strbody & "<br>" & .HTMLBody
        
        For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                        If Trim(FileCell) <> "" Then
                            If Dir(FileCell.Value) <> "" Then
                                .Attachments.Add FileCell.Value
                            End If
                        End If
                    Next FileCell
    
                    .Send  'Or use .Display
                End With
                
                On Error GoTo 0
                Set OutMail = Nothing
                Set OutApp = Nothing
            End If
        Next cell
    
        Set OutApp = Nothing
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
        
    End Sub
    Thanks,
    Liz

  9. #9
    Forum Contributor
    Join Date
    02-28-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    187

    Re: Outlook Mail Automation Modification

    Liz,

    I'm not 100% sure what you want the code to do, some of it appears to be redundant (line 21 for instance, what is it meant to be doing for each cell in column B?).

    You are setting the range of the filepath range to encompass all columns between C and Z, which will catch not only file paths but also CCs,BCCs and Subjects. This should be .Range(Cells(2,3),Cells(lastrow,3)).

    Declare lastrow as soon as you open the sheet, this means you can use it immediately.

    You've changed my suggested code about the '.to' section of the with Outmail section, this won't work as it is only pointing to one cell (as opposed to my method which concatenates all relevant cells in column B.

    I'd recommend going over Ron de Bruin's webpage again, step by step, as it is much better than I in explaining e-mail automation. Fundamentally, it boils down to this:

    For all parts of the e-mail that have more than one component, saving attachments, (i.e. to,CC, BCC) you can concatenate these into a string, delimited by a semi-colon, then just use that string as the value within the 'With Outmail' section.

    As I've answered your initial question, I'd be grateful if you could mark the question as 'solved' and add reputation if any of my advice has helped.

    Best,

    TKCZBW
    Last edited by TKCZBW; 04-15-2014 at 08:45 AM.

  10. #10
    Registered User
    Join Date
    12-19-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    53

    Re: Outlook Mail Automation Modification

    ok. Done!!

  11. #11
    Forum Contributor
    Join Date
    02-28-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    187

    Re: Outlook Mail Automation Modification

    Thanks.

    Hope I was of some help!

+ 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. Outlook 2007, Referencing mail Item created from outlook template
    By thusidie in forum Outlook Programming / VBA / Macros
    Replies: 2
    Last Post: 07-21-2014, 04:33 PM
  2. 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
  3. code to attach the draft mail in new compose mail as attachment in outlook 2010
    By priya1987 in forum Outlook Programming / VBA / Macros
    Replies: 0
    Last Post: 10-10-2012, 08:38 AM
  4. [SOLVED] Outlook Automation Error Problem - Can't locate Outlook Module
    By Allan P. London in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-14-2005, 06:05 PM
  5. [SOLVED] Excel Template modification using automation
    By Terry Holland in forum Excel General
    Replies: 1
    Last Post: 03-30-2005, 02:06 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