Outlook 2007 - Error Inserting Text into Received E-mail

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a VBA macro for handling e-mail attachments in Outlook 2007 (based on
the version from Nicola Delfino). The macro saves any attachments in an
e-mail highlighted in the Inbox, then deletes them, and finally adds a line
of text into the original e-mail with the path where the file saves (see code
below). The macro handles the first two tasks fine, but then I receive the
following error when I try to add the text to the message:

Error Number: 4605
Error Description: This method or property is not available because the
document is locked for editing.

This happens when the text is being inserted into the e-mail message. When
I'm debugging the code, I've noticed that many of the WordEditor properties
are locked and unavailable. How do I unlock the underlying Word.Document
properties and add the text?

Thanks!
- Chris

CODE:
Public Sub StripAttachments()

Dim objOL As Outlook.Application
Dim objMsg As Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolder As String
Dim result

On Error GoTo StripAttachments_err

result = MsgBox("Do you want to remove attachments from selected
email(s)?", vbYesNo + vbQuestion)
If result = vbNo Then
Exit Sub
End If

' Instantiate an Outlook Application object.
' Set objOL = CreateObject("Outlook.Application")
Set objOL = Application
' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection

' Set the folder to save attachements
' Need a better way of doing this - maybe dialog box
strFolder = "D:\Data\Outlook_Attachments\"

' Check each selected item for attachments.
' If attachments exist, save them to the Temp
' folder and strip them from the item.
For Each objMsg In objSelection
' This code only strips attachments from mail items.
If objMsg.Class = olMail Then
' Get the Attachments collection of the item.
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
If lngCount > 0 Then
' We need to use a count down loop for
' removing items from a collection. Otherwise,
' the loop counter gets confused and only every
' other item is removed.
strFile = ""
For i = lngCount To 1 Step -1
' Get the file name.
strFile = strFile & strFolder &
objAttachments.Item(i).FileName

' Save the attachment as a file.
objAttachments.Item(i).SaveAsFile strFile

' Delete the attachment.
objAttachments.Item(i).Delete
Next i

Dim objDoc As Object
Dim objInsp As Outlook.Inspector
Set objInsp = objMsg.GetInspector
Set objDoc = objInsp.WordEditor

objDoc.Characters(1).InsertBefore strFile
objDoc.Save

End If
End If
Next

ExitSub:
Set objDoc = Nothing
Set objInsp = Nothing
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing

Exit Sub

StripAttachments_err:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Macro Name: StripAttachments" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
& vbCrLf & "Error Source: " & Err.Source _
, vbCritical, "Error!"

GoTo ExitSub
End Sub
 
There are issues with things not working in WordMail 2007 and even more
things don't work if Word and Outlook are from different SKU's or from
certain versions of Office. See the discussion of WordMail limitations on
the page of issues for developers at www.outlookcode.com.

You might have to find a different way of doing what you want or it might
not be doable.
 
Thanks for the note. I think this is the page you mentioned, which is very
useful:
http://outlookcode.com/article.aspx?id=59

However, Most of the points about the new WordEditor don't seem to either
apply to me or indicate that I should be able to do what I mentioned in my
first e-mail. I installed Outlook 2007 and Word 2007 at the same time, so I
should have at least the majority of the methods and properties available.

Under the header "Word as the item editor", it says:
"...Inspector.WordEditor will return a Word.Document object for any type of
Outlook item, except NoteItem, making it possible to use Word methods to
insert text and graphics, insert a QuickPart, format text, apply a theme, and
perform other item body tasks that were difficult if not impossible to
perform in the past."

I can achieve similar behavior by opening an e-mail, then selecting "Edit
Message" under the "Actions->Other Actions" toolbar. Shouldn't I be able to
do this via VBA? This seems like basic functionality, regardless if it's a
lightweight or heavyweight version of the Word editor.

Thanks!
- Chris
 
Well, similar code using objDoc.Characters(1).InsertBefore strFile does work
in a new mail item, so it's a matter of WordMail not allowing writing to the
item unless it's set for editing. About the only way I see to do that is to
find the control that allows editing an email item and execute it.

Even with Outlook 2007 you can do that using the old style CommandBar
interface.

Dim oButton As Office.CommandBarButton

Set oButton = oInspector.CommandBars("Menu Bar").FindControl(Id:=5604,
Recursive:=True)
oButton.Execute

After that the email is put into edit mode and the Word type code

objDoc.Characters(1).InsertBefore strFile

will work. I'd see if saving the email item as well as the Document item or
instead of that is necessary though.

An alternative would be to use the Outlook object model and do something
like this:
Dim strBody As String
strBody = objMsg.Body
strBody = strFile & strBody
objMsg.Body = strBody
 
Ken,

Ah, that's what I was missing! I thought there was a method for putting the
Document into Edit mode, but this will work just as well.

I did update the code to use the Outlook object model, although I am losing
the formatting.

Thanks again for the help!

- Chris
 
Back
Top