none
MailItem.HtmlBody for New MailItem is Empty? RRS feed

  • Question

  • 

    Hi,

    I have an office .oft file which is an eCard with images and a text "NameLabel" which will be replaced by name of the receipient by my outlook addin.

    When user opens this .oft file, the content is displayed in the body of the mailitem. User needs to enter the To List and click my outlook addin Submit button with the following event handler.

    The problem I faced is highlighted in Bold.  It seems that the HtmlBody of my mailitem (New) is not formed.

    Please advise.

    Public Sub Button_eCard_TabNewMailMessage_OnClick(ByVal control As Office.IRibbonControl)
    
            Dim itm As Outlook.MailItem
            Dim receipient_list As String() = Nothing
            Dim subject As String = Nothing
            Dim document As Microsoft.Office.Interop.Word.Document = New Microsoft.Office.Interop.Word.Document()
            Dim currInspector As Outlook.Inspector
    
            currInspector = Globals.ThisAddIn.Application.ActiveInspector()
    
            itm = Globals.ThisAddIn.Application.ActiveInspector.CurrentItem
    
            If Not itm Is Nothing Then            
    
                subject = itm.Subject
                receipient_list = itm.To.Split(";")
    
                ' To copy the content of the .oft file into clipboard
                document.Content.Select()
                document.Application.Selection.Copy()
    
            End If
    
            ' To discard what users had entered in the mailitem.
            '
            currInspector.Close(Outlook.OlInspectorClose.olDiscard)
    
    
            ' Sending Out the eCard to each receipient
            '
            For Each receipient As String In receipient_list
    
                Dim eCardItem As Outlook.MailItem = Globals.ThisAddIn.Application.CreateItem(Outlook.OlItemType.olMailItem)
                eCardItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML
                eCardItem.Subject = subject
                eCardItem.To = receipient
                eCardItem.Importance = Outlook.OlImportance.olImportanceLow
    
                eCardItem.Display(False)
    
                ' paste the clipboard content to the body of mailitem, it works
    
                currInspector = Globals.ThisAddIn.Application.ActiveInspector()
                document = currInspector.WordEditor
                document.Content.Paste()
    
                ' I wish to change the NameLabel with the receipient Name, the the problem is
                ' it does not changed
    
                eCardItem.HTMLBody = Replace(eCardItem.HTMLBody, "NameLabel", receipient) 
    
                ' if I issue MsgBox(eCardItem.HTMLBody), is is empty           
    
    
            Next
    
    
         End SUB


    Thursday, October 1, 2015 8:08 AM

Answers

  • Hi TIONGLIP,

    Since you copy the content from Word, I suggest that you debug the code and to see the HTML string before you replacing the string to ensure the HTML string contains the target words as we expected.

    The reason that why I suggest that you check the content because Word application may contain some format code or other character.

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 2, 2015 5:30 AM
    Moderator

All replies

  • Hello,

    Try to move the following line of code out of For Each cycle:

     eCardItem.Display(False)
    

    Also you may try to call the Save method before calling the Display one.

    > The problem I faced is highlighted in Bold.  It seems that the HtmlBody of my mailitem (New) is not formed.

    You can use the HTMLBody property for replacing names and marking them as bold. Use the `<b></b>` tags to mark the text as bold.  Or you can use the Word object model to get the job done -  Selection.Font.Bold .

    P.S. I always recommend breaking the chain of property or method calls and declaring them on separate lines of code. Thus, you will be able to debug the code and see intermediate results and find a problematic property or method call easily (if any).

    Thursday, October 1, 2015 8:51 AM
  • Most of the mailitem API like save, copy are blocked by an in-house email encryption software.

    Hence I cannot perform save as what you proposed.

    I am thinking of the following workaround and need your advice.

    Since the eCard content is already in the clipboard, before I do the

    document.Content.Paste() to the individual recipient's mailitem,

    can I browse through the document.Content, and replace the "NameLabel" string within the Content, to the receipient name?

    If I can achieve this, I think it would solve my problem.

    I tried to search more info on WordEditor but not available.

    Friday, October 2, 2015 1:32 AM
  • Hi TIONGLIP,

    Since you copy the content from Word, I suggest that you debug the code and to see the HTML string before you replacing the string to ensure the HTML string contains the target words as we expected.

    The reason that why I suggest that you check the content because Word application may contain some format code or other character.

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 2, 2015 5:30 AM
    Moderator