Adding a msgbox if error occurs after input box

jrake40

New Member
Joined
Nov 22, 2016
Messages
30
I am having trouble adding in code to display a msgbox when a user inputs a variable text value into my inputbox that cannot be found. My code is looking for a outlook folder matching what the user entered into the inputbox (this value is variable) and if it doesn't find it, the vba will error out. I want it to display a msgbox saying folder was not found and loop until they enter a correct one.


Code:
Sub GetFromInbox()

Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMail As Variant
Dim DateCount As Integer
Dim myDate1 As Date
Dim myDate2 As Date


Dim item As Object
Dim doClip As MSForms.DataObject
Dim xlApp As Object ' Excel.Application
Dim xlWkb As Object


inboxfldr = InputBox("Enter Outlook Folder Name", "Inbox Alert Folder")


Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox).Folders(inboxfldr)
Set doClip = New MSForms.DataObject
    
x = Date
myDate1 = Sheets("Inbox Alerts").Range("A1").Value
myDate2 = Sheets("Inbox Alerts").Range("B1").Value


For Each olMail In Fldr.Items
    If DateSerial(Year(olMail.ReceivedTime), Month(olMail.ReceivedTime), Day(olMail.ReceivedTime)) >= myDate1 And _
       DateSerial(Year(olMail.ReceivedTime), Month(olMail.ReceivedTime), Day(olMail.ReceivedTime)) <= myDate2 And _
       InStr(olMail.Subject, "Alert") >= 0 _
    Then
        doClip.SetText olMail.Body
        doClip.PutInClipboard
        
        Sheets("Inbox Alerts").Cells(Rows.count, 1).End(xlUp).Offset(1, 0).PasteSpecial "Text"
       DateCount = DateCount + 1
    End If
Next olMail


Set Fldr = Nothing
Set olNs = Nothing
Set olApp = Nothing
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Thanks Joe. I was more hoping for an error handling to handle the run-time error I get when it doesn't find the object.
 
Upvote 0
Thanks Joe. I was more hoping for an error handling to handle the run-time error I get when it doesn't find the object.
Best practices usually say it is better to check for the conditions that cause the errors, rather than add error handling code (that is kind of a last resort type of thing).
Why the resistance to using the solutions I referenced?

I prefer to use the Function option, as then it can be re-used easily time and time again, and it is easy to use it in your code by adding a simple IF block, i.e.
Code:
If Not CheckFolderExists(inboxfldr) Then
    MsgBox "Folder name: " & inboxfldr & " does not exist!",vbOkOnly, "ERROR!"
    Exit Sub
End If
 
Last edited:
Upvote 0
I'm not necessarily resistant to it. Just unsure how to get the code sample you gave to work properly in the macro.
 
Upvote 0
Its much easier than you think.

Just copy the Function verbatim to your VB Editor, i.e.
Code:
[COLOR=#000000][FONT=&quot]Function CheckFolderExists(strPath As String) As Boolean[/FONT][/COLOR]
[COLOR=#000000][FONT=&quot]    On Error Resume Next[/FONT][/COLOR]
[COLOR=#000000][FONT=&quot]    Err.Clear[/FONT][/COLOR]
[COLOR=#000000][FONT=&quot]    ChDir strPath[/FONT][/COLOR]
[COLOR=#000000][FONT=&quot]    If Err.Number = 0 Then CheckFolderExists = True[/FONT][/COLOR]
[COLOR=#000000][FONT=&quot]End Function[/FONT][/COLOR]
And then you can use it like any other Excel function, and use it like I demonstrated in my previous post.
That is all there is to it!
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

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