PDA

View Full Version : Trouble with GetInspector.WordEditor



EvilPanda
12-01-2010, 06:40 AM
Hello everyone.

I have run into a problem I hope you can help me with.

What I need to do is create a Word document with the body of all currently selected e-mail items, ie automating a copy-paste operation. I also do some reformatting, inserting pagebreaks and such. The code works exactly the way I want it to, except that it doesn't. As far as I can tell, my problem is that MailItem.GetInspector.WordEditor returns immediately, even if the document has not yet been loaded.

Below is a simplified version of my script, it demonstrates the undesired behaviour. If I select short, text-only messages (or with very little content/formatting), this code completes with success on first attempt (almost) universally. If, however, I select the messages I actually want to work with, it is a different story.

These messages contain no images, but typically 5 - 25 tables with text (and fill colors). Typical length per e-mail is 4 - 6 pages (when pasted to Word). Generally, it runs OK for the first few messages, followed by 100 failures for all consecutive items. If I add myItem.GetInspector.Activate, it is apparant that Outlook is loading and/or rendering the message body (a moving progress bar is shown above blank body).

The only "reliable" way I have found, is a generous repetition of "Sleep" and "DoEvents" in an infinite loop (until doc.characters.count > 1). It is considerably slower than manually opening and copy-pasting each message (2+ hours for some 30 emails).

How can I reliably access the message body of a mail item?

Thanks in advance.

(I have removed all sanity checks etc to keep the code as short as possible)


Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliSeconds As Long)

Sub test()
Dim OlApp As New Outlook.Application
Dim myItem As Outlook.mailItem
Dim fail As Long

For Each myItem In OlApp.ActiveExplorer.Selection
fail = 0
If myItem.GetInspector.WordEditor.Characters.Count <= 1 Then
Do
Sleep 50
fail = fail + 1
Loop Until myItem.GetInspector.WordEditor.Characters.Count > 1 Or fail > 99
End If

If fail = 0 Then
MsgBox "Success on first attempt"
ElseIf myItem.GetInspector.WordEditor.Characters.Count > 1 And fail > 0 Then
MsgBox "Success after " & fail & " failures."
Else
MsgBox "Could not get message body after " & fail & " attempts."
End If
Next

Set OlApp = Nothing
Set myItem = Nothing
End Sub


Edit: I am using Outlook 2007

EvilPanda
12-01-2010, 09:49 AM
... I was completely oblivious to the fact that I have to .Close the Inspector! Had to be something simple :-) ta