VBA Excel - Open a specific folder or subfolder in Outlook - excel

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

Related

Automate Outlook mail scheduler

I came across a code in Excel VBA which sends mail via Outlook based on given time ranges in the code. The "TO, CC, Subject and Mail Body" are all input in excel only.
The code works fine but .Send giving error 287-Application or object defined error. The code works well in my colleague's laptop, so I am guessing it is some setting error in my outlook or excel. I keep my outlook open while executing the code. The code is written below. Can anyone point to what might be wrong?
Sub Send_Email()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Sheet1")
'''''''''' Update Next ''''''''''''''
Call Update_Next_Schedule_Time
Application.OnTime sh.Range("K20").Value, "Send_Email"
''''''''''''''''''''''''''''''''''''''
Dim oa As Object
Dim msg As Object
Set oa = CreateObject("outlook.application")
Set msg = oa.createitem(0)
With msg
.To = sh.Range("C2").Value
.CC = sh.Range("C4").Value
.Subject = sh.Range("C6").Value
.Body = sh.Range("C8").Value
.display
.send
End With
End Sub
I believe this is due to your Outlook security policy. Check if it allows programmatic access. If not, and you cannot convince your IT department to make any change, then you should automate the opened outlook app rather that creating a new Outlook instance. Also, are you trying to send the email to yourself, by any chance?

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

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.

How to open an Outlook email template with Excel VBA?

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"

Modify an opened word document through Excel

How can I modify an opened word document through Excel with VBA?
Here a bit of code I'm writing, but there's something wrong I can't understand.
Dim WordDoc As Word.Document
Dim WordApp As Word.Application
'ThisWorkbook is the opened Excel Workbook through which I control Word documents.
If Len(Dir(ThisWorkbook.path & "\Report.docx")) <> 0 then
'if the document exists in the folder where ThisWorkbook is saved, I check
'if the document is already opened.
If IsFileOpened(ThisWorkbook.path & "\Report.docx")
'if during the run-time I get here, it means that the document exists and
'it's already opened.
'Now I want to get the reference to the opened document "Report.docx",
'so I do a thing like this.
Set WordDoc= Word.Application.Documents(ThisWorkbook.path & "\Report.docx")
'When it tries to excute the instruction over, it gives me a message in which
'it is written that the name is bad or inexistent, even if the document
'is already opened. Is the instruction correct?
Set WordApp= WordDoc.Application
'...other code
Else
'if the document isn't opened, I open it.
Set WordApp = CreateObject("Word.Application")
Set WordDoc = WordApp.Documents.Open(ThisWorkbook.path & "\Report.docx")
'..other code
End If
Else 'I create a new document called "Report.docx" if it doesn't exist
'in the folder where ThisWorkbook is saved.
Set WordApp = CreateObject("Word.Application")
Set WordDoc = WordApp.Documents.Add("Report.docx")
'.... other code
End If
Thanks in advance...
I tried this with an excel workbook and it worked
Set WordDoc= Word.Application.Documents(ThisWorkbook.path & "\Report.docx")
should be
Set WordDoc= Word.Documents("Report")
When I tried using the file path, I got Run-time error "9" Subscript out of range. When I used just the file name, it was successful.
Edit: After trying this with a word document, you do not need the application object and should not use the file extension. I can confirm that this works.
I tried this version
path = ThisWorkbook.path & "\Report.docx"
Set WordApp = GetObject(path).Application
in place of
Set WordDoc= Word.Application.Documents(ThisWorkbook.path & "\Report.docx")
and it works.

Resources