0

My Outlook macro worked at one time, then stopped. When it parses, the macro throws an error "Compile Error: Variable not defined". It seems the macro does not recognize that that folder exists. I cut the code to the bare minimum and it is a repeatable problem. The macro will recognize standard folders such as JUNK and DRAFTS but not PROCESSED_FOLDERS. I have tried renaming Processed_Folders as well as creating a new folder with a different name. No joy.

Folder structure is:

reports@xxx.com
Inbox
      Drafts
      Sent
      Trash
      Junk
      Processed_Reports
Outbox
Sync Issues1 (This computer only)
SearchFolders

CODE:

Sub testfforfolder()
    Dim olApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Dim olFolder As Outlook.MAPIFolder
    Dim msg As Outlook.MailItem
    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
    On Error GoTo xyz
    Set olFolder = olFolder.Folders("Processed_Reports")
    MsgBox "Folder Exists" ' This line works if I use DRAFTS or JUNK
    Exit Sub
xyz:
    MsgBox ("Cannot find Folder") ' I get here if I use PROCESSED_REPORTS
    Exit Sub
End Sub
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
E Meger
  • 1
  • 1
  • (1) What line is highlighted for the "variable not defined" error? (2) Your code relies on "reports@xxx.com" containing the default inbox. Is this true? On my system the default inbox is in "outlook data file". – Tony Dallimore Jun 13 '17 at 14:41
  • At the beginning of this answer, https://stackoverflow.com/a/12146315/973283, there is a macro that may help confirm the cause of the problem. Try `Set oFolder = Session.Folders("reports@xxx.com").Folders("Inbox")` – Tony Dallimore Jun 13 '17 at 14:50
  • This script throws the "Cannot Find Folder" messagebox alert. In the original script the word INBOX was highlighted in a line that read: olm.Move Inbox.Folders("Processed_Reports") [Note: olm was DIM olm As Outlook.MailItem] After struggling I isolated the problem to this issue of not recognizing the folder. – E Meger Jun 14 '17 at 16:25
  • Thank you TONY! That link let me solve the problem. With an updated version of outlook, the default email account was being referenced rather than the account of the selected item. The Processed_Reports folder only existed in a different account folder. The solution, as Tony suggested, was to set the target folder to the full path to the target. I did need one more level: Set TgtFolder = Session.Folders("reports@xxx.com").Folders("Inbox").Folders("Processed_Reports"). From there I could easily move items to TgtFolder. – E Meger Jun 14 '17 at 17:14

2 Answers2

0

Thanks to the comment by Tony Dallimore I was able to solve the issue. The link to https://stackoverflow.com/a/12146315/973283 let me solve the problem. With an updated version of Outlook, the default email account was being referenced rather than the account of the selected item. The Processed_Reports folder only existed in a different account folder. The solution, as Tony suggested, was to set the target folder to the full path to the target. I did need one more level as shown in the working solution below.

Sub testfforfolder()
        Dim olApp As Outlook.Application
        Dim objNS As Outlook.NameSpace
    Dim olFolder As Outlook.MAPIFolder
    Dim msg As Outlook.MailItem
    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
    On Error GoTo xyz
'OLD INCORRECT     
    'Set olFolder = olFolder.Folders("Processed_Reports")
'WORKING CORRECTION
    Set TgtFolder= _
    Session.Folders("reports@xxx.com"). _
    Folders("Inbox").Folders("Processed_Reports")

    MsgBox "Folder Exists" ' This line works if I use DRAFTS or JUNK
    Exit Sub
xyz:
    MsgBox ("Cannot find Folder") ' I get here if I use PROCESSED_REPORTS
    Exit Sub
End Sub
E Meger
  • 1
  • 1
0

here is a way to get the session folder without knowing the session name

Sub topFolder()

    Dim topFolder As Folder
    Set topFolder = Application.Session.GetDefaultFolder(olFolderInbox).Parent

    Dim i As Integer

    For i = 1 To topFolder.Folders.Count
        Debug.Print topFolder.Folders(i).Name
    Next i

    For i = 1 To topFolder.Folders("inbox").Folders.Count
        Debug.Print topFolder.Folders("inbox").Folders(i).Name
    Next i

End Sub
jsotola
  • 2,238
  • 1
  • 10
  • 22