Hey, Scripting Guy! How Can I Display My Office Outlook Appointments Without Starting Office Outlook?

Hey, Scripting Guy! How Can I Display My Office Outlook Appointments Without Starting Office Outlook?

  • Comments 3
  • Likes

Hey, Scripting Guy! Question

Hey, Scripting Guy! I live and I die by my Office Outlook calendar. But Office Outlook sometimes seems to be rather slow when I am logging on first thing in the morning. When I have to boot up my laptop, log on to the network, start Office Outlook, and wait for five minutes just to see what my appointments are, it seems to be a bit much. I wish there were a script I could run that would display my appointments for the week without my having to log on to Office Outlook. Do you know of such a thing?

- SK

SpacerHey, Scripting Guy! Answer

Hi SK,

Of course I know of such a thing, because I just wrote it. We did not have such a script prior to me receiving your e-mail. Today’s script is really cool because you do not even need to have access to the Exchange server; it works against your offline e-mail store. This is important because recurring appointments may not be reported, but if you have a recurring appointment, you know about it anyway and do not need the extra reminder.

This week we are looking at scripting Office Outlook. Interestingly enough, the Office Outlook object model is not as rich as you might suspect. Certainly Office Word and Office Excel have far more capability for automation than Office Outlook. This having been said, a basic familiarity with Office Outlook automation can lend rich results for the enterprising network administrator, consultant, or power user.

If you are interested in VBScript examples of working with Office Outlook, start with the "Hey, Scripting Guy!" archive, and then move on to the Office Space archive. Finally, you would probably like to see some examples of scripts for automating Office Outlook. You are in luck here because we have dozens of well-written scripts in the Community-Submitted Scripts Center. Read on, this is going to be a cool week. If you need help with Windows PowerShell, you can find download links and getting started information in the Windows PowerShell technology hub.

The GetOutlookAppointments.ps1 script makes a connection to the Outlook Calendar folder, returns a collection of the items found there, examines each one for the start date (occurring during the upcoming week), and prints out the subject, start, duration, and location of the appointment.

GetOutlookAppointments.ps1

