Extracting email body to Word (Outlook)

Mike375

Registered User.
Local time
Today, 14:41
Joined
Aug 28, 2008
Messages
2,548
I have manage to find the following code which works fine except the bold red section will not allow it work on a selected email. (1) will be the last email (2) the second last etc. I have very little experience with macros in Outlook.

Code:
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim olItems As Outlook.Items
Dim olItem As Outlook.MailItem
Dim WordWasNotRunning As Boolean
Set olItems = Application.Session.GetDefaultFolder(olFolderInbox).Items
olItems.Sort "[Received]", True
[B][COLOR=red]Set olItem = olItems(1)[/COLOR][/B]
WordWasNotRunning = False
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err Then
     Set wdApp = CreateObject("Word.Application")
     WordWasNotRunning = True
End If
Set wdDoc = wdApp.Documents.Add
wdDoc.Content.InsertAfter olItem.Body
 
olItem.UnRead = False
Set olItem = Nothing
Set olItems = Nothing
wdApp.WindowState = wdWindowStateMinimize
wdApp.Visible = True
wdApp.WindowState = wdWindowStateNormal
 

 

If WordWasNotRunning = True Then
     wdApp.Quit
End If
 
Set wdDoc = Nothing
Set wdApp = Nothing
 
Hi Mike
My knowledge of the Outlook object model is also limited. But the line you highlighted does what I expect when I run it i.e. given that the previous line sorts the mail by date, the line in red sets olitem to the most recent mail (true=descending, false=ascending).

Are you saying that that is not happening? Are you getting an error?

Chris
 
Mike
Another question, what do you mean by a "selected" email? olItems is just a collection of all the emails in the specified folder. There is not "selected" email as such.

If you want a specific email you can filter the collection like this:

Set olItems = Application.Session..GetDefaultFolder(olFolderInbox).Items.Restrict("[Subject] = 'my email subject'")

Obviously you can use any criteria you like to select a specific email e.g. sender, date etc

Also take a look here for more on using the outlook object model, particularly "concepts", "how do I" and "Outlook Object Model Reference"

hth
Chris
 
Chris,

Thanks for that I will give it a go.

With attachments I am OK and click on (highlight) the email and this is the one which is done. The code I posted is as you say, working but I could not get things to work for a specific highlighted email.

When I wore out Google:D quite a few of the solutions were starting in Word or Excel and then opening Outlook but that seemed messy. Actually most of the problems/solutions were aimed at extracting specific text from the email body and in particular the email address from bounced back emails so were starting with < and similar.
 
Chris,

The following got it, that is the body text from the highlighted email. If you highlight several then it does all of them, although as it is you have that number of Word docs open. So needs to have some close and save etc. and clean up.

Code:
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim myOlExp As Explorer
Dim myOlSel As Selection
Dim myOlMail As MailItem
Dim i As Integer, j As Long
Set myOlExp = ActiveExplorer
Set myOlSel = myOlExp.Selection
For j = 1 To myOlSel.Count
Set myOlMail = myOlSel.Item(j)
Dim WordWasNotRunning As Boolean
 
WordWasNotRunning = False
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err Then
     Set wdApp = CreateObject("Word.Application")
     WordWasNotRunning = True
End If
Set wdDoc = wdApp.Documents.Add
wdDoc.Content = myOlMail.Body
 
 
wdApp.WindowState = wdWindowStateMinimize
wdApp.Visible = True
wdApp.WindowState = wdWindowStateNormal
 

 

If WordWasNotRunning = True Then
     wdApp.Quit
End If
 
Set wdDoc = Nothing
Set wdApp = Nothing
 

Set myOlMail = Nothing
Next j
Set myOlSel = Nothing
Set myOlExp = Nothing
 
Chris,

This has got it. Note I have removed the visual opening of Word. All I need to do is work out how to reference the subject or email address so in combination with data/time the "wordsave" is dynamic and ditto for attachment. That site you linked me to took me in the required directions, thanks for that.

As a side note I have found the email package with Access/Word is a very good little seller but up to this point I only has the other side all completed, that is, Access producing the Word doc with bookmarks filled etc and then emailed. What is popular is having the body of the email the Word doc with the bookmarks filled.

Code:
Sub ExpExtract2()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim myOlExp As Explorer
Dim myOlSel As Selection
Dim myOlMail As MailItem
Dim i As Integer, j As Long
Set myOlExp = ActiveExplorer
Set myOlSel = myOlExp.Selection
For j = 1 To myOlSel.Count
Set myOlMail = myOlSel.Item(j)
Dim WordWasNotRunning As Boolean
WordWasNotRunning = False
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err Then
     Set wdApp = CreateObject("Word.Application")
     WordWasNotRunning = True
End If
Set wdDoc = wdApp.Documents.Add
wdDoc.Content = myOlMail.Body
 
