Export HTML emails in Outlook using VBA

Source: Internet
Author: User

In the e-mail I receive every day, the subscribed e-magazines account for a large proportion. There are both news, computer technology, and entertaining articles. Together, there are hundreds of articles. Later, I knew that many people in the organization liked reading the same content as me, and some people subscribed to the same magazine, so I organized them into the LAN after receiving emails every day. It's just that there are so many emails and the workload is not small. How can this problem be solved?

These emails are generally in HTML format and cannot be correctly exported using the Outlook method. They are distributed in many lower-layer sub-folders, which is very troublesome to export. In OUTLOOK, I used VBA to export HTML emails and automatically publish them to the network.

To operate emails in the mailbox, you must first obtain the Outlook MAPI namespace. You can use the following statement:

Dim mobjOutlook As Outlook. NameSpace

Dim objOutlook As New Outlook. Application

MobjOutlook = objoutlook. GetNameSpace ("MAPI ")

Use the GetDefaultFolder method of mobjOutlook. You can get the MAPIFolder object of the Inbox:

Dim objFolder As Outlook. MAPIFolder

ObjFolder = mobjOutlook. GetDefaultFolder (6)

Parameter 6 indicates the inbox. The meanings of other parameters are as follows:

Constant

Value

Description

OlFolderDeletedItems

3

Deleted emails

OlFolderOutbox

4

Sender

OlFolderSentMail

5

Sent Email

OlFolderInbox

6

Inbox

OlFolderCalendar

9

Calendar

OlFolderContacts

10

Contact

OlFolderJournal

11

Diary

OlFolderNotes

12

Notepaper

OlFolderTasks

13

Task

OlFolderDrafts

16

Draft

The properties of objFolder include the mail item set (ITEMS) and the Folders of all the lower-level sub-Folders.

For each email, obtain the receiving time of the email. If the email is received on the current day, create and open an HTML file, and use its topic as the file name to put its HTML content, that is, the value of the HTMLBody attribute is written to this file, and then the file is closed and processed.

For the next level of sub-folders, you can use recursive calling to traverse all emails in each layer of the inbox. The index file is also generated when the mail file is generated.

The complete procedure is as follows:

Private mobjOutlook As Outlook. NameSpace

Private fs, fo

Private Sub GetOutlook ()

Dim objOutlook As New Outlook. Application

Set mobjOutlook = objOutlook. GetNamespace ("MAPI ")

End Sub

Sub ListMailFolders (objFolder As Outlook. MAPIFolder)

Dim objItem As Object

Dim f

Dim str1, str2, str3 As String

For Each objItem In objFolder. Items

If (FormatDateTime (objItem. ReceivedTime, vb1_date) = FormatDateTime (Date, vb1_date) Then

Str2 = objItem. Subject

Str1 = "j: wwwrootnews" + str2 + ". htm"

Set f = fs. OpenTextFile (str1, 2, True, TristateFalse)

F. Write objItem. HTMLBody

F. Close

Str3 = "<p> <a href = '" + objItem. Subject + ". htm'>" + objItem. Subject + "</a> </p>"

Fo. Write str3

End If

Next

Dim objf As Outlook. MAPIFolder

For Each objf In objFolder. Folders

ListMailFolders objf

Next

Set objItem = Nothing

End Sub

Sub ListMailItems (longFolder As Long)

Dim objFolder As Outlook. MAPIFolder

Dim f

If mobjOutlook Is Nothing Then

GetOutlook

End IF

Set objFolder = mobjOutlook. GetDefaultFolder (longFolder)

ListMailFolders objFolder

End Sub

Private Sub storemail ()

Set fs = CreateObject ("Scripting. FileSystemObject ")

Set fo = fs. OpenTextFile ("j: wwwrootnewsindex.html", 2, True, TristateFalse)

Fo. write "<HTML> <HEAD> <META content = 'text/html; charset = gb2312 'HTTP-equiv = Content-Type> <TITLE> </HEAD> <BODY>

ListMailItems (6)

Fo. Write "</BODY> </HTML>"

Fo. Close

End Sub

Create a new macro in Outlook2000, use the VB editor to edit it, and copy the above program to the same module, change the Directory Name of the generated file to the WWW Service root folder name on your WEB server. Call storemail in the macro and execute the macro to export all emails received on the current day.

All the links to these HTML files are stored in index.html under the same directory, so that everyone can browse these articles online.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.