+ Reply to Thread
Results 1 to 8 of 8

Thread: Sending outlook mail using VBA

  1. #1
    Registered User
    Join Date
    09-03-2010
    Location
    england
    MS-Off Ver
    Excel 2010
    Posts
    19

    Sending outlook mail using VBA

    Hi,

    I want an email to be sent, which contains a link to the workbook once a user has filled in a userform and clicks a button on the userform. I have the following code:

    
    Dim outapp As Object
    Dim outmail As Object
    
    Set outapp = GetObject("outlook.application")
    If outapp Is Nothing Then Set outapp = CreateObject("outlook.application")
    
    outapp.session.logon
    Set outmail = outapp.createitem(0)
    
    On Error Resume Next
    With outmail
        .to = "example@hotmail.com"
        .cc = ""
        .bcc = ""
        .Subject = "Number" & UserForm2.TextBox1.Text
        .Body = "Hi," & vbCrLf & _
            "I need you to verify details below" & vbCrLf & _
            "<file:///c:\\NameOfWorkbook>" & vbCrLf & _
            "Thanks," & vbCrLf & vbCrLf _
        .send
        .Readreceiptrequested = False
    End With
    outmail.display
    
    End Sub
    However, I'm always getting invalid qualifier or variable not defined. Could someone please help? Thanks in advance! =)

  2. #2
    Forum Guru mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    3,004

    Re: Sending outlook mail using VBA

    The continuation character in

    "Thanks," & vbCrLf & vbCrLf _
    might be to blame. Try removing it.
    Martin

    Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.

    If my solution has saved you time and/or money, please consider donating to Cancer Research UK.

  3. #3
    Registered User
    Join Date
    09-03-2010
    Location
    england
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Sending outlook mail using VBA

    It didn't work =(
    Now I'm getting:
    Run-error
    Automation error
    invalid syntax

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Sending outlook mail using VBA

    Hello icylemontea,

    I am not really familiar with the Mac Excel 2004, other than there are many differences between the PC and Mac, especially in the use of advanced code. I have contacted another moderator who is very familiar with Macs to take a look at your post.

    Just so we are all on the same page, do you want to include a hyperlink in the email body or do you want to include the URL text? It was not clear to me which you wanted.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    09-03-2010
    Location
    england
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Sending outlook mail using VBA

    Hi Leith,

    Sorry, I'm actually on a different computer using excel 2010 at the moment.
    Regarding the body of the email, I wanted a hyperlink to the excel file.

    Thanks for your help

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Sending outlook mail using VBA

    Hello icylemontea,

    Please update your profile to include Excel 2010. It will help everybody when answering any questions you have. As for the link, it has to added in HTML, which you can do in Outlook. Instead of Body you have to use htmlBody. The message must be a string that includes the proper HTML tags for formatting and to provide the link.I can code this for you if you want.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  7. #7
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Sending outlook mail using VBA

    It's rather curious to have the property .Readreceiptrequested
    set to False after sending the mail.
    I don't know whether it's possible to show the mail after sending it (outlook.display).
    I also fear that you didn't put something into 'NameOfWorkbook'
    try this:

    Sub snb()
      With CreateObject("outlook.application").createitem(0)
        .to = "example@hotmail.com"
        .Subject = "Number"
        .Body = replace("Hi,#I need you to verify details below#<file:///c:\\" & thisworkbook.name & ">#Thanks,##","#" vbLf)
        .send
      End With
    End Sub
    Last edited by snb; 09-12-2010 at 05:31 AM.



  8. #8
    Registered User
    Join Date
    09-03-2010
    Location
    england
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Sending outlook mail using VBA

    Hi,

    I changed

     Set outapp = GetObject("outlook.application")
    to

    Set OutApp = GetObject("", "Outlook.application")
    and that seems to have done the trick.
    Thanks guys for your help.

    Leith, would you mind showing me the coding for .htmlbody?
    Thanks a lot!

+ 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