Consulting

Results 1 to 5 of 5

Thread: Error running code on share mail box

  1. #1
    VBAX Newbie
    Joined
    Apr 2009
    Posts
    5
    Location

    Error running code on share mail box

    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. Refthread 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

    [vba]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[/vba]
    Last edited by Agni1978; 04-23-2009 at 06:35 AM. Reason: forgotten to add one more detail

  2. #2
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    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:

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

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

    Quote Originally Posted by Agni1978
    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. Refthread 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

  3. #3
    VBAX Newbie
    Joined
    Apr 2009
    Posts
    5
    Location

    Problem not solved

    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

  4. #4
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    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. [VBA]If objSelection(i).Class = olMail Then[/VBA]

    Charlize

  5. #5
    VBAX Newbie
    Joined
    Apr 2009
    Posts
    5
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •