PDA

View Full Version : Outlook Automation Error



Espo
02-11-2009, 06:18 AM
Hi all,

I'm trying to write a macro in Outlook that will take items that I have selected, forward them to a hard coded recipient and then delete them. It is a very simple script and it works for MailItem Objects but when it runs on MeetingItem Objects I get an automation error. I tried to run some code I found that was supposed to give me a clue as to what the error is but the code returned something like "No error description found". The one thing I did try was to display the forwarded message and then break before the Msg.Send line and send it manually. When I do this I get a message saying something like "The property you are trying to set is invalid for this item." Here's the code (edited out recipients address).

Sub ForwardSelection()
Set Sel = ActiveExplorer.Selection
For i = 1 To Sel.Count
Set Msg = Sel.Item(i).Forward
Msg.Recipients.Add ("<Email Recipient>")
Msg.Send
Sel.Item(i).Delete
Next
End Sub

Another note. When I try to add a watch for debugging on certain items and then look at the values it causes Outlook to crash. I tried a Detect and Repair on Outlook but this didn't help.

Thanks in advance for any help.

JP2112
02-11-2009, 12:25 PM
You need to check the type of Sel.Item(i) before proceeding. You also need to step backwards when deleting. For example:
Sub ForwardSelection()
Dim obj As Object
Dim Msg As Outlook.MailItem
Dim lCount As Long

' make sure something is selected
Set Sel = ActiveExplorer.Selection
lCount = Sel.Count
If lCount = 0 Then Goto ExitProc

For i = lCount To 1 Step -1
' check if we have a mailitem, if so then forward it and delete orig
Set obj = Sel.Item(i)
If TypeOf obj Is Outlook.MailItem Then
Set Msg = obj.Forward
With Msg
.Recipients.Add ("<Email Recipient>")
.Send
End With
obj.Delete
End If
Next i

ExitProc:
Set obj = Nothing
Set Msg = Nothing
Set Sel = Nothing
End Sub



Hi all,

I'm trying to write a macro in Outlook that will take items that I have selected, forward them to a hard coded recipient and then delete them. It is a very simple script and it works for MailItem Objects but when it runs on MeetingItem Objects I get an automation error. I tried to run some code I found that was supposed to give me a clue as to what the error is but the code returned something like "No error description found". The one thing I did try was to display the forwarded message and then break before the Msg.Send line and send it manually. When I do this I get a message saying something like "The property you are trying to set is invalid for this item." Here's the code (edited out recipients address).

Sub ForwardSelection()
Set Sel = ActiveExplorer.Selection
For i = 1 To Sel.Count
Set Msg = Sel.Item(i).Forward
Msg.Recipients.Add ("<Email Recipient>")
Msg.Send
Sel.Item(i).Delete
Next
End Sub

Another note. When I try to add a watch for debugging on certain items and then look at the values it causes Outlook to crash. I tried a Detect and Repair on Outlook but this didn't help.

Thanks in advance for any help.

Espo
02-11-2009, 12:46 PM
Thanks for the reply.

I am checking basic functionality before I implement error checking so I understand those additions.

The problem I'm having isn't with the MailItem Object. This works just fine. But if I've selected a MeetingItem Object it doesn't work. I've set up a test wherein I just handle MeetingItem Objects and it does not work.

eg.

Dim Mtg As Outlook.MeetingItem
Dim Sel As Outlook.Selection

Set Sel = ActiveExplorer.Selection
For i = 1 to Sel.Count
Set Mtg = Sel.Item(i).Forward
Mtg.Recipients.Add ("<Email Recipient>")
Mtg.Send 'This line generates an automation error'
Sel.Item(i).Delete
Next

If I change this code to work on a MailItem it works. Working with a MeetingItem it does not work. The code posted above, from everything I've found in the language reference, should work for both.

Thanks again.

JP2112
02-11-2009, 02:20 PM
I don't see a reason why your code would fail.