[B]wdDoc.SaveAs ("c:\StoreLettersDemo\FromOutLook.doc") Needs to be dynamic with either subject or email address plus date/time[/B]
wdApp.Quit
Set myOlMail = Nothing
Next j
Set myOlSel = Nothing
Set myOlExp = Nothing
End Sub
 
Doing the SaveAs

wdDoc.SaveAs ("c:\StoreLettersDemo\" + Format(Now, "YYYY-MM-DD") + " " & Format(Now, "hh-mm-ss") + ".doc")

allowed for highlighting a bunch of emails and it did the lot.
 
This got the subject (but was OK if subject was blank)

wdDoc.SaveAs ("c:\StoreLettersDemo\" + Format(Now, "YYYY-MM-DD") + " " & Format(Now, "hh-mm-ss") + " " & (myOlMail.Subject) + ".doc")
 
wdDoc.SaveAs ("c:\StoreLettersDemo\" + Format(Now, "YYYY-MM-DD") + " " & Format(Now, "hh-mm-ss") + " " & (myOlMail.SenderName) + " " & (myOlMail.Subject) + ".doc")

Saved as

2009-07-18 23-51-18 Bill Bloggs Email Testing

But I can't get the email address???

wdDoc.SaveAs ("c:\StoreLettersDemo\" + Format(Now, "YYYY-MM-DD") + " " & Format(Now, "hh-mm-ss") + " " & (myOlMail.SenderName) + " " & "Subject was" + " " & (myOlMail.Subject) + ".doc")

2009-07-18 23-51-18 Bill Bloggs Subject was Email Testing

Everything but email address:D
 
wdDoc.SaveAs ("c:\StoreLettersDemo\" + Format(Now, "YYYY-MM-DD") + " " & Format(Now, "hh-mm-ss") + " " & (myOlMail.SenderEmailAddress) + " " & (myOlMail.SenderName) + " " & "Subject was" + " " & (myOlMail.Subject) + ".doc")

That got the lot.

I had already tried myOlMail.SenderEmailAddress but had a typo and when there is a problem it brings up the standard Word Save As dialog box.
 
The dynamic save fell apart on a Forwarded email, brought up the standard Word Save dialog box.

But was OK with

wdDoc.SaveAs ("c:\StoreLettersDemo\TestForward.doc")

Looks like more work tomorrow night:)
 
The dynamic save works OK on forwarded email if the myOlMail.Subject is left out.

The FW: is causing the problem as if FW: Test is done for subject on ordinary email same problem as forwarding.
 
I spend 20% of my time learning and writing my code and 80% making it "idiot" safe.

I dealt with a "do something with a selected email" recently and I have added below some checks you may want to do.

I note for example you want to handle multiple emails but my code below is set up for only one. I have left these things in just in case you should change later.

Code:
'get the active explorer
Set out_Exp = out_App.ActiveExplorer

'here they have clicked on nothing but are in "Mailbox"
If out_Exp.CurrentFolder.WebViewOn = False Then
        Set out_Sel = out_Exp.Selection
    Else
        MsgBox "No email selected"
        Set out_Exp = Nothing
        Exit Sub
    End If

'first make sure they have only choosen one item if not exit sub
If out_Sel.Count > 1 Then
    MsgBox "more than one email selected"
    Exit Sub
Else
    If out_Sel.Count = 0 Then
    MsgBox "No email selected"
    Exit Sub
    End If
End If

'check that it is in fact an email that has been choosen
If Not out_Sel.Item(1).Class = olMail Then
    MsgBox "Not an email"
    Exit Sub
End If


'next try to get the email item - if there is an error 13 then it is encrypted
On Error Resume Next

Set out_mail = out_Sel.Item(1)

If Err.Number = 13 Then
    Err.Clear
    MsgBox "email is encrypted"
    Exit Sub
End If
 
I got the Forwarded email fixed.

I put this before the Word saveas line

myOlMail.Subject = LTrim(Right(myOlMail.Subject, Len(myOlMail.Subject) - InStr(myOlMail.Subject, ":")))

No need for If for ordinary email as no : for InStr and hence it is 0

I can go to bed now:D
 
darbid

Thanks for that. I will look tomorrow as now 2.05am down here in Australia:eek:
 
Yeh I know I am listing to the radio there.

I am a homesick Aussie in Germany :-(
 
Yeh I know I am listing to the radio there.

I am a homesick Aussie in Germany :-(

What are you doing in Germany?

This

wdDoc.SaveAs ("c:\StoreLettersDemo\" + Format(myOlMail.CreationTime, "YYYY-MM-DD" + " " & "hh-mm-ss") + " " & (myOlMail.SenderEmailAddress) + " " & (myOlMail.SenderName) + " " & "Subject was" + " " & (myOlMail.Subject) + ".doc")

puts the email received time in the file name as opposed to the current date/time.
 

Users who are viewing this thread

Back
Top Bottom