This week's entry in my Lazy Programmer series is a variation of last week's macro that sends an email when Appointment reminders fire.
This VBA sample opens a web page in Internet Explorer after a reminder fires, using the URL entered into a Task's Subject field. To avoid firing with every reminder, it looks at the message class and exits if not a Task reminder. It then checks the Category field. If the Category is "Open Page", the macro opens Internet Explorer and navigates to the URL in the Subject field.
Outlook 2003 users can also use the method described in Scheduling a Recurring Message.
This code could easily be changed to run when an appointment reminder fires, such as to open an online meeting url.
Open a webpage using a reminder
Set macro security to off or to 'always ask'. You may need to restart Outlook for the lower macro security settings to kick in.
To use:
- Open the VBA editor using Alt+F11
- Expand the VBA project
- Double click on ThisOutlookSession to open it
- Paste the code into ThisOutlookSession
Return to Outlook and create your task:
- Create a new task, entering a web address in the Subject field.
- Create a category called "Open Page" and assign it to the task.
- Set the reminder for the desired date and time then save and close.
Private Sub Application_Reminder(ByVal Item As Object) Dim strURL As String Dim oApp As Object Set oApp = CreateObject("InternetExplorer.Application") If Item.MessageClass <> "IPM.Task" Then Exit Sub End If If Item.Categories <> "Open Page" Then Exit Sub End If strURL = Item.Subject oApp.navigate (strURL) oApp.Visible = True 'wait for page to load before passing the web URL Do While oApp.Busy DoEvents Loop End Sub
Open other files types
To open other file types, you need to reference the correct application and change the code that opens the file to the method used by the other application.
For example, to open an Excel workbook, you'll need to create the Excel.Application instead of InternetExplorer.Application, then open the workbook. Remove the DoEvents block, since we don't need to wait for Excel to open.
Set oApp = CreateObject("Excel.Application") oApp.Workbooks.Open FileName:=strURL oApp.Visible = True
Opening some files, such as text files in Notepad, will use a Shell command. Remove the oApp lines and use just the Shell command.
Shell ("Notepad.exe " & strURL), vbNormalFocus
Batch (*.bat) can be opened using just the file path:
Shell (strURL), vbNormalFocus
Notes
If you create a recurring task, remember to mark the task complete, instead of dismissing it.
To use this with an appointment, change If Item.MessageClass <> "IPM.Task" Then to If Item.MessageClass <> "IPM.Appointment" Then. If you want a neater entry on your calendar, put the url in the Location field and replace strURL = Item.Subject with strURL = Item.Location
If you want to use tasks to open different items, such as a web page using one task and an Excel workbook using another, you need to wrap the code in If statements:
If Item.Categories = "Open Web" Then 'do whatever End If If Item.Categories = "Open Excel" Then 'do whatever End If
Hello,
This is a very awesome feature. Had a question however, I set this up and it worked great the first day but i have not been able to get it to work since. Originally i did not "Mark as complete" and i dismissed it, but i have completely deleted that task occurrence and created a new one but cannot get it to work again. I setup my Category as Open Page and inserted my file path on the subject line. Below is the coding i have in Outlook VBA under ThisOutlookSession. Also i do have this setup to reoccur every Mon - Thursday with a reminder time @ 4:45 PM
-------------------------------------------------------------------
Private Sub Application_Reminder(ByVal Item As Object)
Dim strURL As String
Dim oApp As Object
Dim oIE As Object
Set oIE = CreateObject("Excel.Application")
oIE.Workbooks.Open FileName:="C:\Users\user's login\Desktop\filename.xlsm"
oIE.Visible = True
If Item.MessageClass "IPM.Task" Then
Exit Sub
End If
If Item.Categories "Open Page" Then
Exit Sub
End If
strURL = Item.Subject
End Sub
-------------------------------------------------------------------
Thanks in advance for your help
Did you confirm the macro security is set to low (or sign the macro using SelfCert)? That is the usual cause of a macro working one day and not the next.
Hello Diane, I came to this page after having received today's (May 8) Outlook Daily Tips. In the newsletter you say that "with a little tweaking, you could use it to open almost any file type". I would like to use this to open (a) specific Excel file(s) triggered by a task reminder. What would be the code for doing this, assuming the file path is something like M:/My Data/My Dropbox/Project A/filename01.xlsx (or xslm)? I uinderstand this is an Outlook tips websiter and newsletter, but if you could point me in the right direction, it would be awesome. Bruno
I knew when i wrote that that I'd be asked for the code fairly quickly. :) You need to create the Excel object then open the path.
Short version:
change the application:
Set oIE = CreateObject("Excel.Application")
hard-coded path - if you need to change the file name, use the subject field string.
oIE.Workbooks.Open FileName:="C:\calendar-series.xls"
oIE.Visible = True
remove the doevents block.