user2423476
user2423476

Reputation: 2275

Using Word VBA outlook email body is blank

I made the following vba script in outlook and it works fine when I get the body of the email. I moved the script to word vba and now when I get the email body its empty. I can access the subject and other fields fine but the email body field is blank. How can I access the body of the email?

Dim appOutlook As Object
Dim olNs As Object
Dim olFolder As Object
Dim olItem As Object
Dim iRow As Integer
Dim email_body As String

' Get/create Outlook Application
On Error Resume Next
Set appOutlook = GetObject(, "Outlook.Application")
If appOutlook Is Nothing Then
Set appOutlook = CreateObject("Outlook.Application")
End If
On Error GoTo 0

Set olNs = appOutlook.GetNamespace("MAPI")
Set olFolder = olNs.GetDefaultFolder(6) _
                .Parent.Folders("folder2")  ' 6 == Inbox for some reason


For iRow = 1 To olFolder.Items.Count
Next iRow

For Each myItem In olFolder.Items
myItem.Display
 Dim Email As Outlook.MailItem
Set Email = appOutlook.ActiveInspector.CurrentItem
myItem.Close olDiscard
'Word document
Dim wdApp As Word.Application
Set wdApp = CreateObject("Word.Application")

Dim wdDoc As Word.Document
Set wdDoc = wdApp.Documents.Add
    wdDoc.Activate

Dim wdRange As Word.Range
Set wdRange = wdDoc.Range(0, 0)
email_body = Email.Body

Upvotes: 0

Views: 400

Answers (1)

Eugene Astafiev
Eugene Astafiev

Reputation: 49453

In the following code you iterate over all items in the folder and display each item in a new inspector window in Outlook:

For Each myItem In olFolder.Items

MsgBox myItem.Body

There is no need to call the Display method to get the actual mail item. Instead, you can use the existing reference.

Upvotes: 0

Related Questions