Solved

Outlook.Mailitem.Body datatype

Posted on 2011-03-15
22
1,037 Views
Last Modified: 2012-05-11
Hi Experts,
    I ran into a problem where after retrieving the mailitem body text, the result appears what i see isn't what i get. In my case:

Dim objOutlook As Outlook.Application
Dim oMail As Outlook.MailItem
Set oMail = Application.ActiveInspector.CurrentItem
Dim strBody As String
strBody = oMail.Body

after these codes I found strBody string is a whole line of string which looks fine to me. then i started my processing:

if strBody = "Daily" then
  i = 1
End if

For illustrating purpose, my strBody in my Email returns me "Daily" as i debug.print mailitem.body in immediate window. but when i run the if statements, it never went through 'Then'.
It turns out to me that strBody isn't just "Daily" as what i saw in immediate window.

I also did a check on mailitem.bodyformat, it is plaintext. Does anybody have a clue how do i solve this issue?
0
Comment
Question by:miketonny
  • 6
  • 4
  • 3
  • +4
22 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35144292
Perhaps if you call Trim()?
strBody = oMail.Body.Trim()

Open in new window

0
 
LVL 2

Author Comment

by:miketonny
ID: 35144457
sry i forgot to mention it's in Outlook 2010 vba macro,
I tried Trim(strBody) before, it's not working as it should.
I see from immediate window strBody = "Daily",
but it just dont pass over 'Then'
0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 125 total points
ID: 35319660
Certain that there are no new lines and other "white space" involved? You would not see that in Immediate Window, and trim doesn't trim that - it only removes real spaces. Try what
   ? ">" & strBody & "<"
displays.
0
 
LVL 15

Accepted Solution

by:
dbbishop earned 125 total points
ID: 35319661
Try:

If Left(strBody, 5) = "Daily" Then
  i = 1
End If

 
0
 
LVL 30

Expert Comment

by:MlandaT
ID: 35319710
can you post the msg file?
0
 
LVL 2

Author Comment

by:miketonny
ID: 35320079
? ">" & strBody & "<"
>Submission from online OEM form for applying for new emailed pricelists.Logged in users details:Customer: ROS0111Contact: EGEE ENTERPRISESPricelevel: 3IP: 121.98.129.47Email: kate@egee.co.nzRequested email details:Email: kate@egee.co.nzType: XLSXFrequency: Daily
<
that's what strBody originally has.

0
 
LVL 2

Author Comment

by:miketonny
ID: 35320100
Hi dbbishop,  that actually worked, thanks.
do you mind tell me why though?
I use :
If strFrequency= "Daily" then
  intFreq = 1
Else
    intFreq = 5
End if
which is not working, but
If Left(strFrequency, 5) = "Daily" Then
    intFreq = 1
Else
    intFreq = 5
End If

this worked.
strFrequency's value is "Daily" for both cases. I'm curious why same value returns me different results
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 35320245
Apparently there is a character after Daily in the email message

0
 
LVL 15

Expert Comment

by:dbbishop
ID: 35320313
As mlmcc stated, there may be a 'control character' following the actual text. These are typically not displayable characters.

You can run a little test if you're up to it:

strBody = oMail.Body
debug.print len(strBody)
For i = 1 To Len(strBody)
    Debug.Print ASCII(MID$(strBody, i, 1)
Next i

This should give you:
68      ' D
97      ' a
105    ' i
108    ' l
121    ' y
...

Anything after the 121 is extraneous data.
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 68

Expert Comment

by:Qlemo
ID: 35321469
That is what I tried to express in http:#35319660 ...
0
 
LVL 76

Assisted Solution

by:David Lee
David Lee earned 125 total points
ID: 35322332
Hi, miketonny.

Trim only removes spaces.  There may be a control character in the body that you can't see.  How about using InStr to test for the presence of "Daily" in the body?
0
 
LVL 30

Assisted Solution

by:MlandaT
MlandaT earned 125 total points
ID: 35322624
you can remove all whitespace characters from the message body with this regular expression:

	strBody = oMail.Body
	Dim reg As New Regex("\s*")
	strBody = reg.Replace(strBody, "").trim()

Open in new window


That should take out all the funny invisible characters. The
0
 
LVL 30

Expert Comment

by:MlandaT
ID: 35322632
If it is enough to just detect the presence of the word "Daily" in the body, you can even just use the Like operator...

If strFrequency Like "*Daily*" then
  intFreq = 1
Else
    intFreq = 5
End if

Open in new window

0
 
LVL 2

Author Comment

by:miketonny
ID: 35328134
Hi dibishop, you're right, i ran the test and there was a 10 & 13 after 121, so that's the carriage return and linefeed at the end of the word, I now know how to test that.
thx guys for the help, much appreciated
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 35328329
I mentioned that in http:#a35319660 ..., and have been first to do so.
0
 
LVL 2

Author Comment

by:miketonny
ID: 35328700
sry Qlemo i missed that when i tick the boxes, is there any other way to assign points?
0
 
LVL 2

Author Comment

by:miketonny
ID: 35328910
cool all set now, thanks for reminding my mistake, i just saw the reference
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 35328917
Thank you for taking the additional effort to do a fair split.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Outlook Free & Paid Tools
Find out what you should include to make the best professional email signature for your organization.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now