Outlook VBA to create a folder and setup a rule.

thardin

Board Regular
Joined
Sep 29, 2021
Messages
137
Office Version
  1. 365
Platform
  1. Windows
How do I add to this code to prevent a runtime error when someone runs this code for the first time and doesn't have a folder named "Voya" in Outlook?
In other words, how can I automatically create a folder in outlook named "Voya" and setup a rule that automatically moves a daily email with a specific subject to that folder? Thanks a lot :)

Sub Voya_Open_Most_Recent_Email()

Dim OlApp As Object
Dim OlMail As Object
Dim OlItems As Object
Dim OlFolder As Object
Dim i As Long

Set at = Report_529.Voya_at

StartTime = Time
at = Dash(85)
at = at & vbCrLf & Space(2) & "VOYA Report" & Space(25) & "Date: " & ejDate & Space(10) & "Time: " & Time
at = at & vbCrLf & Dash(85)

If Err <> 429 Then
Set OlApp = CreateObject("Outlook.Application")
Else
Set OlApp = GetObject(, "Outlook.Application")
End If


Set OlFolder = OlApp.GetNamespace("MAPI").Folders(My_Email).Folders("Voya")

Set OlItems = OlFolder.Items

i = OlItems.Count

OlFolder.Items(i).Display


Set OlFolder = Nothing
Set OlItems = Nothing
Set OlMail = Nothing
Set OlApp = Nothing

End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top