Home > Software engineering >  VBA - How to insert Horizontal Line in Outlook TaskItem
VBA - How to insert Horizontal Line in Outlook TaskItem

Time:09-14

I want to insert an horizontal line, eg., before a text in a TaskItem body. It is possible to do this using Insert menu and clicking the horizontal line button on symbols group. But, how to code this?

This is what I've tried:

Sub NewTask()

 Dim objTask As Outlook.TaskItem

 Set objTask = Application.CreateItem(olTaskItem)

 With objTask

        .Subject = "Example Task"
        .Body = ??What to put in here?? & "Example Body"
        .Save

End With

Set objTask = Nothing

End Sub

This post show how to reach this for Mails. As far as I know, MailItem have Html body property whereas TaskItem does not have.

Thanks in advance.

CodePudding user response:

Instead, you need to use the TaskItem.RTFBody property which returns or sets a byte array that represents the body of the Microsoft Outlook item in Rich Text Format.

The code for a horizontal line is the following:

\pard \brdrb \brdrs\brdrw10\brsp20 {\fs4\~}\par \pard

To set up the RTF formatting in Outlook you may use the following code:

.BodyFormat = olFormatRichText
.Body = StrConv("your RTF string", vbFromUnicode) 'Convert RTF string to byte array

Be aware, The Outlook object model supports three main ways of customizing the message body:

  1. The Body property returns or sets a string representing the clear-text body of the Outlook item.
  2. The HTMLBody property of the MailItem class returns or sets a string representing the HTML body of the specified item. Setting the HTMLBody property will always update the Body property immediately. For example:
     Sub CreateHTMLMail() 
       'Creates a new e-mail item and modifies its properties. 
       Dim objMail As Outlook.MailItem 
       'Create e-mail item 
       Set objMail = Application.CreateItem(olMailItem) 
       With objMail 
        'Set body format to HTML 
        .BodyFormat = olFormatHTML 
        .HTMLBody = "<HTML><BODY>Enter the message <a href="http://google.com">text</a> here. </BODY></HTML>" 
        .Display 
       End With 
     End Sub
  1. The Word object model can be used for dealing with message bodies. See Chapter 17: Working with Item Bodies for more information.

Note, the MailItem.BodyFormat property allows you to programmatically change the editor that is used for the body of an item.

CodePudding user response:

I was not able to reach the solution through RTFBody. However the Word object model approach, pointed by Eugene Astafiev, helped me to solve the issue.

First of all: Add reference to Word library in VBA Editor, Tools, References

And this is the example sub working:


Sub NewTask()

 Dim objTask As Outlook.TaskItem
 Dim objInsp As Inspector
 Dim objDoc As Word.Document
 Dim objSel As Word.Selection

 Set objTask = Application.CreateItem(olTaskItem)
 Set objInsp = objTask.GetInspector
 Set objDoc = objInsp.WordEditor
 Set objSel = objDoc.Windows(1).Selection

 With objTask

        .Subject = "Example Task"
        objSel.InsertAfter "Example Body"
        objDoc.InlineShapes.AddHorizontalLineStandard
        .Display
        .Save

End With

Set objTask = Nothing
Set objInsp = Nothing
Set objDoc = Nothing
Set objSel = Nothing

End Sub
  • Related