VBA Warning pops up before macro runs

rplohocky

Active Member
Joined
Sep 25, 2005
Messages
292
Office Version
  1. 365
Platform
  1. Windows
Hello,
The code below works but only after the warning pops up. I get a critical warning pop up that says "No Such Template". Can anyone see why the warning pops up first then actually does what the macro is supposed to do?

<code>
Sub OL()



Dim objOL As Outlook.Application, msg As MailItem, p, i
Set objOL = CreateObject("Outlook.Application")

On Error Resume Next
For i = 1 To Range("h" & rows.Count).End(xlUp).Row
p = "C:\Users\RPlohocky\Desktop\Macro Projects\testing workbooks" & Cells(i, "h") & ".msg" ' template path
Set msg = objOL.CreateItemFromTemplate(p)

If Err.Number > 0 Then MsgBox "No such template", vbCritical, Cells(i, "h")


msg.Subject = "Driver Add Request Workflow# " & [d2] & " Fleet/Unit " & [e2] & " / " & [f2]
msg.Display
' msg.Send

Next
Set msg = Nothing
Set objOL = Nothing

End Sub
</code>
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
You use on error resume next which can be troublesome.

There must be an error in these lines:

Code:
[COLOR=#574123][FONT=monospace]For i = 1 To Range("h" & rows.Count).End(xlUp).Row[/FONT][/COLOR]
[COLOR=#574123][FONT=monospace]p = "C:\Users\RPlohocky\Desktop\Macro Projects\testing workbooks" & Cells(i, "h") & ".msg" ' template path[/FONT][/COLOR]
[COLOR=#574123][FONT=monospace]Set msg = objOL.CreateItemFromTemplate(p)[/FONT][/COLOR]

Because your error number must be greater than 0 as your message always deisplays:


Code:
[COLOR=#ff0000][FONT=monospace][/FONT][FONT=monospace][B]If Err.Number > 0[/B][/FONT][/COLOR][COLOR=#574123][FONT=monospace] Then MsgBox "No such template", vbCritical, Cells(i, "h")[/FONT][/COLOR]

If you remove the 'On Error Resume Next' line, this will tell you which line is causing the error as the code execution will stop
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

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