That worked great. Thanks!
The only thing I'm finding is that the code (which sets an Outlook object;
creates a new mail message; set the To and Subject fields; and then pastes
into the body) works most of the time. But every once in a while (maybe one
out of three or four times) I get an automation error at the paste command.
If I break the code and re-execute the paste command, it then works.
So I'm thinking perhaps it's a time thing, where occasionally Outlook is
delayed in creating the mail message, and so the paste command fails because
the message isn't created yet? (Outlook is usually open when it's run; so
it's not a question of Outlook being opened.)
I tried putting a DoEvents statement in between the creation of the mail
message and the execution of the paste command. But it had no effect.
Any idea why I might be getting these intermittent failures? Code I'm
executing is below. Thanks!
Neil
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Set objOutlook = GetOutlookObject(False)
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
.To = varTo
.Subject = varSubject
.Display
.GetInspector.CommandBars.ExecuteMso ("Paste")
End With
Public Function GetOutlookObject(NewInstance As Boolean) As Object
Dim objOutlook As Object
If NewInstance Then
Set objOutlook = CreateObject("Outlook.Application", "LocalHost")
Else
Set objOutlook = GetAppObject("Outlook.Application")
End If
Set GetOutlookObject = objOutlook
Set objOutlook = Nothing
End Function
Public Function GetAppObject(ClassName As String) As Object
Dim obj As Object
Dim blnError As Boolean
On Error Resume Next
Set obj = GetObject(, ClassName)
blnError = Err.Number > 0
If blnError Then
Set obj = CreateObject(ClassName, "LocalHost")
End If
Set GetAppObject = obj
Set obj = Nothing
End Function
"Ken Slovak" <(E-Mail Removed)> wrote in message
news:i2c55g$m1g$(E-Mail Removed)...
> objOutlookMsg.GetInspector.CommandBars.ExecuteMso() where the supplied
> idMso is that of the ribbon control you want to execute, in this case
> "Paste".
>
> --
> Ken Slovak
> [MVP - Outlook]
> http://www.slovaktech.com
> Author: Professional Programming Outlook 2007.
> Reminder Manager, Extended Reminders, Attachment Options.
> http://www.slovaktech.com/products.htm
>
>
> "Neil" <neil.ginsberg+(E-Mail Removed)> wrote in message
> news:i2b8vk$j6c$(E-Mail Removed)...
>>I have some code which automates an Edit | Paste in Outlook 2003, as
>>follows:
>>
>> objOutlookMsg.GetInspector.CommandBars.Item("Menu
>> Bar").Controls("Edit").Controls("Paste").Execute
>>
>> where objOutlookMsg is an object variable set to an e-mail message.
>>
>> This code works fine in 03. However, it results in an "Automation Error"
>> in Outlook 2007.
>>
>> How do I automate an Edit | Paste in Outlook 2007?
>>
>> Thanks!
>>
>> Neil
>>
>