Outlook ReportItem.Body returning messed up encoding
Asked Answered
N

3

6

If certain users automate the Outlook Client to view bounce backs/ReportItems in a shared inbox, rather than returning the clear text of the message as indicated by the documentation there is a unicode string that has been parsed as a UTF-8 string - so it looks like Chinese.

I can get past that with some code, but the additional issue is that this change occurs in Outlook as well for all users with access to that inbox. The message itself as viewed in Outlook appears as Chinese characters - the original unicode html parsed as UTF-8.

We are using the normal methods to access the report item:

For Counter as Integer = Inbox.Items.Count To 1 Step -1
    Dim Report As Outlook.ReportItem = Inbox.Items(Counter)
    Dim Body As String = Report.Body

The last line is where we get the garbled text In VBA it attempts to parse it as ASCII and returns a large block of "?". In .Net it returns the value parsed as UTF-8 and we get the characters that appear Chinese. In either case the report item in the inbox begins displaying as Chinese characters and continues to do so for all users of that inbox.

Narghile answered 13/3, 2015 at 19:20 Comment(0)
H
8

I just had this happen to my VBA function in Outlook that processes email bounce backs for orders and marks those orders as requiring attention. The original email in outlook looks fine but when I attempt to process it, the characters change to Chinese and Report.Body just shows question marks.

I found using StrConv to convert to Unicode could get me the correct body contents for processing.

Dim strBody as String
strBody = StrConv(Report.Body, vbUnicode)
Homovec answered 14/4, 2015 at 4:41 Comment(3)
This is pretty much what I discovered as well, that the unicode is displaying as UTF8 for some reason. My issue remains though. We can access the message programatically, but we still need to be able to have the message in the inbox in a human readable manner, so what I'm shooting for is something that won't cause the issue to begin with.Narghile
Here's the c# equivalent I used: System.Text.Encoding.UTF8.GetString(new UnicodeEncoding().GetBytes(report.Body))Altruist
StrConv( vbUnicode ) doesn't exist in .NET. Here's the equivalent that I used: Function UnsquishString(ByVal squished As String) As String Dim len As Double = str.Length * 2.0 Dim b(len - 1) As Byte System.Buffer.BlockCopy(str.ToCharArray(), 0, b, 0, b.Length) Return System.Text.Encoding.GetEncoding(1252).GetString(b) End FunctionPlaymate
M
0

I came across this issue and I've written a function that solves the issue for me. I thought I'd share it here in case it's of use to anyone else.

Private Sub Example()
    Dim Item As Object
    
    Set Item = Application.ActiveExplorer.Selection(1)
    
    Debug.Print ItemBody(Item)
End Sub

Public Function ItemBody(Item As Variant) As String
On Error Resume Next
    If TypeName(Item) = "ReportItem" Then
        With Item.GetInspector
            ItemBody = .WordEditor.Content
            .Close 1
        End With
    Else
        ItemBody = Item.Body
    End If
End Function
Munch answered 2/9, 2020 at 21:49 Comment(0)
T
0

Yes, there is a problem with ReportItem.Body property in the Outlook Object Model (present in Outlook 2013 and 2016) - you can see it in OutlookSpy (I am its author): select an NDR message, click Item button, select the Body property - it will be garbled. Worse than that, once the report item is touched with OOM, Outlook will display the same junk in the preview pane.

The report text is stored in various MAPI recipient properties (click IMessage button in OutlookSpy and go to the GetRecipientTable tab). The problem is the ReportItem object does not expose the Recipients collection. The workaround is to either use Extended MAPI (C++ or Delphi) or Redemption (any language - I am also its author) - its ReportItem.ReportText property does not have this problem:

set oItem = Application.ActiveExplorer.Selection(1)
set oSession = CreateObject("Redemption.RDOSession")
oSession.MAPIOBJECT = Application.Session.MAPIOBJECT
set rItem = oSession.GetRDOObjectFromOutlookObject(oItem)
MsgBox rItem.ReportText
Third answered 19/4, 2022 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.