In Outlook 2013, can a macro open a new custom form? - vba

In Outlook 2013 using Developer tab -> Design a Form, I created a custom form (with no mods yet) from the delivered Message form and placed it in my Personal Forms Library. Outlook tells me that the Message class is: IPM.Note.MyForm
I've created a macro and set up a new ribbon button to run the macro. I would like the macro to open a new instance of my custom form, but I can't get it working.
With the following code I can get the macro to open a new instance of the delivered Message Form:
Set newItem = Application.CreateItem(olMailItem)
newItem.Display
Set newItem = Nothing
I can't get it to open my custom form. I've tried the following as arguments to CreateItem: olMailItem.MyForm and IPM.Note.MyForm.
The macro editor intellisense has about 9 options for arguments to CreateItem, all of them appear to be delivered objects/forms, and it errors if one of these options aren't used.
I've done very little vba and office macros, is there some way to get this macro to open my custom form? Thanks.

See Items.Add http://msdn.microsoft.com/en-us/library/office/ff861028(v=office.15).aspx
Sub AddForm()
Dim myNamespace As outlook.NameSpace
Dim myItems As outlook.Items
Dim myFolder As outlook.Folder
Dim myItem As outlook.MailItem
Set myNamespace = Application.GetNamespace("MAPI")
Set myFolder = myNamespace.GetDefaultFolder(olFolderInbox)
Set myItems = myFolder.Items
Set myItem = myItems.Add("IPM.Note.MyForm")
End Sub

Related

Create email, based on Outlook template, from a Word menu

This is the code for other Word templates on the menu.
Private Sub "button name_Click()
Unload ####Menu
End Sub
This is code I've seen to create an Outlook item from Word.
Sub CreateFromTemplate()
Dim MyItem As Outlook.MailItem
Set MyItem = Application.CreateItemFromTemplate("C:\statusrep.oft")
MyItem.Display
End Sub
Sub CreateTemplate()
Dim MyItem As Outlook.MailItem
Set MyItem = Application.CreateItem(olMailItem)
MyItem.Subject = "Status Report"
MyItem.To = "Dan Wilson"
MyItem.Display
MyItem.SaveAs "C:\statusrep.oft", OlSaveAsType.olTemplate
End Sub
How do I combine these?
It seems you just need to automate Outlook from Word VBA. To start an Outlook Automation session, you can use either early or late binding. Late binding uses either the Visual Basic GetObject function or the CreateObject function to initialize Outlook. For example, the following code sets an object variable to the Outlook Application object, which is the highest-level object in the Outlook object model. All Automation code must first define an Outlook Application object to be able to access any other Outlook objects.
Dim objOL as Object
Set objOL = CreateObject("Outlook.Application")
To use early binding, you first need to set a reference to the Outlook object library. Use the Reference command on the Visual Basic for Applications (VBA) Tools menu to set a reference to Microsoft Outlook xx.x Object Library, where xx.x represents the version of Outlook that you are working with. You can then use the following syntax to start an Outlook session.
Dim objOL as Outlook.Application
Set objOL = New Outlook.Application
Most programming solutions interact with the data stored in Outlook. Outlook stores all of its information as items in folders. Folders are contained in one or more stores. After you set an object variable to the Outlook Application object, you will commonly set a NameSpace object to refer to MAPI, as shown in the following example.
Set objOL = New Outlook.Application
Set objNS = objOL.GetNameSpace("MAPI")
Set objFolder = objNS.GetDefaultFolder(olFolderContacts)
Once you have set an object variable to reference the folder that contains the items you wish to work with, you use appropriate code to accomplish your task, as shown in the following example.
Sub CreateNewOutlookMail()
Dim objOLApp As Outlook.Application
Dim NewMail As Outlook.MailItem
' Set the Application object
Set objOLApp = New Outlook.Application
' You can only use CreateItem for default items
Set NewMail = objOLApp.CreateItem(olMailItem)
' Display the new mail form so the user can fill it out
NewMail.Display
End Sub
See Automating Outlook from a Visual Basic Application for more information.

How to switch views in Outlook

I'm trying to create an add-in for Microsoft Outlook. I'm at the beginning part of writing the add-in, and what I'd like to have happen is when the user clicks the button I've made, the view switches from whatever they're looking at (either the inbox, calendar, tasks, etc.) to their contacts list.
After much trial and error, this was as far as I got. But I know i'm a ways away.
Dim myNameSpace As Outlook.NameSpace = Nothing
myNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderContacts).Display()
Set Application.ActiveExplorer.CurrentFolder property to the folder in question (e.g. the folder returned by GetDefaultFolder).
Here is it:
Dim contactsFolder as Outlook.Folder = Nothing
Dim myNameSpace As Outlook.NameSpace = Nothing
myNameSpace = Application.GetNamespace("MAPI")
contactsFolder = myNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderContacts)
Application.ActiveExplorer.CurrentFolder = contactsFolder
The CurrentFolder property sets a Folder object that represents the current folder displayed in the explorer.

