0

I have Outlook 2010. I get emails with the same subject line, with a PDF to open. When the PDF is open, Adobe asks if I want to add it to an Excel response file, and I say yes.

I'd like to have it respond with "Okay" when Adobe asks about adding to the response file, but I can manage without it. At this line:

Set SubFolder = Mailbox.Folders("Response File")

I am getting an error:

The attempted operation failed. An object could not be found.

The subfolder where the unread emails are is called "!Response File" (without quotes) underneath my Inbox. After having the PDF opened, I'd like to mark the email as read, and moved to another subfolder (under Inbox) called "Extracted" (without quotes).

Sub GetAttachments()
  On Error GoTo GetAttachments_err
  Dim ns As NameSpace
  Dim Inbox As MAPIFolder
  Dim SubFolder As MAPIFolder
  Dim Item As Object
  Dim Atmt As Attachment
  Dim FileName As String
  Dim i As Integer

  Set ns = GetNamespace("MAPI")
  Set Inbox = ns.GetDefaultFolder(olFolderInbox)
  Set Mailbox = Inbox.Parent
  Set SubFolder = Mailbox.Folders("!Response File")
  i = 0

  'check if there is any mail in the folder'
  If SubFolder.Items.Count = 0 Then
    MsgBox "There are no messages in the folder.", vbInformation, _
    "Nothing Found"
    Exit Sub
  End If

  'Check each message and save the attachment'
  If SubFolder.Items.Count > 0 Then
    For Each Item In SubFolder.Items
      If Item.UnRead = True Then
        For Each Atmt In Item.Attachments
          FileName = "C:\Users\abrupbac\Desktop\Response Emails\" & Atmt.FileName
          Atmt.SaveAsFile FileName 'saves each attachment'

          'this code opens each attachment'
          Set myShell = CreateObject("WScript.Shell")
          myShell.Run FileName

          'this sets the email as read'
          Item.UnRead = False
          'updates the counter'
          i = i + 1

        Next Atmt
      End If
    Next Item
  End If

  'Display results

  If i > 0 Then
    MsgBox "I found " & i & " attached files." _
     & vbCrLf & "They are saved on your desktop" _
     & vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
  Else
    MsgBox "I didn't find any attached files in your mail.", vbInformation, _
     "Finished!"
 End If

'Replenish Memory'
GetAttachments_exit:

  Set Atmt = Nothing
  Set Item = Nothing
  Set ns = Nothing
  Exit Sub

  'function for sorting the excel attachment'

GetAttachments_err:
  MsgBox "An unexpected error has occurred." _
  & vbCrLf & "Please note and report the following information." _
  & vbCrLf & "Macro Name: GetAttachments" _
  & vbCrLf & "Error Number: " & Err.Number _
  & vbCrLf & "Error Description: " & Err.Description _
  , vbCritical, "Error!"
  Resume GetAttachments_exit
End Sub
Leigh
  • 28,765
  • 10
  • 55
  • 103
Tony
  • 3
  • 1
  • 6

1 Answers1

1

welcome to StackOverflow!

To answer your specific question,

I get a "The attempted operation failed. An object could not be found." error at:
Set SubFolder = Mailbox.Folders("!Response File")

You get this error because "!Response File" is not within the parent of Inbox. It can be tricky to find a folder by name. You could instead access the folder by ID. One way to get the ID of a desired folder is to write a function to do so.

    Function GetInboxFolderID(FolderName As String) As String
    Dim nsp As Outlook.Folder
    Dim mpfSubFolder As Outlook.Folder
    Dim mpfSubFolder2 As Outlook.Folder
    Dim flds As Outlook.Folders
    Dim flds2 As Outlook.Folders

    Set nsp = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
    Set flds = nsp.Folders
    Set mpfSubFolder = flds.GetFirst
    Do While Not mpfSubFolder Is Nothing
        If mpfSubFolder.Name = FolderName Then
            GetInboxFolderID = mpfSubFolder.EntryID
            Exit Function
        End If
        Set flds2 = mpfSubFolder.Folders
        Set mpfSubFolder2 = flds2.GetFirst
        Do While Not mpfSubFolder2 Is Nothing
            If mpfSubFolder2.Name = FolderName Then
                GetInboxFolderID = mpfSubFolder2.EntryID
                Exit Function
            End If
            Set mpfSubFolder2 = flds2.GetNext
        Loop
        Set mpfSubFolder = flds.GetNext
    Loop
End Function

Additionally, here is a code to test it.

Sub testing()
Dim tv As String
tv = GetInboxFolderID("Response File")
  Set myNewFolder = Application.Session.GetFolderFromID(tv)
 myNewFolder.Display

End Sub 

This function loops thorugh your main set of user folders, then checks each of these folders for the string given in folder name. If the function finds it, then it returns the ID to that folder.

The testing subroutine is just there for debugging purposes, and when you run it, it should open the folder you named in the function i.e "Response File"

Changing your line :

Set SubFolder = Mailbox.Folders("!Response File")

To:

Set SubFolder = Application.Session.GetFolderFromID(GetInboxFolderID("Response File"))

Should get you past your current bug, if you implement my function.

Additionally, you may be able to close the "Okay" message using SendKeys

Call AppActivate("Adobe Reader", True)
 DoEvents
 SendKeys "{Enter}"

Hope this helps!

