Consulting

Results 1 to 11 of 11

Thread: Runtime error: The attempted operation failed.An object could not be found

  1. #1

    Runtime error: The attempted operation failed.An object could not be found

    Hi guys,

    I have written this outlook macro which works fine on your personal mailbox but it gives an error when I'm trying to run it on the shared mailbox:

        On Error Resume Next
        
        Set myOlApp = Outlook.Application
        Set mynamespace = myOlApp.GetNamespace("mapi")
      
        Dim strRowData As String
        Dim strDelimiter As String
        Dim myDestFolder As Outlook.Folder
        Dim olRecip As Outlook.Recipient
        Dim ShareInbox As Outlook.MAPIFolder
        Dim SubFolder As Object
        Dim InputFolder As String
        Dim OutputFolder As String
        Dim ProdMail As String
        Dim SavePath As String
          
        Dim oXLApp As Object, oXLwb As Object, oXLws As Object
        
        Dim lRow As Long
        
        On Error Resume Next
        Set oXLApp = GetObject(, "Excel.Application")
        
        '~~> If not found then create new instance
        If Err.Number <> 0 Then
            Set oXLApp = CreateObject("Excel.Application")
        End If
        Err.Clear
        On Error GoTo 0
        
        '~~> Open the relevant file
        SavePath = "\\C:\Macro"
        Set oXLwb = oXLApp.Workbooks.Open(SavePath & "\Test.xlsx")
                   
        'Extract Mailbox and subfolder details from a sheet named as "Folder Details"
        
        Set oXLws = oXLwb.Sheets("Folder Details")
               
        ProdMail = oXLws.Range("B1")
        InputFolder = oXLws.Range("B2")
        OutputFolder = oXLws.Range("B3")
           
           
        strRowData = ""
        
        ' Code to extract emails from specific subfolder within shared folder and copy the data across excel spreadsheet.
        
        Set olRecip = mynamespace.CreateRecipient(ProdMail)
        
        Set ShareInbox = mynamespace.GetSharedDefaultFolder(olRecip, olFolderInbox)
        Set SubFolder = ShareInbox.Folders(InputFolder) 'Change this line to specify folder
        Set myDestFolder = ShareInbox.Folders(OutputFolder)
    The error is on the below line:
    Set SubFolder = ShareInbox.Folders(InputFolder)
    
    Below is the error message :

    Run-time error -2147221233 (8004010f):` The attempted operation failed. An object could not be found.

    Can anyone please help to troubleshoot this issue? Why the macro is working fine on personal mailbox but not on the shared mailbox.

    Many Thanks,
    Aman



  2. #2
    VBAX Contributor
    Joined
    Nov 2020
    Location
    Swansea,South Wales,UK
    Posts
    112
    Location
    Where is ShareInbox set?
    Look at the structure in the Locals window.

    Edit:Sorry, just noticed right above the error line.
    However are these objects getting valid values?
    Last edited by Gasman; 04-05-2024 at 08:52 AM.

  3. #3
    Thanks @Gasman - Below is the structure where Resolutions is the shared mailbox and the subfolders within it are _03 April and _04 April. The ShareInbox is giving me "Inbox" value


  4. #4
    Somehow it's not letting me send the screenshot

  5. #5
    VBAX Contributor
    Joined
    Nov 2020
    Location
    Swansea,South Wales,UK
    Posts
    112
    Location
    Quote Originally Posted by Derek_123 View Post
    Somehow it's not letting me send the screenshot
    I think you would need to attach the jpg.

    Regardless I believe that should be declared and set somewhere?

    OK, I am going blind, sorry.
    Walk your code with F8 and see if everything has valid values.

  6. #6
    All variable (ProdMail,InputFolder,OutputFolder,ShareInbox) have valid values in them . As I mentioned earlier, The below line is giving an error and before that line all variables store correct values

    Set SubFolder = ShareInbox.Folders(InputFolder) 'Change this line to specify folder
    

  7. #7
    Administrator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,254
    Location
    Have you tried it as below, keep in mind you need to specify the name of the shared mailbox in the below code:
        Dim mynamespace As outlook.Namespace
        Dim strRowData As String
        Dim strDelimiter As String
        Dim myDestFolder As outlook.Folder
        Dim ShareInbox As outlook.MAPIFolder
        Dim SubFolder As Object
        Dim InputFolder As String
        Dim OutputFolder As String
        Dim ProdMail As String
        Dim SavePath As String
        Dim oXLApp As Object, oXLwb As Object, oXLws As Object
        Dim lRow As Long
        
        On Error Resume Next
        Set oXLApp = GetObject(, "Excel.Application")
        
        '~~> If not found then create new instance
        If Err.Number <> 0 Then
            Set oXLApp = CreateObject("Excel.Application")
        End If
        Err.Clear
        On Error GoTo 0
        
        '~~> Open the relevant file
        SavePath = "\\C:\Macro"
        Set oXLwb = oXLApp.Workbooks.Open(SavePath & "\Test.xlsx")
                   
        'Extract Mailbox and subfolder details from a sheet named as "Folder Details"
        
        Set oXLws = oXLwb.Sheets("Folder Details")
               
        ProdMail = oXLws.Range("B1")
        InputFolder = oXLws.Range("B2")
        OutputFolder = oXLws.Range("B3")
           
           
        strRowData = ""
        
        ' Code to extract emails from specific subfolder within shared folder and copy the data across excel spreadsheet.
        Set mynamespace = outlook.GetNamespace("MAPI")
        Set ShareInbox = mynamespace.Session.Folders.Item("Name of shared mailbox") ' change to suit
        Set SubFolder = ShareInbox.Folders.Item(InputFolder) 'Change this line to specify folder
        Set myDestFolder = ShareInbox.Folders.Item(OutputFolder)
    When I tested the above code the folders that I located were in a shared mailbox but outside of the shared mailbox inbox so to speak.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2408, Build 17928.20080

  8. #8
    Thanks @georgiboy - So I created 2 subfolders within shared mailbox but outside the shared mailbox inbox and run the code by changing below 3 lines. Unfortunately it's still giving me the same error message. The code works fine on the personal email account. Do you think it's something to do with the permissions on the shared mailbox?

     Set ShareInbox = mynamespace.Session.Folders.Item(ProdMail) ' change to suit
        Set SubFolder = ShareInbox.Folders.Item(InputFolder) 'Change this line to specify folder
        Set myDestFolder = ShareInbox.Folders.Item(OutputFolder)

  9. #9
    Administrator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,254
    Location
    What is the value of ProdMail when you run the code?

    For example my shared mailbox as viewed on the folder view on the left hand view of Outlook is named: 'Distribution Returns'
    For this reason ProdMail would have to be 'Distribution Returns' as a text string and not someone's email address.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2408, Build 17928.20080

  10. #10
    Thanks a lot @georgiboy for all your help . It worked like a charm

  11. #11
    Administrator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,254
    Location
    You are welcome, glad we could help. Thanks for marking the thread as solved.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2408, Build 17928.20080

Posting Permissions

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