How I can run macro automatically with new email arrives in outlook

Am very new to VBScript and macro . I have a vbscript which will create tickets for email's but I need to run this script manually every time .To make it automation I have mapped my macro to the "run a script" script rule in Outlook but when the rule runs it is not fetching the data in the mail which is arrived .It's creating tickets with previous email always .
I have gone through so many VBscripts but none worked to convert the mail that received in to ticket.
If any one faced similar type of issue please let me know the complete solution.
Dim olApp As outlook.Application
Dim objNS As outlook.NameSpace
Dim objSourceFolder As outlook.MAPIFolder
Dim objDestFolder As outlook.MAPIFolder
Dim Msg As outlook.MailItem
Set olApp = outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set objSourceFolder = objNS.GetDefaultFolder(olFolderInbox)
Set objDestFolder = objSourceFolder.Folders("Termination")
Set Msg = objDestFolder.Items.GetLast
Set sh = CreateObject("Shell.Application")
Set ie = CreateObject("InternetExplorer.Application")
LocationURL = "Ticket URL” & Msg.EntryID"
ie.Navigate (LocationURL)
ie.Visible = True
With ie.Document
.getElementById("details").Value = Msg.Body
.getElementById("short_description").Value = Msg.Subject
.getElementById("requester_login").Value = "premchand"
End With
When you run a macro from a rule it typically has an argument to which the item triggering the rule gets passed: you do not have to navigate through the Inbox to find the item.
Public Sub DoSomething(Item As Outlook.MailItem)
'code which acts on "Item"
End Sub
There are several ways for handling incoming emails:
The NewMailEx event of the Application class which is fired when a new item is received in the Inbox. This event is not available in Microsoft Visual Basic Scripting Edition (VBScript).
The ItemAdd event of the Items class is fired when one or more items are added to the specified collection. So, you can subscribe to the Inbox folder to see new items. Be aware, this event does not run when a large number of items are added to the folder at once. This event is not available in Microsoft Visual Basic Scripting Edition (VBScript).
Assign a macro VBA sub to the rule in Outlook. In that case the incoming mail item is passed as a parameter to the sub as Tim showed.

Call Outlook procedure using VBScript

