+ Reply to Thread
Results 1 to 15 of 15

Thread: Opens Outlook from Excel using VBA and send emails

  1. #1
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Opens Outlook from Excel using VBA and send emails

    Hi,

    I have the following code, and while it is working, but I would like to tweak it around to suit my needs:

    Private Sub EmailtoTeam_Click()
    ' This example sends the last saved version of the Activeworkbook object .
        Dim OutApp As Object
        Dim OutMail As Object
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        On Error Resume Next
       ' Change the mail address and subject in the macro before you run it.
        With OutMail
            .To = "testabc@gmail.com"
            .CC = ""
            .BCC = ""
            .Subject = "test"
            .Body = "Hello World!"
            .Attachments.Add ActiveWorkbook.FullName
            .Send
        End With
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Sub
    What I want:

    Have the macro open up an instance of Outlook and manually fill in what goes in the body. The recipient's email address and the subject lines can use what's in the code above.

    The code will send the current active workbook out as an attachment, and I would like to keep this feature.

    Is this doable via a macro?

    Thanks
    Last edited by Lifeseeker; 02-10-2012 at 02:42 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Opens Outlook from Excel using VBA and send emails

    Anybody able to assist on this one?

    Thanks

  3. #3
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Opens Outlook from Excel using VBA and send emails

    Any one able to assist?

    The current code opens outlook and passes what's in the code to corresponding fields in Outlook.

    However, what I would like it to work is:

    1) Open outlook and fill the corresponding fields in Outlook
    2) have user manually hit "Send" button in Outlook.
    3) have the macro close that instance of Outlook.

    Is this doable?

    
    Private Sub EmailtoTeam_Click()
    ' This example sends the last saved version of the Activeworkbook object .
        Dim OutApp As Object
        Dim OutMail As Object
        Dim strBody As String
    
        
        Shell ("Outlook")
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(1)
    
        strBody = "Hi" & vbNewLine & vbNewLine & _
        "I have updated the tracking spreadsheet" & vbNewLine & vbNewLine & _
        "Please let me know if you have any questions." & vbNewLine & vbNewLine & _
        "Cheers!"
    
        On Error Resume Next
       ' Change the mail address and subject in the macro before you run it.
        With OutMail
            .To = "test@gmail.com"
            .CC = ""
            .BCC = ""
            .Subject = "Tracking sheet updated"
            .Body = strBody
            
        End With
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
        
    End Sub
    Thanks
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Opens Outlook from Excel using VBA and send emails

    You would need to change
    .Send
    to
    .Display
    to pop the email up for the user.

    I would not recommend quitting Outlook as you don't know whether it was already opened by the user.
    Good luck.

  5. #5
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Opens Outlook from Excel using VBA and send emails

    Wow nice! That's an easy fix.

    When it opens the Outlook, it actually opens the "Appointment" window?

    Is there any way to have it open the "Message" window with email signatures and everything?

    Thanks

    Private Sub EmailtoTeam_Click()
    ' This example sends the last saved version of the Activeworkbook object .
        Dim OutApp As Object
        Dim OutMail As Object
        Dim strBody As String
    
        
        Shell ("Outlook")
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(1)
    
        strBody = "Hi" & vbNewLine & vbNewLine & _
        "I have updated the tracking spreadsheet" & vbNewLine & vbNewLine & _
        "Please let me know if you have any questions." & vbNewLine & vbNewLine & _
        "Cheers!"
    
        On Error Resume Next
       ' Change the mail address and subject in the macro before you run it.
        With OutMail
            .To = "test"
            .CC = ""
            .BCC = ""
            .Subject = "Tracking sheet updated"
            .Body = strBody
            .display
            
        End With
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
        
    End Sub

  6. #6
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Opens Outlook from Excel using VBA and send emails

    That is because you created an appointment item! This line
    Set OutMail = OutApp.CreateItem(1)
    shoudl be
    Set OutMail = OutApp.CreateItem(0)
    Good luck.

  7. #7
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Opens Outlook from Excel using VBA and send emails

    wow nice, I wasn't aware of that.

    so I guess

       Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    by default creates appointments?

    Thanks

  8. #8
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Opens Outlook from Excel using VBA and send emails

    oh and.....I guess the code just creates a new instance of Outlook and from there displays the email window.

    What if....the Outlook is already open? I don't want it to create a new instance of Outlook if the Outlook is already running. In this case, is it possible to have it display an new email window from existing Outlook application?

  9. #9
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Opens Outlook from Excel using VBA and send emails

    It will use an open outlook instance.
    The 0 means mail item, 1 means appointment.
    Good luck.

  10. #10
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Opens Outlook from Excel using VBA and send emails

    Quote Originally Posted by OnErrorGoto0 View Post
    It will use an open outlook instance.
    The 0 means mail item, 1 means appointment.
    ah good to know indeed.

    thank you

  11. #11
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Opens Outlook from Excel using VBA and send emails

    Hi,

    I am wondering if I can put a check in the code and have it first check to see if the Outlook is already open. If the Outlook is already running, then just open up a new email window from it and execute the rest of the code. If Outlook is not yet running, first start Outlook and from it open up an "Email" window and execute the rest of the code.

    Is this possible?

    THanks

  12. #12
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Opens Outlook from Excel using VBA and send emails

    Is it not doing that anyway?
    Good luck.

  13. #13
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Opens Outlook from Excel using VBA and send emails

    Sorry should have been more clearer:

    Currently, if the Outlook is already running, while the code does open the email window, it has to open a new instance of the Outlook along with it. I think this is because the code itself actually creates a new Outlook object and from it creates an appointment type of "0"?

    So, I guess the more times users click on the button, the more instances of Outlook are created, and this might be a problem down the road now as I see it. (users probably just want to click on it once and if the Outlook is already running, open the appointment type of 0 instead of creating a new Outlook object). (Typically, users will have both Excel and Outlook running at the same time here...)

    Do you have any suggestions on this?

    Thanks

  14. #14
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Opens Outlook from Excel using VBA and send emails

    Ah, I missed the fact you have
        Shell ("Outlook")
    in your code for some reason. Remove that.
    Good luck.

  15. #15
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    423

    Re: Opens Outlook from Excel using VBA and send emails

    Nice!

    Yeah, I believe I added that shell statement initially because I wasn't understanding how the code creates the Outlook as an object, but thank you and it's working like a charm.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0