VBA emails not recognized as HTML?

N

NakedJ

I am new to VBA, and have been trying to piece together code that will
export emails that are HTML format to a location on my hard drive.
The code that I have (below) saves some of the HTML files, but not all
of them. I can't figure out what is different about the e-mails that
some are recognized as HTML and some are not.

I have tried to use "If smsg.bodyformat = olFormatHTML Then" before
the saveas, but this does not work either.

Any suggestions?

Here is the Code:
Sub ExportOutlookMessagesToDisk()
Dim ol As New Outlook.Application
Dim olns As Outlook.NameSpace
Dim cf As Outlook.MAPIFolder
Dim Prop As Outlook.UserProperty
Dim sMsg As Outlook.MailItem
Dim oAtt As Outlook.Attachment
Dim iMsgCnt As Integer
Dim iAttachCnt As Integer
Dim iRnd As Long
Set olns = ol.GetNamespace("MAPI")
Set cf = ActiveExplorer.CurrentFolder ' current folder)
On Error Resume Next

Randomize
iMsgCnt = 0

For Each sMsg In cf.Items
sMsg.SaveAs "C:\Documents and Settings\jaflint\My Documents
\email\" & StripChars(sMsg.Subject) & ".HTML", olHTML

'sMsg.Subject
'With sMsg.Attachments
' iAttachCnt = sMsg.Attachments.Count
' If iAttachCnt > 0 Then
' For Each oAtt In sMsg.Attachments
' oAtt.SaveAsFile "C:\hold\Attachments\" &
Mid(CStr(Rnd), 3) & "_" & oAtt.FileName
' Next
' End If
iMsgCnt = iMsgCnt + 1
Next
End Sub

Public Function StripChars(ByVal sStr) As String
sStr = Replace(sStr, """", "")
sStr = Replace(sStr, "'", "")
sStr = Replace(sStr, ":", "")
StripChars = sStr
End Function
Thanks,
Jason
 
S

Sue Mosher [MVP-Outlook]

I have tried to use "If smsg.bodyformat = olFormatHTML Then" before
the saveas, but this does not work either.

Doesn't work in what way? Only HTML format messages can be saved as .htm files. You might also be overwriting messages with identical subjects.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
http://www.outlookcode.com/article.aspx?id=54
 
N

NakedJ

Doesn't work in what way? Only HTML format messages can be saved as .htm files. You might also be overwriting messages with identical subjects.

Nope, even with the getinspector method it doesn't work. If I open
the e-mails and look at their properties, they are in HTML format, but
only some of them save. All have different subjects.

I have seen in MSDN documentation that if Word is the default editor,
then bodyformat and getinspector methods do not work. I will test to
see if this is the problem. If so, is there another method?

Thanks again, your book is very helpful, I feel I have come a long
way...

Jason
 
N

NakedJ

I have seen in MSDN documentation that if Word is the default editor,
then bodyformat and getinspector methods do not work. I will test to
see if this is the problem. If so, is there another method?


As I expected, this is the problem. If the e-mail is edited using
Word as the editor, the email is not recognized as html. When I
switched to Outlook as my editor, it worked perfectly.

Anyone know a workaround to save or convert email edited using word to
a format that will be recognized as HTML in VBA? This seems like a
wierd bug in Outlook 2000. Is this true in outlook 2003 or 2007 as
well? (We are likely upgrading soon).
 
S

Sue Mosher [MVP-Outlook]

I don't know how it could work perfectly, since Outlook 2000 doesn't support the BodyFormat property.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
http://www.outlookcode.com/article.aspx?id=54
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top