How to open an Outlook email template with Excel VBA? - excel

I am trying to open an Outlook 2010 email template with Excel. I get the following error:
"Run-time error '-2147287038 (80030002)':
Cannot open file:
C:\My\Path\MyTemplate.oft. The file may not exist, you may not have permission to open it, or it may be open in another program. Right-click the folder that contains the file, and then click Properties to check your permissions for the folder."
The file exists (in the right place), I checked permissions (as far as I know how), and the file is not open.
Sub Mail_experiment()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.mailitem
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItemFromTemplate("C:\My\Path\MyTemplate.oft")
On Error Resume Next
With OutMail
.to = "myEmail#aol.com"
.CC = ""
.BCC = ""
.Subject = "This is my Subject line"
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub

"I tried my code on a different computer and it worked. I went back and changed the file name of my template, as the previous name was in all capitals (shouldn't matter, right?). I got a message about needing to be an administrator to do this, which I thought was strange since this is my personal home computer. However, when I clicked "continue" it went ahead and changed the name. Now it works. – David Elphee Sep 14 '14 at 13:23"

Related

Is it possible to have an excel macro open outlook in browser and populate the fields of a new message as well as attach a file?

Question: Is it possible to create a macro that opens outlook in a web browser and populates the fields of a new message as well as attach a file? The outlook portion of the current macro only opens outlook in a browser.
ActiveWorkbook.FollowHyperlink Address:="https://outlook.office365.com/mail/**shared mailbox address**"
Background: I am trying to update an excel macro that currently saves a pdf of the sheet, opens the outlook application, fills out the necessary fields and attaches the saved pdf to the email. This macro has worked fine, but we have recently moved to using a shared mailbox to send the message. Now the users have encountered problems sending from the shared mailbox using the outlook application. The solution is to use outlook in the browser (edge), but the macro I currently have can only open outlook in the browser and requires the user to fill out all the fields and find and attach the saved pdf. There have been problems with this and I was hoping there was a way to automate the process like our old macro would.
Old macro:
Set OlApp = CreateObject("Outlook.Application")
Set NewMail = OlApp.CreateItem(0)
On Error Resume Next
With NewMail
.To = ReportName
.CC = ""
.Subject = TempFileName
.Body = ""
.Attachments.Add FileFullPath '--- full path of the pdf where it is saved
.Display '.Send or use .Display to show you the email before sending it.
End With
On Error GoTo 0
Well, Yes and No.
Yes, you can get VBA to do this... but No, it won't be remotely as easy as running it through the desktop application.
A workaround that still uses desktop application, is to
Give all users that need to send the email access to this inbox from their own desktop apps.
Use the ".SendUsingAccount" property
Sub ExampleSub()
Dim OLApp As Object
Dim NewMail As Object
Set OLApp = CreateObject("Outlook.Application")
Set NewMail = OLApp.CreateItem(0)
On Error Resume Next
With NewMail
.SentOnBehalfOfName = "MySecondaryAddress#Domain.com"
.To = "someaddress#gmail.com"
.CC = ""
.Subject = "Example Subject"
.Body = "Good Morning..."
'.Attachments.Add FileFullPath '--- full path of the pdf where it is saved
.Display '.Send or use .Display to show you the email before sending it.
End With
On Error GoTo 0
End Sub

Sending emails from excel in outlook no longer works