In your original code, you used "Msg" as a variable to refer to the current item. How did you declare it? If you declared it as Outlook.MailItem, that would explain the behavior you are experiencing. The code will work on messages but error out on anything else.

Otherwise the code is exactly the same for forwarding MeetingItems and MailItems.

It's also possible that the string literal you are passing to the Recipients Collection (using the Add Method) isn't resolving.

Try calling the ResolveAll Method of the Recipients Object.

Msg.Recipients.ResolveAll

That would trigger an error if the recipient could not be resolved for some reason. You should make sure you are passing either a valid email address or resolvable name to the Recipients collection.

Also, are you forwarding MeetingItems to potential meeting attendees? If so, it's better to simply ask the meeting organizer to send them an invite, instead of the participants forwarding it around. That way the meeting organizer knows who is attending.



Thanks for the reply.

I am checking basic functionality before I implement error checking so I understand those additions.

The problem I'm having isn't with the MailItem Object. This works just fine. But if I've selected a MeetingItem Object it doesn't work. I've set up a test wherein I just handle MeetingItem Objects and it does not work.

eg.

Dim Mtg As Outlook.MeetingItem
Dim Sel As Outlook.Selection

Set Sel = ActiveExplorer.Selection
For i = 1 to Sel.Count
Set Mtg = Sel.Item(i).Forward
Mtg.Recipients.Add ("<Email Recipient>")
Mtg.Send 'This line generates an automation error'
Sel.Item(i).Delete
Next

If I change this code to work on a MailItem it works. Working with a MeetingItem it does not work. The code posted above, from everything I've found in the language reference, should work for both.

Thanks again.

Espo
02-11-2009, 03:00 PM
Thanks for the reply. I tried to check to see if the recipient is resolved and it is. This is a static recipient. The script I'm setting up is to save me some time. It turns out that in my company there is someone with the exact same name as me and so I'm getting a lot of his emails and meeting requests. So I want to be able to select the items that are for him and forward them in one shot. So it's not like it's getting the email address programmatically.

Also, I set up a more complicated version of this code where I declare a MailItem Object called Msg and a MeetingItem Object called Mtg. I would then do

TypeName(Sel.Item(i))

to determine which object should be used for the forward. Again in this instance it is handling the mail items fine but barfing on the meeting items.

I think this has a lot to do with the properties of the meeting item getting hosed and becoming invalid. I wrote a routine that used the ItemProperties property of the MeetingItem object to check all of the properties. It turns out that I get 'Type mismatch' errors on certain elements of the resulting collection even though if I run a TypeName on that element it shows 'ItemProperty' just like the elements that work... weird.

JP2112
02-11-2009, 03:33 PM
Can you post the more complicated version?


Thanks for the reply. I tried to check to see if the recipient is resolved and it is. This is a static recipient. The script I'm setting up is to save me some time. It turns out that in my company there is someone with the exact same name as me and so I'm getting a lot of his emails and meeting requests. So I want to be able to select the items that are for him and forward them in one shot. So it's not like it's getting the email address programmatically.

Also, I set up a more complicated version of this code where I declare a MailItem Object called Msg and a MeetingItem Object called Mtg. I would then do

TypeName(Sel.Item(i))

to determine which object should be used for the forward. Again in this instance it is handling the mail items fine but barfing on the meeting items.

I think this has a lot to do with the properties of the meeting item getting hosed and becoming invalid. I wrote a routine that used the ItemProperties property of the MeetingItem object to check all of the properties. It turns out that I get 'Type mismatch' errors on certain elements of the resulting collection even though if I run a TypeName on that element it shows 'ItemProperty' just like the elements that work... weird.

Espo
02-11-2009, 06:39 PM
OK by more complicated I just mean more lines. Here it is...


Sub ForwardSelected ()
Dim Sel As Outlook.Selection
Dim Msg As Outlook.MailItem
Dim Mtg As Outlook.MeetingItem