JDB_Dragon
  • 162
  • 10
  • Wow, JDB, thank you for the detailed response! I will try out your suggestion (it may be a little over my head), but I'm going to give it a go. Thanks so much for your help!! – Tony Dec 17 '15 at 15:22
  • I'm having trouble where I need to put this function routine. Again, still learning VBA. Thank you! – Tony Dec 17 '15 at 15:32
  • At the bottom of your code, the line End Sub marks the end of the Sub-Routine. Sub-Routines and Functions are the two main types of procedures. To add the function, add it after the End Sub line. This will define it as a seperate procedure, and will allow you to call it seperately. Let me know if this is clear, It can be hard to explain without visual aide. – JDB_Dragon Dec 17 '15 at 15:35
  • Thank you - I placed the function routine at the end (and commented out the GoTo Error to see where I was getting an error). So, I am getting an error "Could not open the item. Try again." at this line: `Set SubFolder = Application.Session.GetFolderFromID(GetFolderIDByName("!Response File"))` Do I need to have the function variables declared at the beginning of the Subroutine? Sorry for my ignorance. Thank you. – Tony Dec 17 '15 at 15:52
  • Did you try my testing sub-routine to see if it worked properly? – JDB_Dragon Dec 17 '15 at 15:54
  • Yes, and I'm getting the "Could not open the item" error. I commented out the original sub when testing. Sorry about all the formatting. – Tony Dec 17 '15 at 15:58
  • I updated the "testing "code in my answer to help us debug this. The original code I wrote for that sub was in bad form, but it was meant as a "quick-test". Now, if you get a message box telling you "Failed to find folder" then our issue may be you are spelling it wrong, or it is located within a folder of a folder, so 3 deep in the file tree. – JDB_Dragon Dec 17 '15 at 16:13
  • I am getting the "Failed to find folder" error, so I did two things. I removed the "!" part of the name to see if that was an issue. Also, copied the exact name of the folder and pasted it in the code to make sure the two spellings are the same. The Response File is directly below the Inbox folder, with no subfolders. So, it should be a second level folder, right? – Tony Dec 17 '15 at 16:18
  • It should be a second level yes. And did it still not work with the name pasted in? Additionally, did you update both of my codes or just the testing one? I did add one line (the first line) to the GetID funtion. This will help catch the error. – JDB_Dragon Dec 17 '15 at 16:21
  • Still got the Failed to find folder error. I updated both codes. I renamed the folder "A" and the sub code with "A" just to be sure there were no spelling errors (one letter, right?) hahah. Anyway, still coming up with the error. Do you think that my reference library needs to be updated, and if so, with what? – Tony Dec 17 '15 at 16:35
  • Outside of VBA, right click the menu showing your available folders and do Add Folder. Inside the resulting menu, could you tell me where the desired folder is located? ie. Public Folders->Response – JDB_Dragon Dec 17 '15 at 18:07
  • @JBD_Dragon, sorry for the delay: I right-click on Inbox and "Create New Folder" ... the resulting dialog says "Folder contains: Mail and Post Items" Is this what you are referring to? – Tony Dec 17 '15 at 19:50
  • It's under my account name under inbox, so Tony@company.com -> Inbox - Response File (now named "A"). – Tony Dec 17 '15 at 20:00
  • There, that was the problem! It was three deep. Whew, I think we got it. I could have solved this with recursion, but that's an advanced topic for a begginner. I have updated all of the code sections appopriately to search your "Inbox" folder for sub-folders up to two deep. Let me know if it works. – JDB_Dragon Dec 17 '15 at 20:09
  • I changed the name of the function to GetInboxFolderID Use: `Set SubFolder = Application.Session.GetFolderFromID(GetInboxFolderID("Response File"))` – JDB_Dragon Dec 17 '15 at 20:30
  • Solved that error. (name was wrong). Now when I run the code (debugging was fine), I get "Method 'Run' of object 'WshSchell3' failed. at this line: `myShell.Run FileName` – Tony Dec 17 '15 at 20:38
  • Try replacing Set myShell = CreateObject("WScript.Shell") : myShell.Run FileName with Shell("C:\Program Files (x86)\Adobe\Reader 11.0\Reader\acrord32.exe " & FileName, vbNormalNoFocus) Before your report back change the path to where your reader is located, instead of just using the one I have on my 64-bit machine. – JDB_Dragon Dec 17 '15 at 20:52
  • It's not taking that syntactically. What am I removing and replacing? I'm confused. Never worked with shell before. Thank you. By the way, the path to my reader is exactly as you have it on your post. – Tony Dec 17 '15 at 21:09
  • Remove : `Set myShell = CreateObject("WScript.Shell") : myShell.Run FileName` Add: `Shell("C:\Program Files (x86)\Adobe\Reader 11.0\Reader\acrord32.exe " & FileName, vbNormalNoFocus)` – JDB_Dragon Dec 17 '15 at 21:11
  • Add `Call` in front of it. Additionally, try not to drag questions like this out in comments. Small things like this you can google or try to figure out yourself. – JDB_Dragon Dec 17 '15 at 21:17
  • True. I got hung up on trying to figure out the shell business. Thank you greatly for your patience and your help! I'll see if I can take it from here. :) Thought you'd be interested to know that the Outlook portion worked. Had a bit of trouble on the Adobe side, so I'm going to try and figure that out. Thanks again! – Tony Dec 17 '15 at 21:21
  • No problem Tony, if my answer helped, please mark is as the answer so that this can be considered closed. If you have further questions on the Adobe side, they should be asked in a seperate question. -JDB – JDB_Dragon Dec 17 '15 at 21:24