Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending Email via Outlook 3

Status
Not open for further replies.

RPEN

Programmer
Aug 23, 2007
33
CA
Is it possible to send an email via Outlook through Extra Basic?
 
Yes, but you will be promted as to whether or not you'd really like to send the e-mail your script creates.

[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.


 
That might be acceptable. Is it possible to save the sent email in the senders 'sent' folder?

Do you have any sample code?

Thanks.
 
Code:
Sub SendMail()
    Dim oOutlook         As Object
    Dim oMailItem       As Object
    
    On Error GoTo SendMail_Err
    
    Set oOutlook = CreateObject("Outlook.Application")
    Set oMailItem = oOutlook.CreateItem(olMailItem)
    With oMailItem
        .To = "Email@somthing.com"
        .Subject = "Bow Chicka Wow Wow"
        .Body = "My big chicken is awesome."
        .Send
    End With

    'Tidy Up
    Set oOutlook = Nothing
    Set oMailItem = Nothing
    Exit Sub
    
SendMail_Err:
    MsgBox "No Worky"
End Sub

Sub main
    SendMail
End sub
Will save in senders sent folder.

[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.


 
Thanks, that works great! And it doesn't prompt either, just sends right away. Although I may have to change the Subject line to something more appropriate than "Bow Chicka Wow Wow".
[thumbsup]
 
Is it possible to have the email open up so that the user can review and modify the email before sending?

Thanks
 
Code:
[COLOR=green].display[/color]
[COLOR=green]'[/color].Send

[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.


 
Ok, I think I've got it, I just changed ".Send" to ".Display". It works, the email comes up, but I'm getting this error on the line that has ".Display":

no such property or method
Line number: 42
Stopping macro playback


Any idea why I'm getting this error?

Here's my code (your code):

Code:
Sub Main()
    

    Dim oOutlook        As Object
    Dim oMailItem       As Object
    
    On Error GoTo SendMail_Err
    
    Set oOutlook = CreateObject("Outlook.Application")
    Set oMailItem = oOutlook.CreateItem(olMailItem)
    With oMailItem
        .To = "myemail@mts.net"
        .Subject = "Email Test"
        .Body = "This email was sent from a macro."
        .Display
    End With

    'Tidy Up
    Set oOutlook = Nothing
    Set oMailItem = Nothing
    Exit Sub
    
SendMail_Err:
    MsgBox "ERROR!"

End Sub
 
Thanks, I was aware of that, but I need to scrape fields off of several application screens, as well as retrieve info from a falat file nad a spreadsheet, then combine that info into an email.
 
Odd I get the same error when running the script through my EB compiler. It works in VBA for the 2003 and 2007 version of Outlook. I'm posting a link for info regarding the other methods/properties of the Mailitem Object below. I've noticed other odities when using objects in EB I'll add it to the list. You can either handle the error (as the result is what you want despite it) or write it in VB or VBA if one of the other apps your scraping is MS product with acess to VBA.


[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.
 
Hello All
I have created a short script along the same lines as MrMilson but I was wondering if there is a way I can attach a document to send with the email

Your help would be appreciated
 
before .send

.Attachments.Add (YourFile)

[small]Sometimes you gotta leave your zone of safety. You have to manufacture Inspirado. You gotta get out of the apartment. You've got to run with the wolves. You've got to dive into the ocean and fight with the sharks. Or just treat yourself to a delicious hot fudge sundae........ with nuts. - Jack Black[/small]
 
Hello,

I am using a macro which uses the above code and is similar in principle to this thread.
My issue however is that I require the email to have several lines of text for the body, and I can't figure out how to get the Macro to drop to a new line?!?

For example:

The text should read

"Good Day,

Please be advised blah blah blah......
If we have not recieved blah blah blah....... "

Except all I get is:

"Good Day, Please be advised blah blah blah...... If we have not recieved blah blah blah......."

All the text in the Body of the email is on one line. I just can't get the format correct.

Does anyone know, would the text for the main body of the email have to be stipulated in a string, and called in a function?
Or is there a simpler way to get Outlook to recognise a new line?

 
RPEN said:
Ok, I think I've got it, I just changed ".Send" to ".Display". It works, the email comes up, but I'm getting this error on the line that has ".Display":

no such property or method
Line number: 42
Stopping macro playback

Any idea why I'm getting this error?

I discovered this a while back and the odd thing to me is that although it generates an error, it still displays the email. I just use On Error Resume Next and it works fine.

Christadelphian said:
Does anyone know, would the text for the main body of the email have to be stipulated in a string, and called in a function?
Or is there a simpler way to get Outlook to recognise a new line?

I think there are two ways to do this. One is with carriage returns (Chr(13)) and the other is with <br> in HTML. I've shortened the code below to just pertain to the parts I'm talking about.

Code:
With oMailItem
    .To = "youremail@domain.com"
    .Subject = "Your Subject"
    .Body = "Good Day," & Chr(13) & Chr(13) & _
        "Please be advised blah blah blah......" & Chr(13) & _
        "If we have not recieved blah blah blah......."
    .Display
End With

HTML:
Code:
With oMailItem
    .To = "youremail@domain.com"
    .Subject = "Your Subject"
    .BodyFormat = 2 'HTML format
    .HTMLBody = "Good Day,<br><br>" & _
        "Please be advised blah blah blah......<br>" & _
        "If we have not recieved blah blah blah.......<br>"    
    .Display
End With

With HTML you can change font and font size etc. I'm not an HTML expert but you can find some examples online.
 
Thanks StreetProg,

That has done the trick nicely. Never thought of a Carriage Return. Couldn't get Extra! to recognise the HTML tag though.

Now all I have to do is work out how to get outlook to add the company disclaimer at the bottom!! :eek:)

Much appreciated!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top