Set Sel = ActiveExplorer.Selection
If Sel.Count < 1 Then Goto Cleanup

For i = 1 to Sel.Count
Select Case TypeName(Sel.Item(i))
Case = "MailItem"
Set Msg = Sel.Item(i).Forward
Msg.Recipients.Add ("<Email Recipient>")
Msg.Send
Case = "MeetingItem"
Set Mtg = Sel.Item(i).Forward
Mtg.Recipients.Add ("<Email Recipient>")
Mtg.Send
Case Else
MsgBox("Invalid Item Type Selected")
End Select
Sel.Item(i).Delete
Next

Cleanup:
Set Sel = Nothing
Set Msg = Nothing
Set Mtg = Nothing

End Sub


Note that I'm working on this project from work and am replying to this post from my Macbook at home so I don't have an immediate ability to verify this code... I'm just including it from memory. It looks right.... or close enough.

Espo
02-11-2009, 06:39 PM
OK by more complicated I just mean more lines. Here it is...


Sub ForwardSelected ()
Dim Sel As Outlook.Selection
Dim Msg As Outlook.MailItem
Dim Mtg As Outlook.MeetingItem

Set Sel = ActiveExplorer.Selection
If Sel.Count < 1 Then Goto Cleanup

For i = 1 to Sel.Count
Select Case TypeName(Sel.Item(i))
Case = "MailItem"
Set Msg = Sel.Item(i).Forward
Msg.Recipients.Add ("<Email Recipient>")
Msg.Send
Case = "MeetingItem"
Set Mtg = Sel.Item(i).Forward
Mtg.Recipients.Add ("<Email Recipient>")
Mtg.Send
Case Else
MsgBox("Invalid Item Type Selected")
End Select
Sel.Item(i).Delete
Next

Cleanup:
Set Sel = Nothing
Set Msg = Nothing
Set Mtg = Nothing

End Sub


Note that I'm working on this project from work and am replying to this post from my Macbook at home so I don't have an immediate ability to verify this code... I'm just including it from memory. It looks right.... or close enough.

Sorry about the double post.... must have clicked twice by accident.

JP2112
02-11-2009, 07:53 PM
I understand, but unless I can see the actual code you're using (minus anything proprietary, of course), I can't tell why it's not working.

Did you try changing the code to step backwards? When you remove an item from a folder, the index changes, so you need to step backwards as I showed in my code sample.

Also, are you stepping through the code to see which item is currently being processed when the code fails? I wasn't clear on how you are determining the current item. You mentioned adding a watch, I prefer the easier method of just stepping through the code by pressing F8 repeatedly, then hovering over each variable (or typing "?<variable>" in the Immediate Window) to check it's value.



OK by more complicated I just mean more lines. Here it is...


Sub ForwardSelected ()
Dim Sel As Outlook.Selection
Dim Msg As Outlook.MailItem
Dim Mtg As Outlook.MeetingItem

Set Sel = ActiveExplorer.Selection
If Sel.Count < 1 Then Goto Cleanup

For i = 1 to Sel.Count
Select Case TypeName(Sel.Item(i))
Case = "MailItem"
Set Msg = Sel.Item(i).Forward
Msg.Recipients.Add ("<Email Recipient>")
Msg.Send
Case = "MeetingItem"
Set Mtg = Sel.Item(i).Forward
Mtg.Recipients.Add ("<Email Recipient>")
Mtg.Send
Case Else
MsgBox("Invalid Item Type Selected")
End Select
Sel.Item(i).Delete
Next

Cleanup:
Set Sel = Nothing
Set Msg = Nothing
Set Mtg = Nothing

End Sub

Note that I'm working on this project from work and am replying to this post from my Macbook at home so I don't have an immediate ability to verify this code... I'm just including it from memory. It looks right.... or close enough.

Sorry about the double post.... must have clicked twice by accident.