VBA to send emails using Outlook .oft template

ssh99

New Member
Joined
Oct 25, 2020
Messages
30
Office Version
  1. 2010
Platform
  1. Windows
  2. MacOS
I have a script I use to automate sending emails on Outlook. It works fine but I now want it to send the emails using a template. I have added the 'NewMail as Outlook.Object' line to the code to try and do this but get the following error: Run-Time Error 424: Object Required. I have checked the file mapping for the template to be used and it is correct. Can anyone help resolve this?

The VBA code is:

VBA Code:
Sub Send_Emails()



Dim OutApp As Object

Dim OutMail As Object

Dim OutAccount As Outlook.Account

Dim NewMail As Outlook.MailItem



For i = 2 To Sheet1.Cells(Rows.Count, 2).End(xlUp).Row

Set OutApp = CreateObject("Outlook.Application")

Set OutMail = OutApp.CreateItem(0)

Set OutAccount = OutApp.Session.Accounts.Item(1)

Set NewMail = obApp.CreateItemFromTemplate("G:\Shared\test")



On Error Resume Next

With NewMail

.To = Cells(i, 2).Value

.Attachments.Add Cells(i, 3).Value

.Importance = olImportanceHigh

.OriginatorDeliveryReportRequested = True

.Send

End With

On Error GoTo 0



Set OutMail = Nothing

Set OutApp = Nothing

Next



End Sub
 
Last edited by a moderator:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Code:
"G:\Shared\test.dot"
or whatever... U are missing the file extension. HTH. Dave
I updated the code so it now says "G:\Shared\test.oft". But the same error still comes up. Any idea what's wrong?
 
Upvote 0
Sorry if the file exists, I'm not sure what is wrong. Google seems to suggest trying opening the file manually and/or renaming the file.... or adding code to let the user pick the file instead of hard coding the file address and then use a variable derived from the file picker. Good luck. Dave
 
Upvote 0
To get it work change this ...
Rich (BB code):
Set NewMail = obApp.CreateItemFromTemplate("G:\Shared\test")

into this ...
Rich (BB code):
Set NewMail = OutApp.CreateItemFromTemplate("G:\Shared\test.oft")
 
Upvote 0
Solution
To get it work change this ...
Rich (BB code):
Set NewMail = obApp.CreateItemFromTemplate("G:\Shared\test")

into this ...
Rich (BB code):
Set NewMail = OutApp.CreateItemFromTemplate("G:\Shared\test.oft")
That's worked perfectly. Thank you!
 
Upvote 0
Glad it's sorted and thanks for letting me know.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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