macro to change to html and set style

J

julien Touche

Hi

i'm trying to set with a macro the styles of an email on Outlook 2003.

what i want to do (in regexp+html)
/^\+ (.*)/<b>+ $1<\/b>/
/^([A-Z0-9].*)$/<hr>\n<i>$1<\/i>/

so in clear text,
if prefixed with "+ ", set the line to bold
if prefixed with a upper-case word, set a line and italic style

i didn't manage to do it with a macro.
i also need to first check mail is html type as i mostly use text one.


any help ?


thanks a lot
regards
 
M

Michael Bauer [MVP - Outlook]

Julien, the MailItem's BodyFormat property tells you the format. Read
HTMLBody and look for '+ ' with Instr. You then need to identify the tags
left and rigth from the found '+ ' that cause a line break, like <br>, <p>
etc. Then either enclose it in <b></b> or add a Style attribute.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Keep your Outlook categories organized!
http://www.shareit.com/product.html?productid=300120654&languageid=1
(German: http://www.VBOffice.net/product.html?pub=6)

Am Mon, 19 Feb 2007 20:10:06 +0100 schrieb julien Touche:
 
J

julien Touche

Michael Bauer [MVP - Outlook] wrote on 20/02/07 7:25:
Julien, the MailItem's BodyFormat property tells you the format. Read
HTMLBody and look for '+ ' with Instr. You then need to identify the tags
left and rigth from the found '+ ' that cause a line break, like <br>, <p>
etc. Then either enclose it in <b></b> or add a Style attribute.

sorry, could you precise more. i'm a beginner with outlook macro

something like:
http://www.outlookcode.com/codedetail.aspx?id=1526


thanks
regards
 
M

Michael Bauer [MVP - Outlook]

Julien, I can't read RegExp and don't know exactly what you're looking for.
AFAIK the VBScript.dll contains RegEx, so if it helps in your situaution you
might use:
Set rx = CreateObject("VBScript.RegExp")

The sample you're referring to contains almost everything I could tell you.
It shows how to detect the format and how to work with the differences.

Finding a + is very easy, e.g.:
Dim pos&
pos=Instr(1, Item.Body, "+")

If it returns a value>0 then that's the position where the character is
found. Finding an upper cased word is more complicated. Without RegExp I'd
probably loop through all the text and check each character:

Dim s$, i&

s = "Some TEXT Here"
For i = 1 To Len(s)
Select Case Asc(Mid(s, i, 1))
Case 65 To 90
Debug.Print "upper case at: " & i
Case 32
Debug.Print "blank space at: " & i
End Select
Next

Please play with that sample. It finds upper cases and blank spaces in plain
text. In HTML it might be more complicated, e.g. '&nbsp;' causes a blank
space, too.

The next difficult thing is to find a line. In plain text it's quite easy,
there are only vbCRLF or vbLF that causes a new line. In HTML is a lot more.
I can't list ad hoc all tags which causes a new line; <br>, <p> and <div>
are probably the most common ones. From the position where you found your
prefix you have to search forwards and backwards to find the tags which tell
you where the line begins and ends. You search forwards with Instr, and
backwards with InstrR.

For testing you could use a simple string like:
Dim s$
s="<p>row ONE here</p><p>row +Two here</p>"

Play with it, use different keywords/tags until your code finds everything.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Keep your Outlook categories organized!
http://www.shareit.com/product.html?productid=300120654&languageid=1
(German: http://www.VBOffice.net/product.html?pub=6)

Am Sun, 25 Feb 2007 08:59:40 +0100 schrieb julien Touche:
Michael Bauer [MVP - Outlook] wrote on 20/02/07 7:25:
Julien, the MailItem's BodyFormat property tells you the format. Read
HTMLBody and look for '+ ' with Instr. You then need to identify the tags
left and rigth from the found '+ ' that cause a line break, like <br>,
etc. Then either enclose it in <b></b> or add a Style attribute.

sorry, could you precise more. i'm a beginner with outlook macro

something like:
http://www.outlookcode.com/codedetail.aspx?id=1526


thanks
regards
 
J

julien Touche

i've find this
http://msdn2.microsoft.com/en-us/library/aa168454(office.11).aspx
strXML = Regex.Replace(strXML, "&", "&amp;")

so i make the following macro
Sub test()
Dim objItem, toBodyTag, strFinalBody
Dim olMail As Outlook.MailItem
On Error Resume Next

Set objItem = Application.ActiveInspector.CurrentItem

' convert mail to html format
strID = objItem.EntryID
Set olNS = Application.GetNamespace("MAPI")
olMail = olNS.GetItemFromID(strID)
olMail.BodyFormat = olFormatHTML
olMail.Save

' replace
objItem.HTMLBody = Regex.Replace(objItem.HTMLBody, "^+ (.*)", "<b>+
$&</b>")
objItem.HTMLBody = Regex.Replace(objItem.HTMLBody, "^+ ([A-Z ].*)",
"<hr><br /><b>+ $&</b>")

Set theMail = Nothing
Set olNS = Nothing
Set objItem = Nothing
End Sub
<<<

but it does nothing :(
not even the html format conversion.
ideas ?


thanks a lot
Regards

Julien
 
M

Michael Bauer [MVP - Outlook]

You need to save the e-mail before changing the BodyFormat else the content
gets lost.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Keep your Outlook categories organized!
http://www.shareit.com/product.html?productid=300120654&languageid=1
(German: http://www.VBOffice.net/product.html?pub=6)


Am Mon, 26 Feb 2007 19:21:55 +0100 schrieb julien Touche:
i've find this
http://msdn2.microsoft.com/en-us/library/aa168454(office.11).aspx
strXML = Regex.Replace(strXML, "&", "&amp;")

so i make the following macro
Sub test()
Dim objItem, toBodyTag, strFinalBody
Dim olMail As Outlook.MailItem
On Error Resume Next

Set objItem = Application.ActiveInspector.CurrentItem

' convert mail to html format
strID = objItem.EntryID
Set olNS = Application.GetNamespace("MAPI")
olMail = olNS.GetItemFromID(strID)
olMail.BodyFormat = olFormatHTML
olMail.Save

' replace
objItem.HTMLBody = Regex.Replace(objItem.HTMLBody, "^+ (.*)", "<b>+
$&</b>")
objItem.HTMLBody = Regex.Replace(objItem.HTMLBody, "^+ ([A-Z ].*)",
"<hr><br /><b>+ $&</b>")

Set theMail = Nothing
Set olNS = Nothing
Set objItem = Nothing
End Sub
<<<

but it does nothing :(
not even the html format conversion.
ideas ?


thanks a lot
Regards

Julien
 
J

julien Touche

Michael Bauer [MVP - Outlook] wrote on 27/02/07 7:36:
You need to save the e-mail before changing the BodyFormat else the content
gets lost.

sorry, but i don't see which code is missing.

there is
olMail.Save
after FormatConversion

something else ?

thanks a lot
 
M

Michael Bauer [MVP - Outlook]

Nothing is missing, it's just the wrong order. Save *before* changing the
format, please.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Keep your Outlook categories organized!
http://www.shareit.com/product.html?productid=300120654&languageid=1
(German: http://www.VBOffice.net/product.html?pub=6)

Am Tue, 27 Feb 2007 22:33:48 +0100 schrieb julien Touche:
Michael Bauer [MVP - Outlook] wrote on 27/02/07 7:36:
You need to save the e-mail before changing the BodyFormat else the content
gets lost.

sorry, but i don't see which code is missing.

there is
olMail.Save
after FormatConversion

something else ?

thanks a lot
 
J

julien Touche

like this ?

===
Sub test()
Dim objItem, toBodyTag, strFinalBody
Dim olMail As Outlook.MailItem
On Error Resume Next

Set objItem = Application.ActiveInspector.CurrentItem

' convert mail to html format
strID = objItem.EntryID
Set olNS = Application.GetNamespace("MAPI")
olMail = olNS.GetItemFromID(strID)
olMail.Save

olMail.BodyFormat = olFormatHTML

' replace
objItem.HTMLBody = Regex.Replace(objItem.HTMLBody, "^+ (.*)", "<b>+
$&</b>")
objItem.HTMLBody = Regex.Replace(objItem.HTMLBody, "^+ ([A-Z ].*)",
"<hr><br /><b>+ $&</b>")

Set theMail = Nothing
Set olNS = Nothing
Set objItem = Nothing
End Sub
===


Regards

Julien
 
J

julien Touche

Michael Bauer [MVP - Outlook] wrote on 2/03/07 7:00:
Yes, that could work. BTW: After you got the ActiveInspector.CurrentItem,
why do call GetItemFromID?

no this doesn't :(
GetItemFromID is not necessary (from a copy/paste)
 
M

Michael Bauer [MVP - Outlook]

Julien, *what* doesn't work? As I told you, I can't help you with RegExp.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Keep your Outlook categories organized!
http://www.shareit.com/product.html?productid=300120654&languageid=1
(German: http://www.VBOffice.net/product.html?pub=6)

Am Sat, 03 Mar 2007 15:20:33 +0100 schrieb julien Touche:
Michael Bauer [MVP - Outlook] wrote on 2/03/07 7:00:
Yes, that could work. BTW: After you got the ActiveInspector.CurrentItem,
why do call GetItemFromID?

no this doesn't :(
GetItemFromID is not necessary (from a copy/paste)
 
J

julien Touche

Michael Bauer [MVP - Outlook] wrote on 3/03/07 16:45:
Julien, *what* doesn't work? As I told you, I can't help you with RegExp.

all, when i execute the macro, nothing happen:
- no change of format txt -> html
- no change of text

thanks a lot to you ... but are you alone to help others here ?
 
M

Michael Bauer [MVP - Outlook]

Try just to convert the format:

Sub test()
Dim olMail As Outlook.MailItem
' On Error Resume Next

Set olMail = Application.ActiveInspector.CurrentItem
olMail.Save
olMail.BodyFormat = olFormatHTML

End Sub

Whether the format changes or not can you see at the Inspectors caption. If
that works you might go on wth your RegExp. But instead of:

objItem.HTMLBody

you have to use:

olMail.HTMLBody


--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Keep your Outlook categories organized!
http://www.shareit.com/product.html?productid=300120654&languageid=1
(German: http://www.VBOffice.net/product.html?pub=6)

Am Sun, 04 Mar 2007 11:50:24 +0100 schrieb julien Touche:
Michael Bauer [MVP - Outlook] wrote on 3/03/07 16:45:
Julien, *what* doesn't work? As I told you, I can't help you with RegExp.

all, when i execute the macro, nothing happen:
- no change of format txt -> html
- no change of text

thanks a lot to you ... but are you alone to help others here ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top