Welcome to Office Zealot Sign in | Join | Help

Robert Green

Putting the Visual Studio in Office Development
It depends on what the definition of "this" is

I was perusing the VSTO MSDN forum a couple days ago and I saw Add a command button to the send email screen. This person (Bear23 to be exact) wanted to add a button to an Outlook 2003 Inspector. Well, not only do I know how to do this, but I have code I know works. I have Office 2007 and VSTO 2005 SE on both of my main computers, so I fire up my VPC with Office 2003 and VSTO. I create a new VSTO Outlook project, add my existing code, confirm it works and post the code.

Bear23 writes back that my code doesn't work and yields an error on the following code:

openInspectors = this.Inspectors;

The error is ThisAddIn does not contain a definition for 'Inspectors'

The code works on my machine, but not Bear23's. So yesterday ends with both of us confused.

This morning, I notice that in the post, Bear23 said that he or she is using VSTO 2005 SE. I have VSTO 2005 on my VPC. So I install VSTO 2005 SE and what do you know, my code now yields the same error.

In VSTO 2005, you have one option for creating Outlook 2003 Add-ins. You can select Office as the project type and Outlook Add-in as the template.

 VSTO 2005 Outlook 2003 Add-in

 If you do this (and I did the first time), your project has a ThisApplication class and the code above works just fine. "this" refers to Outlook and Outlook has a collection of Inspectors.

If you install VSTO 2005 SE over VSTO 2005 you have two options for creating Outlook 2003 Add-ins. You can select 2003 Add-ins as the project type and Outlook Add-in as the template. If you only have VSTO 2005 SE, which is Bear23's situation, you only get this option.

VSTO 2005 SE Outlook 2003 Add-in

If you choose this template (which Bear23 did), your project has a ThisAddIn class and the code above does not work. "this" refers to the add-in, not to Outlook. "this.Application" refers to Outlook and you use the following code:

openInspectors = this.Application.Inspectors;

So there are three different scenarios, and any given VSTO user will fall into one of them. Next time I give somebody code or use someone else's code, I should check to make sure we are in synch on which type of Add-in we are creating. You should too.

VSTO Outlook Add-In demo

Here is the demo script for the VSTO 2005 SE Outlook Add-In demo I did today in the Webcast. The Add-In lets you select a contact from the AdventureWorks database and then populate a mail message with boilerplate and information from the database.

Hope you enjoy this. Comments and questions welcome.

Create the project

1.       Create a new Outlook 2007 Add-in project named ContactsAddIn.

Create the data source

1.       Add a new data source, connecting to AdventureWorks.

2.       Select the Contact table.

3.       Include FirstName, LastName, EmailAddress and Phone.

4.       Open the DataSet Designer.

5.       Configure the TableAdapter.

6.       Add the following to the query:

WHERE LastName LIKE @lastName + '%'

ORDER BY LastName, FirstName

7.       Click Advanced Options.

8.       Make sure Generate statements is unchecked.

9.       Change the method names to FillByContact and GetDataByContact

10.   Finish.

11.   Close the dataset.

Create the user control

1.       Add a new user control named Contacts.

2.       Drag the Contact table from the Data Sources Window onto the user control.

3.       Delete the navigation control.

4.       Uncheck Adding, Editing and Deleting for the grid.

5.       Edit Columns.

6.       Remove EmailAddress and Phone.

7.       Swap the order of LastName and FirstName.

8.       Add a space to the headers for LastName and FirstName.

9.       Set grid’s RowHeadersVisible property to False.

10.   Set SelectionMode to FullRowSelect.

11.   Set ScrollBars to Vertical.

12.   Make the grid 215 wide.

13.   Make LastNameToolStripTextBox 50 wide.

14.   Change the Text property of FillByContactToolStripButton to Search.

15.   Add a button named PopulateEmailButton to the control and center it.

16.   Make the user control 225 wide.

17.   Double click the FillByContactToolStripButton button and see the code that runs.

Customize the Ribbon Xml

1.       Add a new Ribbon support item to the project.

2.       Uncomment the ThisAddin code.

3.       Open Ribbon1.xml.

4.       Replace the xml with the following:

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"

onLoad="OnLoad">

  <ribbon>

    <tabs>

      <tab id="VSTOAddIns" label="VSTO Add-Ins">

        <group id="Group1" label="Helpers" visible ="1">

          <button id="Contacts" label="Contact info"

            onAction="ContactsButtonClick"

            imageMso="MailSelectNames"/>

        </group>

      </tab>

    </tabs>

  </ribbon>

</customUI>

Add Ribbon Xml as project resource

1.       Open the Resource tab of the Project designer.

2.       Drag Ribbon1.xml onto the designer.

3.       Close and save.

Add code to the Ribbon class

1.       Return to Ribbon1.vb.

2.       Add the following code to declare a variable that records if the Contacts custom task pane is visible.

Private contactsControlVisible As Boolean

3.       Replace the code in GetCustomUI with the following code.

If ribbonID = "Microsoft.Outlook.Mail.Compose" Then

  Return My.Resources.Ribbon1

Else

  Return ""

End If

4.       Replace OnToggleButton1 with the following code.

Public Sub ContactsButtonClick( _

  ByVal control As Office.IRibbonControl)

 

  If Not contactsControlVisible Then

    ' Display the Contacts task pane

    Globals.ThisAddIn.AddContactsTaskPane()

  Else

    ' Hide the Contacts task pane

    Globals.ThisAddIn.RemoveContactsTaskPane()

  End If

  contactsControlVisible = Not contactsControlVisible

End Sub

Add code to display and hide the custom task pane

1.       Open ThisAddIn.vb.

