emailing through outlook via macro

Cease

Board Regular
Joined
Dec 28, 2010
Messages
112
Hey guys,
I try to find these things before posting to save you guys the work...

I'm still very much a newbie with code, but I've found this code:
Code:
Sub SendEmail()
    Dim OutlookApp As Object
    Dim MItem As Object
    Dim cell As Range
    Dim email_ As String
    Dim subject_ As String
    Dim body_ As String
    Dim attach_ As String
 
     'Create  Outlook object
    Set OutlookApp = CreateObject("Outlook.Application")
 
     ' Loop through the rows
    For Each cell In Columns("a").Cells.SpecialCells(xlCellTypeConstants)
 
        email_ = cell.Value
        subject_ = cell.Offset(0, 1).Value
        body_ = cell.Offset(0, 2).Value
 
 
         'Create Mail Item and send it
        Set MItem = OutlookApp.CreateItem(0)
        With MItem
            .To = email_
            .Subject = subject_
            .Body = body_
            .Send
        End With
    Next
End Sub

Where column A= email address, B= subject and C=body - and it works great.

What do I need to change to have it pull data from a different sheet?

I'm trying to have a macro on sheet 1 email data from sheet 2?

Also what do I need to change if there are multiple cells in column c that I want in the body of the email?

Any help or direction would be greatly appreciated!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Ok, I found how to have it mail from another sheet by having the macro on the sheet with the data to send, and a link to it from the initial page.

Can anyone tell me what I need to change in the code to have multiple cells in the body of the email?

Also I'm running excel 2007.

Thanks again in advance for any help!
 
Upvote 0
I use this, The parts in red should help you. Good luck. I pieced this together from tips I picked up on Mr Excel. This site has helped me a great deal and I hope this helps you

Code:
Sub Mail_yank_case_Outlook()
'Working in Office 2000-2010
    Dim OutApp As Object
    Dim OutMail As Object
    [COLOR=Red]Dim strbody As String[/COLOR]
    Dim Email As String
    Dim TechName As String
    Dim LDAPName As String
        Dim r As Integer, x As Double
    For r = 2 To 2 'data in rows 2
'       Get the email address tech name and current user name from ldap
' LDAP name is pulled from macro that runs when this spreadsheet is opened and placed in row 2
        Email = Cells(r, 5)
        TechName = Cells(r, 2)
        LDAPName = Cells(r, 7)
        



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

    [COLOR=Red]strbody = "Hi," & vbNewLine & vbNewLine & _
              "I am part of the level 2 deskside team.  We've received your ticket, Case # ______ and I have placed the case in my list while " & TechName & " is out." & vbNewLine & _
              "If you have any questions, please feel free to reply to this email and I will be happy to assist you" & vbNewLine & _
              "Thank you, " & vbNewLine & _
              LDAPName[/COLOR]

    On Error Resume Next
    With OutMail
        .To = "Change to Users SSO for case you are assigning"
        .CC = Email
        .BCC = ""
        .Subject = "Helpdesk Case"
        [COLOR=Red].Body = strbody[/COLOR]
        'You can add a file like this
        '.Attachments.Add ("C:\test.txt")
        .Display   'or use .Send
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
    Next r
End Sub
 
Upvote 0
Thanks for the reply. It's right along what I'm going for (and I'll keep this as a reference cause that is awesome), but I can't seem to figure out the syntax to change to do what it is I'm looking for.

Here's what I'm trying:

Rich (BB code):
Sub SendEmail2()
Dim OutlookApp As Object
Dim MItem As Object
Dim cell As Range
Dim email_ As String
Dim subject_ As String
Dim body_ As String
Dim attach_ As String
 
'Create Outlook object
Set OutlookApp = CreateObject("Outlook.Application")
 
' Loop through the rows
For Each cell In Range("a3:a5").Cells.SpecialCells(xlCellTypeConstants)
 
email_ = cell.Value
subject_ = cell.Offset(0, 1).Value
body1_ = range.Offset(0, 2)&(1, 2).Value
 
 
'Create Mail Item and send it
Set MItem = OutlookApp.CreateItem(0)
With MItem
.To = email_
.Subject = subject_
.Body = body1_
.Display
End With
Next
End Sub

Just trying to get the body to be cells c3:c4
Something like that.

Thanks again,
 
Last edited:
Upvote 0
Thanks again DippNCope!

With the link and your previous post I was able to piece together this little number:
Code:
Sub SendEmail2()
    Dim OutlookApp As Object
    Dim MItem As Object
    Dim cell As Range
    Dim email_ As String
    Dim subject_ As String
    Dim body_ As String
    Dim attach_ As String
 
     'Create  Outlook object
    Set OutlookApp = CreateObject("Outlook.Application")
 
     ' Loop through the rows
    For Each cell In Range("a3:a5").Cells.SpecialCells(xlCellTypeConstants)
 
        email_ = cell.Value
        subject_ = cell.Offset(0, 1).Value
        body1_ = Cells.Range("c3").Value
        body2_ = Cells.Range("c4").Value
 
 
         'Create Mail Item and send it
        Set MItem = OutlookApp.CreateItem(0)
        With MItem
            .To = email_
            .Subject = subject_
            .Body = body1_ & vbNewLine & body2_
            .Display
        End With
    Next
End Sub
posts multiple cells one under the other in an email.

Now I have another question but I'll set that as a new post; thanks again for all your help!
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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