PDA

View Full Version : outlook vba format richtext



zulfahmi
04-05-2019, 08:56 AM
Here I have three files on my desktop named Personal with extensions .htm,.rtf,.txt
When I call this subroutine it should openupoutlook and with signatures from file personal
But when I call it with richtext it ignores vbnewline character as strconv detects only string and it displays signature although but without a line I.e vbnewline please help if any modification to this code


sub outlookmail()
Dim olapp as outlook.application
Dim olemail as outlook.mailitem
Dim str() as byte

Set olapp =new outlook.application
Set olemail =olapp.mailitem

With olemail
.bodyformat =olformatrichtext
.display
str() = strconv("dear someone" & vbnewline & getsigtext(olformatrichtext)
.rtfbody=str()
End with
End sub

Function Getsigtext(sigtype as olbodyformat)
dim str as string,dim sigpath as string,sigext as string,sigtext as string
Dim fso as scripting.filesystemobject
Dim ts as scripting.textstream

Sigpath = environ("userprofile") & "\Desktop\Personal" & sigext

Select case sigtype
Case olformathtml
Sigext=".htm"
Case olformatrichtext
Sigext=".rtf"
Case olformatplain
Sigext=".txt"
End select
Set fso =new scripting.filesystemobject
Set ts =fso.opentextfile(sigpath)
Sigtext = ts.readall
Getsigtext = sigtext
End function

gmayor
04-05-2019, 09:12 PM
Frankly I wouldn't do it like that. Looking at your code it appears that you are intending to run the code from an application other than Outlook, so you need to ensure Outlook is started properly and for that you should use the code from the link at the top of the code below. Then you can use the Outlook Word editor to edit the message body directly and retain the signature associated with the account e.g. as follows:


Option Explicit

Sub outlookmail()
'Use the code from http://www.rondebruin.nl/win/s1/outlook/openclose.htm to start Outlook
Dim olApp As Object
Dim olEmail As Object
Dim olInsp As Object
Dim wdDoc As Object
Dim oRng As Object
Const olFormatPlain As Long = 1
Const olFormatHTML As Long = 2
Const olFormatRichText As Long = 3

Set olApp = OutlookApp()
Set olEmail = olApp.CreateItem(0)

With olEmail
.BodyFormat = olFormatRichText
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range
oRng.Collapse 1
oRng.Text = "Dear someone," & vbCr
oRng.Collapse 0
oRng.Select
.Display
End With

Set olEmail = Nothing
Set olInsp = Nothing
Set wdDoc = Nothing
Set oRng = Nothing
Set olApp = Nothing
End Sub

zulfahmi
04-05-2019, 10:21 PM
I didn't understand your code. What is orng I.e. Wddoc.range. Where is personal file in the code. Anyway if vbcr is used whether it will create new line in rtf body?

gmayor
04-05-2019, 11:39 PM
The process uses the Outlook Word editor to create and edit the message document.

wdDoc is the message area of the message you are creating.

The document is treated as a Word document for the purpose of VBA.

oRng is the Word Range in that message document that dictates where the text is written. It is set initially to the whole of the message (in a new message that would include the default signature).

The range is then collapsed to its start, putting the text entry point at the beginning of the message (before any existing text in the message i.e. the sigature). You can then add your message to that range.

Then collapse the range to its end and select the range which puts the entry point immediately after the range, so that you may continue typing as required.

Try it! But don't forget to copy the function that accesses Outlook from the link or it won't work.