dougmarkham
Active Member
- Joined
- Jul 19, 2016
- Messages
- 252
- Office Version
- 365
- Platform
- Windows
Hi Folks,
Basic aim
I am looking to automate the adding of new email folders to Outlook 2016 for new jobs that come into our office.
Detail
I have created an excel model for handling new jobs. My goal is to put some VBA code into Outlook's VBA editor that:
1) Opens the excel model
2) Reads a Worksheet called OutlookFolders (see table below) containing Column A (Parent folder) and Column B (Folder names)
3) Adds a folder name matching that in Column B nested under the Parent folder in Column A.
I have looked into available VBA code for this purpose and all the examples are a few years old. Here is the code I have:
Create Outlook Folders from a List of Folder Names
I have tested this code. It opens the excel spreadsheet fine but gave the following error message when setting the object parent folder:
Run-time error '-2147221233 (8004010f)': The attempted operation failed. An object could not be found.
The line of code that the debug pointed to is:
Would you please help me to modify this code to work with Outlook 2016 (ms 365)?
Kind regards,
Doug
Basic aim
I am looking to automate the adding of new email folders to Outlook 2016 for new jobs that come into our office.
- We are using Microsoft outlook 2016 through a MS 365 contract on an exchange server controlled by Microsoft.
Detail
I have created an excel model for handling new jobs. My goal is to put some VBA code into Outlook's VBA editor that:
1) Opens the excel model
2) Reads a Worksheet called OutlookFolders (see table below) containing Column A (Parent folder) and Column B (Folder names)
3) Adds a folder name matching that in Column B nested under the Parent folder in Column A.
Parent folder (A1) | Folder names (B1) |
Inbox | Test Folder |
I have looked into available VBA code for this purpose and all the examples are a few years old. Here is the code I have:
Create Outlook Folders from a List of Folder Names
VBA Code:
Public Sub MoveSelectedMessages()
Dim objParentFolder As Outlook.Folder ' parent
Dim newFolderName 'As String
Dim strFilepath
Dim xlApp As Object 'Excel.Application
Dim xlWkb As Object ' As Workbook
Dim xlSht As Object ' As Worksheet
Dim rng As Object 'Range
Set xlApp = CreateObject("Excel.Application")
strFilepath = "S:\Office\JobManager.xlsm"
If strFilepath = False Then
xlApp.Quit
Set xlApp = Nothing
Exit Sub
End If
Set xlWkb = xlApp.Workbooks.Open(strFilepath)
Set xlSht = xlWkb.Worksheets("OutlookFolders")
Dim iRow As Integer
iRow = 2
'select starting parent
Set objParentFolder = Application.ActiveExplorer.CurrentFolder
Dim parentname
While xlSht.Cells(iRow, 1) <> ""
parentName = xlSht.Cells(iRow, 1)
newFolderName = xlSht.Cells(iRow, 2)
If parentName = "Inbox" Then
Set objParentFolder = Session.GetDefaultFolder(olFolderInbox)
Else
Set objParentFolder = objParentFolder.Folders(parentName)
End If
On Error Resume Next
Dim objNewFolder As Outlook.Folder
Set objNewFolder = objParentFolder.Folders(newFolderName)
If objNewFolder Is Nothing Then
Set objNewFolder = objParentFolder.Folders.Add(newFolderName)
End If
iRow = iRow + 1
' make new folder the parent
' Set objParentFolder = objNewFolder
Set objNewFolder = Nothing
Wend
xlWkb.Close
xlApp.Quit
Set xlWkb = Nothing
Set xlApp = Nothing
Set objParentFolder = Nothing
End Sub
I have tested this code. It opens the excel spreadsheet fine but gave the following error message when setting the object parent folder:
Run-time error '-2147221233 (8004010f)': The attempted operation failed. An object could not be found.
The line of code that the debug pointed to is:
VBA Code:
Set objParentFolder = objParentFolder.Folders(parentname)
Would you please help me to modify this code to work with Outlook 2016 (ms 365)?
Kind regards,
Doug