[Reflection.Assembly]::LoadWithPartialname("Microsoft.Office.Interop.Outlook") | 
out-null
$i=$j=$null
$dte = Get-Date
$week = $dte.AddDays(7)
$olFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$folder = $namespace.getDefaultFolder($olFolders::olFolderCalendar)
$items = $folder.items
$items |
Foreach-Object {
 Write-Progress -activity "Inspecting $($items.count) calendar appointments"  -Status "Processing ..." `
-PercentComplete ($i/$items.count*100)
 if($_.Start -ge $dte -AND $_.Start -le $week) 
  { 
    $_.Subject 
    $_.Start
    $_.Duration
    $_.Location
  }
 $i++
}

The first thing we need to do is to load the Outlook Interop assembly. This assembly will give us access to olDefaultFolders enumeration to make it easy for us to access the calendar folder. This is seen here:

[Reflection.Assembly]::LoadWithPartialname("Microsoft.Office.Interop.Outlook") | 
out-null

After the assembly is loaded, we initialize a couple of variables. We will use these variables to count the appointments and track our progress through the calendar. It is therefore important that we can be confident of the starting value of the variable. By setting each to null, we know we are starting with a blank slate. This is seen here:

$i=$j=$null

When the variables are initialized, we capture the current date and time by using the Get-Date cmdlet:

$dte = Get-Date

After we have the current date, we add seven days to it by using the AddDays method. The AddDays method comes from the System.DateTime .NET Framework class and was discussed in our Tuesday article this week.

$week = $dte.AddDays(7)

When we have finished adding seven days to the timestamp, we load the OlDefaultFolders enumeration. To do this, we place the name of the enumeration in a string and cast it as a type by using the –as operator. We store the newly created enumeration in the $olFolders variable as shown here:

$olFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]

We now need to create an instance of the Outlook.Application object. The Outlook.Application object is the main object we use when working with Outlook.

As an aside, guess what? The Word.Application object is the main Office Word object. Excel.Application is the main Office Excel object. PowerPoint.Aplication is the main PowerPoint object. Visio.Application is the main Office Visio object. Access.Application is the main Office Access object. And we could continue this pattern for all of the Microsoft Office products. The main entry point into the MSDN documentation for Office development is here. Select the product from the left side, choose developer reference, and then find the object model reference. From there, you will find all the documentation for each of the objects. The enumeration reference is generally at the bottom of the list. All the objects generally appear in alphabetical order.

To create the Outlook.Application object, we use the New-Object cmdlet with the comobject parameter and store the resulting object in the $outlook variable:

$outlook = new-object -comobject outlook.application

Next, we use the GetNameSpace method to return the namespace object. The GetNameSpace method always uses the input value of "MAPI" and it returns a namespace object, which we store in the $namespace variable:

$namespace = $outlook.GetNameSpace("MAPI")

After we have the namespace object, we can use the GetDefaultFolder method to retrieve the Office Outlook calendar. When using the GetDefaultFolder method of the namespace object, we need to give it one of the olDefaultFolders enumeration values. To do this, we use the olDefualtFolders enumeration we stored in the $olFolders variable. The GetDefaultFolder method returns a folder object that points to the folder that was specified when the method was called. This is seen here:

$folder = $namespace.getDefaultFolder($olFolders::olFolderCalendar)

Now we use the items property to obtain a collection of Calendar items. We store the collection of Calendar items in the $items variable:

$items = $folder.items

We take the collection of Calendar items and pipeline them to the ForEach-Object cmdlet. This will allow us to work with the individual items from the collection one at a time. Because we are using pipelining, the items will stream across and we are able to work on them immediately instead of having to load the whole thing in memory. This is seen here:

$items |
Foreach-Object {

We use the Write-Progress cmdlet to inform the user of the progress the script is making as it works through the calendar appointments. We use the same three parameters we have used all week: activity, status, and percentcomplete. The Write-Progress cmdlet will display progress information as the script progresses. The code is seen here:

 Write-Progress -activity "Inspecting $($items.count) calendar appointments"  -Status "Processing ..." `
-PercentComplete ($i/$items.count*100)

The Write-Progress output is seen here:

Image of the Write-Progress output

 

As we are working through the collection of Calendar items, if the start date falls within current seven-day period, we print out the subject, start, duration, and location of the appointment. We could have chosen other information if we had wished. All the properties of the AppointmentItem object are documented on MSDN. Because we are using the ForEach-Object cmdlet and working with items through the pipeline, we reference each property of the AppointmentItem object with the $_ variable, an automatic variable that refers to the current item on the pipeline. This is seen here:

 if($_.Start -ge $dte -AND $_.Start -le $week) 
  { 
    $_.Subject 
    $_.Start
    $_.Duration
    $_.Location
  }
 $i++
}

The result from running the script is seen here:

Image of the Write-Progress output

 

Well, SK, that is it for the GetOutlookAppointments.ps1 script. This also concludes our scripts for Office Outlook Week. Join us tomorrow for Quick-Hits Friday, where we will answer a bunch of questions. Until tomorrow, take care.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • <p>$ Variables I need it for about the issues I was having difficulty in consistency thanks ... &nbsp;&lt;a href=&quot;<a rel="nofollow" target="_new" href="http://www.erpakambalaj.com&quot;&gt;&#199;ember">http://www.erpakambalaj.com&quot;&gt;&#199;ember</a> Makinası&lt;/a&gt;</p>

  • <p>This is very interesting. I have a requirement to do something similar, but I need to interrorgate the Office 365 Exchange server, rather than the local Outlook files, so that I can do it for mutiple users in one hit.</p> <p>Any idea how I might do this?</p> <p>Thanks.</p>

  • <p>Is there a way to tweak this script so it only shows time periods without appointments</p>