I have a procedure in Outlook that sends all the saved messages in Drafts folder.
Below is the code:
Public Sub SendMail()
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim olDraft As Outlook.MAPIFolder
Dim strfoldername As String
Dim i As Integer
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(olFolderInbox)
strfoldername = olFolder.Parent
Set olDraft = olNS.Folders(strfoldername).Folders("Drafts")
If olDraft.Items.Count <> 0 Then
For i = olDraft.Items.Count To 1 Step -1
olDraft.Items.Item(i).Send
Next
End If
End Sub
Above code works fine.
Question:
I want to use Task Scheduler to fire this procedure as a specified time.
1. Where will I put the procedure in Outlook, Module or ThisOutlookSession?
2. I am not good in vbscript so I also don't know how to code it to call the Outlook Procedure. I've done calling Excel Procedure but Outlook doesn't support .Run property.
So this doesn't work:
Dim olApp
Set olApp = CreateObject("Outlook.Application")
olApp.Run "ProcedureName"
Set olApp = Nothing
I've also read about the Session.Logon like this:
Dim olApp
Set olApp = CreateObject("Outlook.Application")
olApp.Session.Logon
olApp.ProcedureName
Set olApp = Nothing
But it throws up error saying object ProcedureName is not supported.
Hope somebody can shed some light.
SOLUTION:
Ok, I've figured out 2 work around to Avoid or get pass this pop-up.
1st one: is as KazJaw Pointed out.
Assuming you have another program (eg. Excel, VBScript) which includes sending of mail via Outlook in the procedure.
Instead of using .Send, just .Save the mail.
It will be saved in the Outlook's Draft folder.
Then using below code, send the draft which fires using Outlook Task Reminder.
Option Explicit
Private WithEvents my_reminder As Outlook.Reminders
Private Sub Application_Reminder(ByVal Item As Object)
Dim myitem As TaskItem
If Item.Class = olTask Then 'This works the same as the next line but i prefer it since it automatically provides you the different item classes.
'If TypeName(Item) = "TaskItem" Then
Set my_reminder = Outlook.Reminders
Set myitem = Item
If myitem.Subject = "Send Draft" Then
Call SendMail
End If
End If
End Sub
Private Sub my_reminder_BeforeReminderShow(Cancel As Boolean)
Cancel = True
Set my_reminder = Nothing
End Sub
Above code fires when Task Reminder shows with a subject "Send Draft".
But, we don't want it showing since the whole point is just to call the SendMail procedure.
So we added a procedure that Cancels the display of reminder which is of olTask class or TaskItem Type.
This requires that Outlook is running of course.
You can keep it running 24 hours as i did or, create a VBscript that opens it to be scheduled via Task Scheduler.
2nd one: is to use API to programatically click on Allow button when the security pop-up appears.
Credits to SiddarthRout for the help.
Here is the LINK which will help you programmatically click on the Allow button.
Of course you have to tweak it a bit.
Tried & Tested!
Assuming that you have Outlook Application always running (according to comment below your question) you can do what you need in the following steps:
add a new task in Outlook, set subject to: "run macro YourMacroName" and set time (plus cycles) when your macro should start.
go to VBA Editor, open ThisOutlookSession module and add the following code inside (plus see the comments inside the code):
Private Sub Application_Reminder(ByVal Item As Object)
If TypeName(Item) = "TaskItem" Then
Dim myItem As TaskItem
Set myItem = Item
If myItem.Subject = "run macro YourMacroName" Then
Call YourMacroName '...your macro name here
End If
End If
End Sub
Where will I put the procedure in Outlook, Module or ThisOutlookSession?
Neither. Paste the below code in a Text File and save it as a .VBS file. Then call this VBS file from the Task Scheduler as shown HERE
Dim olApp, olNS, olFolder, olDraft, strfoldername, i
Set olApp = GetObject(, "Outlook.Application")
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(6)
strfoldername = olFolder.Parent
Set olDraft = olNS.Folders(strfoldername).Folders("Drafts")
If olDraft.Items.Count <> 0 Then
For i = olDraft.Items.Count To 1 Step -1
olDraft.Items.Item(i).Send
Next
End If
If you are using Outlook 2007 or newer I have found you can easily eliminate the security pop up you mentioned above when running your script by doing the following:
In Outlook 2007 Trust Center, go to Macro Security - Select "No security Check for macros"
In Outlook 2007 Trust Center, go to Programatic Access - Select "Never warn me abous suspicious activity.
Of course that technically leaves you open to the remote possibility for someone to email you some malicious email script or something of that nature I assume. I trust my company has that managed though and this works for me. I can use VBS scripts in Outlook, Access, Excel to send emails with no security pop up.
Another Option:
If you don't want to do that, another option that has worked well for me prior to this is here:
http://www.dimastr.com/redemption/objects.htm
Basically a dll redirect that does not include the popup. It leaves your other default security in place and you write \ call your VBA for it and send mail without the secutity pop-ups.

I cannot see my VBA macro in 'run a script' selection box

I copied the following code in my oulook VBE, from one of the VBA communities and amended it as per my need.
I can run it using F5 and F8. Now I would like to run this macro whenever I receive an email in folder1.
I tried setting up a rule but I cannot see the macro listed in the 'run a script' selection box.
I have already checked that
macro security setting are correct
macro is in a module not in a class
can you please tell me what is going wrong in the setting.
Public Sub SaveAttachments()
Dim myOlapp As Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.MAPIFolder
Dim yourFolder As Outlook.MAPIFolder
Dim myItem As Outlook.MailItem
Dim myAttachment As Outlook.Attachment
Dim I As Long
Set myOlapp = CreateObject("Outlook.Application")
Set myNameSpace = myOlapp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
Set yourFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myFolder = myFolder.Folders("folder1")
Set yourFolder = yourFolder.Folders("folder2")
For Each myItem In myFolder.Items
If myItem.Attachments.Count <> 0 Then
For Each myAttachment In myItem.Attachments
I = I + 1
myAttachment.SaveAsFile "C:\arthur\test.csv"
Next
End If
myItem.Move yourFolder
Next
End Sub
To be recognized as proper script macro for the Rule Wizard, the macro has to have the expected parameter:
Sub myRuleMacro(item as Outlook.MailItem)
MSDN article (still valid for Outlook 2007/2010/2013/2016)
Related article
Article about enabling run-a-script rules otherwise disabled due to security reasons
(registry key EnableUnsafeClientMailRules).
I had the same issue today on a similar script after Office was upgraded to Version 1803 (Build 9126.2282). Removing the "Pubic" keyword from the sub did the trick. Not sure why, since has been working the other way for years.
I also had to re-add the reg key that had disappeared - EnableUnsafeClientMailRules.