View Full Version : Sending email syntax


Alc
10-23-2007, 07:34 AM
I'm using the following to create an email and attach a report to it (str_body, str_recipient and lb_readreceipt are set elsewhere).

Set objOutlook = CreateObject("Outlook.Application")

Set ObjOutlookMsg = objOutlook.CreateItem(olMailItem)

With ObjOutlookMsg
Set objoutlookattach = .Attachments.Add(str_New_File)
.To = str_Recipient
.Subject = str_Subject
.Body = str_Body
.readreceiptrequested = lb_ReadReceipt
.Display 'to display the message instead of sending it
End With

This works fine, but now my manager wants to add various other bits and pieces to it. I can't find an example to base my work on, so does anyone know how I could easily amend the above to do any of the following?
1) Insert a hyperlink to a website.Ideally, without displaying the entire address (e.g. display 'google' but open www.google.com, when clicked)

2) Insert a hyperlink to an email address

3) Highlight certain words in the str_Body string (e.g. highlight some in yellow, some in green, etc.)

I've tried recording a macro in Outlook and stepping into it, to copy the code, but I'm not having the same success I have when doing that with Excel.

Thanks in advance for any suggestions.

KenHigg
10-23-2007, 07:38 AM
My first shot would be to include html tags for all of this. And you may want to point out to your boss that you'll always be at the mercy of the recipients email application when it comes to formatting...

:)
ken

Alc
10-23-2007, 07:41 AM
My first shot would be to include html tags for all of this. And you may want to point out to your boss that you'll always be at the mercy of the recipients email application when it comes to formatting...

:)
ken
Thanks for the reply.

Fortunately, the reports all go out internally, so it will always be Outlook that sends and receives them.

Showing my ignorance, by html tags, do you mean things like <img> ?

KenHigg
10-23-2007, 07:46 AM
Yes...

May not work but it's worth giving a quick try. Try it with something real simple like <b></b> ???

:)
ken

Alc
10-23-2007, 07:49 AM
Thanks, I shall give it a go and see how it works out.

Alc
10-23-2007, 09:30 AM
*phew* Okay, after much hair-pulling, it's working :).
Ken, your HTML pointer made all the difference and it was plain sailing from there.

In case it's of use to anyone else, I did the following

1) Created the body text of the email and stored it, as type 'memo', in a new table. This text included all the necessary html tags to highlight words, set paragraphs, create hyperlinks, etc.

2) When setting the value for str_Body, I made sure that it was being fetched from ths new table, based on the name of the report to be attached.

3) Alter the existing 'create email' code as follows:

Set objOutlook = CreateObject("Outlook.Application")

Set ObjOutlookMsg = objOutlook.CreateItem(olMailItem)

With ObjOutlookMsg
Set objoutlookattach = .Attachments.Add(str_New_File)
.To = str_Recipient
.Subject = str_Subject
'.Body = str_Body
.readreceiptrequested = lb_ReadReceipt
.Display 'to display the message instead of sending it
.BodyFormat = olFormatHTML
.HTMLBody = str_Body
End With

Note: Without this line to specify HTML formatting is being used,
.BodyFormat = olFormatHTML

the hyperlinks still work, but you see the code itself, rather than the desired text, plus you lose any paragraphs, line breaks, etc.

KenHigg
10-23-2007, 10:16 AM
Cool.

Also, I have created an html page from a report, strategically placing html tags and exporting the report as .txt then renaming it with an .html extension...

:)
ken

Alc
10-24-2007, 06:22 AM
Okay, slightly different - but connected - question.

How can I set a 'follow up' flag on one of my created emails?
I know how to do it manually in Outlook, but if I use my normal Excel fallback of starting a macro recording, carrying out the required actions, then going into the macro's code to see the steps, it's blank.

KeithG
10-24-2007, 06:33 AM
check out the FlagRequest and FlagDueBy properties of the mailitem object. You can look thme up in Outlook VBA help

Alc
10-24-2007, 06:37 AM
Many thanks.
This would be so much easier if I knew what these properties were all called (but I suppose it would take out half the 'fun').