How to import HTML Outlook mails into MS Access using VBA

html sql-server ms-access

88 观看


93 作者的声誉

I have been searching the internet for hours to find a solution to my question.

I know how to import mails for a long time, but can't keep the format of the HTML-body like I could do when copy the body from OL by hand into the Rich-text memo field on an access form.

The field (olbody) is bound to a SQL server table field olbody (nvarchar(max)) and could store html mail body text when I insert the content of the clipboard.

I even tried to read the .HTMLbody property in the clipboard

clp.SetText .HTMLBody

and then I pasted manually the content of the clipboard with Ctrl+V in the field, but again, it does no show up like the HTML mail. But I can see all HTML commands, which should format the mail.

Without formating the mail in the access field, the content is almost unreadable because no tab, lineskip etc. is in the body.

Any help welcome. Thanks

作者: mak 的来源 发布者: 2017 年 12 月 27 日

回应 (1)


22131 作者的声誉


The proper way to display HTML is using a web browser. This means: use the web browser control!

You can load the HTML in the webbrowser control on load, and when your mail field changes.

Example code:

Private Sub Form_Current()
    Dim wb As Object
    Set wb = MyWebbrowserControl.Object
    With wb
        .Navigate2 "about:blank"
        Do Until .ReadyState = 4 '=READYSTATE_COMPLETE
            'This is a somewhat inefficient way to wait, but loading a blank page should only take a couple of milliseconds
        .Document.Write MemoField.Value
    End With
End Sub

Replace Memofield with your mail field, and MyWebbroserControl with your webbrowser control, and you should be ready to go.

Note that you probably should use the Access web browser, and not the ActiveX web browser. This code will work with both, but the Access web browser control uses a more modern version of IE, and doesn't come with a non-removable border.

作者: Erik A 发布者: 27.12.2017 06:14