PDA

View Full Version : How to automate folder creation?



totalnoob
01-11-2018, 09:46 AM
Hello everyone, you were all such great help with my last question so I thought I would ask for a little more if you don't mind!!

Here's what I'm trying to do:
I have groups of new employees coming in and I want to help them organize important emails. I want to somehow automate folder creation and assign rules to those folders. I want them ideally to run a batch file or something they can just double-click from the desktop which will then go through the process of creating Folder 1, Folder 2, etc. then create rules to send emails to those folders.

Here's what I have tried:
I know you can automate folder creation using an excel list, here is my code for that:

Option Explicit
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 = xlApp.GetOpenFilename
If strFilepath = False Then
xlApp.Quit
Set xlApp = Nothing
Exit Sub
End If

Set xlWkb = xlApp.Workbooks.Open(strFilepath)
Set xlSht = xlWkb.Worksheets(1)
Dim iRow As Integer

iRow = 2

Set objParentFolder = Application.ActiveExplorer.CurrentFolder
While xlSht.Cells(iRow, 1) <> ""
newFolderName = xlSht.Cells(iRow, 1)

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


So now I'm just trying to find a way to somehow get them to run this macro in Outlook without having them manually get into VBA to paste it or import it. Is there a way to do this that you can think of? Maybe something that will import the .bas and run it? Google has been no help.

I haven't even started tackling the rule creation part, but any insight on that front would be extremely helpful as well.