Before I go asking for help I want to let you know that I am not very good at this and have just learnt bits and bobs from online pages and forums when coding.
I've got a spreadsheet which I set up for sending out for quotations to certain suppliers (repetitive). I have been using this spreadsheet for almost 2 years now and it's been working fine. Late last week I tried to click on both buttons and found that excel and outlook froze up for a minute but no email was ever generated or sent. I assume something in the settings has changed due to an update as it's suddenly stopped working. I am using Office365. I noticed in outlook options under Add-ins that 'Microsoft VBA for Outlook Addin' is listed under Inactive Application Add-ins...for whatever that's worth
Screenshot - (https://ibb.co/fv2Pzx7)
Normally I would add the details for each of the files that I wish to attach on the right hand side (the directory of the file and the file name). The Job Name at the bottom right is the subject.
In VBAProject Reference I have the following boxes ticked:
Visual Basic for Applications
Microsoft Excel 16.0 Object Library
OLE Automation
Microsoft Office 16.0 Object Library
My code is quite basic I think (seeing as though even I could do it!)
Sub GenerateAutoEmail3_Click()
On Error GoTo ErrHandler
' SET Outlook APPLICATION OBJECT.
Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")
' CREATE EMAIL OBJECT.
Dim objEmail As Object
Set objEmail = objOutlook.CreateItem(olMailItem)
With objEmail
.To = Range("E3")
.Subject = Range("F3")
.Body = "Hi " & Range("C3") & "," & Range("R13") & Range("D3") & Range("R14")
.Attachments.Add Range("I3").Text
.Attachments.Add Range("K3").Text
.Attachments.Add Range("M3").Text
.Attachments.Add Range("O3").Text
.Send ' DISPLAY MESSAGE.
End With
' CLEAR.
Set objEmail = Nothing: Set objOutlook = Nothing
ErrHandler:
Range("G3").Value = Now
'
End Sub
(Couldn't get those last few lines in the code box!...)
Sorry if I haven't done a good job of explaining my issue or if I've rabbled on for no reason!
Thanks

Task Scheduler Opens Excel file but keeps running and does not complete

I have the code below in a Module. I've tried using Workbook_Open in the 'ThisWorkbook' and I've tried using Auto_Open in the Sheet. Neither one would run the code upon opening the spreadsheet. This is why my code is in a Module. The program runs fine if I manually open it. However, when using the task scheduler it opens the excel file and keeps running. It doesn't execute the code. The code is just a simple email with an attachment.
As for Task Scheduler, In the General tab, I have Run only when the user is logged on, and Run with the highest privileges checked. I also have Wake the computer to run this task checked in conditions.
Sub Auto_Open()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Application.DisplayAlerts = False
On Error Resume Next
With OutMail
.to = "bjenks#ormat.com"
.CC = ""
.BCC = ""
.Subject = "Test Workbook Open"
.Body = "Hi there"
.Attachments.Add ("C:\Users\bjenks\Desktop\Test.xlsx")
.Send 'or use .Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
I unchecked Run with the highest privileges and change to Public and it worked.

VBA Excel - Open a specific folder or subfolder in Outlook

I am trying to open outlook from a VBA macro in Excel. I was able to open it but how do make it go to a specific folder? Let's say the "sent Items," "Draft folder," etc. Also, how about choosing a folder in another mailbox? I have two mailboxes within my Outlook.
Here's the code that I have so far:
Sub my_prov_openOutlook()
Dim oOutlook As Object
On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
On Error GoTo 0
If oOutlook Is Nothing Then
Shell ("OUTLOOK")
'''I would like to open a specific folder under the inbox or under a subfolder
Else
oOutlook.ActiveWindow.Activate
'''I would like to open a specific folder under the inbox or under a subfolder
End If
Do not use GetObject with Outlook - use CreateObject. Outlook is a singleton: if it is already running, you will get a pointer to the existing instance. If it is not running, it will start up.
Also don't use On Error Resume Next - you won't know if you get an error and where it occurs.
Set oOutlook = CreateObject("Outlook.Application")
set oNS = oOutlook.GetNamespace("MAPI")
oNS.Logon 'does not do anything if Outlook is already running
set oFolder = oNS.GetDefaultFolder(6) 'olFolderInbox
if (oOutlook.ActiveExplorer Is Nothing) Then
oFolder.Display
Else
set oOutlook.ActiveExplorer = oFolder
End If

Error While Sending Email through Outlook from Excel VBA

I have write a macro in outlook and excel vba, the description are:
1. Code in Outlook for Open Excel file if email subject line match :
Private Sub Items_ItemAdd(ByVal Item As Object)
If TypeOf Item Is Outlook.MailItem Then
'// Subject line here
If InStr(Item.Subject, "Run Dashboard") Then
Call openExcel
End If
End If
End Sub
Once Excel is open and dashboard has run then email has to be sent via excel.
vba and code:
Dim outapp As Object
Dim nmail As Object
Set outapp = CreateObject("Outlook.Application")
Set nmail = outapp.CreateItem(0)
With nmail
.To = "xxxxxx#xxxx.com"
.cc = ""
.bcc = ""
.Subject = flname
.htmlbody = RangetoHTML(Range("A1:" & Split(Cells(, lastcol1).Address, "$")(1) & lastrow1))
.attachments.Add ActiveWorkbook.FullName
.display
End With
On Error GoTo 0
Set nmail = Nothing
Set outapp = Nothing
Now I am facing the error on Set outapp = CreateObject("Outlook.Application")
This error is only showing if i open excel file through outlook email as mentioned in point 1, if i open file in normal way i.e. without outlook help, then code is running successfully.
Please help in the same.
Thanks in advance
Why do you need to automate Excel from Outlook and then Outlook from Excel?
Set outapp = CreateObject("Outlook.Application")
Instead you may get the running Outlook instance (if any) because only one instance of Outlook can be run simultaneously. See How to automate Outlook from another program and GetObject and CreateObject behavior of Office automation servers for more information.
Try to use the following line of code instead:
Set nmail = Application.CreateItem(olMailItem)
If you have multiple profiles in Outlook configured most probably you will need to use the Logon method of the Namespace class.

Resources