Reading from Mailbox

N

Nedan Nedzatra

Hia!

How are you frieds?

Could someone give me an example of codes that read a received message
(Inbox) into a Word Document?

Thanks.
 
G

Graham Mayor

The following will read the message body content (as text) of the first
listed message in the Inbox folder into a new Word document. You will need
to set a reference to the Microsoft Outlook Object Library in vba tools >
references.

Sub ExtractOLMessage()
Dim i As Long
Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim olFolder As Outlook.Folder
Dim olItem As Outlook.MailItem
Dim TempDoc As Document

On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err = 429 Then
Set olApp = CreateObject("Outlook.Application")
End If

Set olNs = olApp.GetNamespace("MAPI")

Set olFolder = olNs.GetDefaultFolder(olFolderInbox)
i = olFolder.Items.Count
Set olItem = olFolder.Items(i)
Set TempDoc = Documents.Add
TempDoc.Content.InsertAfter olItem.Body
olItem.UnRead = False
Set olItem = Nothing
Set olItem = Nothing
Set olFolder = Nothing
Set olNs = Nothing
Set olApp = Nothing
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
N

Nedan Nedzatra

Great.

Thank You.

Graham Mayor said:
The following will read the message body content (as text) of the first
listed message in the Inbox folder into a new Word document. You will need
to set a reference to the Microsoft Outlook Object Library in vba tools >
references.

Sub ExtractOLMessage()
Dim i As Long
Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim olFolder As Outlook.Folder
Dim olItem As Outlook.MailItem
Dim TempDoc As Document

On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err = 429 Then
Set olApp = CreateObject("Outlook.Application")
End If

Set olNs = olApp.GetNamespace("MAPI")

Set olFolder = olNs.GetDefaultFolder(olFolderInbox)
i = olFolder.Items.Count
Set olItem = olFolder.Items(i)
Set TempDoc = Documents.Add
TempDoc.Content.InsertAfter olItem.Body
olItem.UnRead = False
Set olItem = Nothing
Set olItem = Nothing
Set olFolder = Nothing
Set olNs = Nothing
Set olApp = Nothing
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>





.
 
N

Nedan Nedzatra

Great.

Thanks.

Graham Mayor said:
The following will read the message body content (as text) of the first
listed message in the Inbox folder into a new Word document. You will need
to set a reference to the Microsoft Outlook Object Library in vba tools >
references.

Sub ExtractOLMessage()
Dim i As Long
Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim olFolder As Outlook.Folder
Dim olItem As Outlook.MailItem
Dim TempDoc As Document

On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err = 429 Then
Set olApp = CreateObject("Outlook.Application")
End If

Set olNs = olApp.GetNamespace("MAPI")

Set olFolder = olNs.GetDefaultFolder(olFolderInbox)
i = olFolder.Items.Count
Set olItem = olFolder.Items(i)
Set TempDoc = Documents.Add
TempDoc.Content.InsertAfter olItem.Body
olItem.UnRead = False
Set olItem = Nothing
Set olItem = Nothing
Set olFolder = Nothing
Set olNs = Nothing
Set olApp = Nothing
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>





.
 
G

Graham Mayor

You are welcome :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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

Top