PDA

View Full Version : Error running code on share mail box



Agni1978
04-23-2009, 06:27 AM
Error running code on share mail box
Hi,

I am using the below code to print the attachments of selected e-mails on any folder in outlook. But when I am running this on a local Inbox folder this code is working pretty much in the expected manner, But unfortunately my requirement is to select the mails in a Shared Mailbox and print the attachments. I am experiencing error
Run-time error '-21477221233 (8004010f)': Method 'Attachments' of object 'MailItem' failed.

The same was happening for me when I am using code in KB522 on shared mailbox with some modification. Ref:(thread KB522 RuntimeError in TargetFolderItems_ItemAdd )

Can you please let me know what to change in code in order to get the below code work for Shared Mail box?

Thanks in advance

Public Sub PrintSelectedMails()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim j As Long
Dim lngCount As Long
Dim Response As Integer
Dim msg As String
Dim strSubject As String
Dim dispname As String

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

' Check selected item for attachments.
For i = objSelection.Count To 1 Step -1
lngCount = objSelection(i).Attachments.Count
If lngCount > 0 Then
strSubject = objSelection(i).Subject
If strSubject = "" Then
msg = "Selected item #" & i
Else
msg = strSubject
End If
' Response = MsgBox(msg & " has attachments." & vbCr _
' & "Do you wish to delete?", vbYesNo)
' If Response = vbNo Then
'objSelection(i).PrintOut
Set objAttachments = objSelection(i).Attachments
For j = 1 To objSelection(i).Attachments.Count
dispname = "P:\" & objAttachments.Item(j).DisplayName
objAttachments.Item(j).SaveAsFile dispname
ShellExecute 0&, "print", dispname, 0, "P:\", 12
'objSelection(i).Attachments(j).Print
Next j
' Else
' objSelection(i).PrintOut
' 'objSelection(i).Delete
' End If
' Else
' objSelection(i).PrintOut
' 'objSelection(i).Delete
End If
Next

ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub

JP2112
04-23-2009, 05:59 PM
If you are running this code in Outlook, you should not be using

CreateObject("Outlook.Application")

Use the native Application Object to derive all your other objects. i.e. Set objOL = Application.

What line is the code stopping on, and what mailitem is being processed? Are you sure that mailitem contains attachments?


FYI you might also want to fix this code:

Set objAttachments = objSelection(i).Attachments
For j = 1 to objSelection(i).Attachments.Count
objAttachments is already a reference to the attachments collection, so you probably meant

Set objAttachments = objSelection(i).Attachments
For j = 1 to objAttachments.Count
--JP


Error running code on share mail box
Hi,

I am using the below code to print the attachments of selected e-mails on any folder in outlook. But when I am running this on a local Inbox folder this code is working pretty much in the expected manner, But unfortunately my requirement is to select the mails in a Shared Mailbox and print the attachments. I am experiencing error
Run-time error '-21477221233 (8004010f)': Method 'Attachments' of object 'MailItem' failed.

The same was happening for me when I am using code in KB522 on shared mailbox with some modification. Ref:(thread KB522 RuntimeError in TargetFolderItems_ItemAdd )

Can you please let me know what to change in code in order to get the below code work for Shared Mail box?

Thanks in advance

Agni1978
04-27-2009, 08:46 PM
Hi JP,

After doing the necessary changes still the error appears on Shared mail box. The code is prettymuch working as expected with the local Inbox or with any local folders.

Also I had noticed that this is happening only when a message is not viewable in reading pane.

Kindly let me know if there is any fix available for this.

Thanks in advance.

Regards,
Upendra

Charlize
04-28-2009, 01:42 AM
The messagefolder can contain other things then messages. Read requests, Calendar invites, ... You need to check if the selected item is a mailitem before you do what you want to do. You need to check the class of the item. If objSelection(i).Class = olMail Then

Charlize

Agni1978
04-28-2009, 03:44 AM
Hi Charlize,
It is entering into this and still showing me error at
objSelection(i).Attachments.Count
and it is happening only for sharedmail box for the items that are not visible in reading pane.

Kindly let me know the solution to fix this.

Regards,
Upendra