2.       Insert the following code.

Private ctpContacts As _

  Microsoft.Office.Tools.CustomTaskPane

 

Public Sub AddContactsTaskPane()

  ctpContacts = Me.CustomTaskPanes.Add( _

    New Contacts, "Contacts")

  ctpContacts.Visible = True

End Sub

 

Public Sub RemoveContactsTaskPane()

  Me.CustomTaskPanes.Remove(ctpContacts)

End Sub

Run the application

1.       Save and build.

2.       Run.

3.       Create a new mail message.

4.       Click the VSTO Add-Ins tab.

5.       Click Contact info.

6.       Enter hernan and click Search. You should see multiple Hernandez's in the grid.

7.       Close Outlook.

Add code to populate the mail message

1.       Return to the user control.

2.       Double click Populate email.

3.       Add the following declarations.

Private contact As AdventureWorksDataSet.ContactRow

Private thisInspector As Outlook.Inspector

Private thisMessage As Outlook.MailItem

Private buildString As System.Text.StringBuilder

4.       Add the following code to the PopulateEmailButton_Click method.

thisInspector = _

  Globals.ThisAddIn.Application.ActiveInspector

thisMessage = CType(thisInspector.CurrentItem, _

  Outlook.MailItem)

 

contact = CType(CType( _

  Me.ContactBindingSource.Current, _

  DataRowView).Row, _

  AdventureWorksDataSet.ContactRow)

 

thisMessage.Subject = "Thanks for your interest"

thisMessage.To = contact.EmailAddress

 

buildString = New System.Text.StringBuilder

buildString.AppendLine(String.Format("Dear {0} {1}", _

  contact.FirstName, contact.LastName))

buildString.AppendLine()

buildString.AppendLine( _

  "Thank you for contacting MCW Technologies. " & _

  "We are looking forward to meeting with you and " & _

  "discussing your software development needs. ")

buildString.AppendLine()

buildString.AppendLine(String.Format( _

  "I will be calling you at {0} within 24 hours " & _

  "to set up an appointment.", contact.Phone))

buildString.AppendLine()

buildString.AppendLine("Thank you,")

buildString.AppendLine()

buildString.AppendLine("Robert Green")

buildString.AppendLine("Sr Consultant")

buildString.AppendLine("MCW Technologies")

 

thisMessage.Body = buildString.ToString

Run the application

1.       Save and build.

2.       Run.

3.       Create a new mail message.

4.       Click VSTO Add-Ins tab.

5.       Click Contact info.

6.       Enter hernan and click Search.

7.       Select a Hernandez and click Populate Email.

8.       Send. (The email address is fake, so you are not really sending this to anyone.)

9.       Open a new mail message. The custom task pane is still there.

10.   Click VSTO Add-Ins tab.

11.   Click Contact info.

12.   Close Outlook.

Close the task pane when the mail is sent.

1.       Return to ThisAddIn.

2.       Replace ThisAddIn_Startup with the following code.

Private mailItem As Outlook.MailItem

 

Private Sub ThisAddIn_Startup(ByVal sender As Object, _

  ByVal e As System.EventArgs) Handles Me.Startup

 

  AddHandler Application.ItemSend, AddressOf MailSent

End Sub

 

Private Sub MailSent(ByVal item As Object, _

  ByRef cancel As Boolean)

 

  RemoveContactsTaskPane()

End Sub

 

Run the application

1.       Save and build.

2.       Run.

3.       Create a new mail message.

4.       Click VSTO Add-Ins tab.

5.       Click Contact info.

6.       Enter hernan and click Search.

7.       Select a Hernandez and click Populate Email.

8.       Send.

9.       Open a new mail message. The custom task pane is closed.

10.   Close Outlook.

 

Outlook 2007 performance update

Have you seen this? Have you downloaded it? Have you found Outlook 2007 to perform better?

According to Will Kennedy, it improves Outlook 2007 performance when "reading, moving, deleting and downloading e-mail messages for users who have large mailbox files". I do all those things, as do you no doubt. He also says that, while the update was designed for people with large mailbox files, "you may see an improvement in performance... even if your mailbox isn't 'large'".

Well, I downloaded this and I have to say, I have seen a measurable improvement in performance, but only because 0 is lieterally measurable. I wonder if my 372 MB pst just isn't considered large.

Of course, I represent a sample size of 1. What have you seen?

Straight from the Experts

I am the next expert in the VSTO: Straight from the Experts Webcast series with Mike Hernandez. Check it out here this Thursday, May 3 at 12:00 PM PST.

I will show a very cool Outlook Add-In demo built with VSTO 2005 SE. Data bound controls, custom task panes, Ribbon customization, automating repetitive tasks. It's all in this demo.

If you missed Mike and Ken Getz, you can view it on demand here.

Welcome to my Office Development blog

Should I start with a few words about me? Okay. I am Robert Green, a Sr Consultant with MCW Technologies. I have been with MCW for about 2 years. I have spent much of that time writing courseware for AppDev, doing some projects for the VSTO team and speaking at conferences and user groups. Prior to that I was at Microsoft for 8+ years, most of it in Developer Tools Marketing. I was the first Product Manager for VSTO and have been using it for longer than it has been shipping.

For too long, Office Development has existed in a parallel universe to Visual Studio development. But we know that Office development is not just VBA macro coding. It is using the .NET Framework and VB or C# to build solutions where the user interface is Office applications. It is about combining the skills you already have with the client applications users are familiar with. It is about having your cake and eating it too and getting paid for it.

This blog is for Visual Studio developers who want to build Office solutions (people like me) and also for Office solutions developers who want to use Visual Studio. I'm glad to be here and I hope you are glad to have me here.