Office Development with Visual Studio

Develop Office Business Applications using Visual Studio

February, 2010

Posts
  • Office Development with Visual Studio

    Activate Ribbon Tabs in Office 2010 Solutions (Norm Estabrook)

    • 5 Comments

    Office 2010 now lets you activate tabs on the Ribbon. I gave this a try in a Outlook 2010 project and was very pleased with how easy it was to accomplish. Here are a couple of back-of-the-napkin examples that I chicken scratched over lunch.

    From the Ribbon Load event

    This code activates (puts in focus) the built-in Add-Ins tab when the user creates a new mail message. 

     [VB]

    Public Class Ribbon1
    
        Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
            Dim mailItem As Microsoft.Office.Interop.Outlook.MailItem = _
                Globals.ThisAddIn.Application.ActiveInspector().CurrentItem
    
            If Not (mailItem Is Nothing) Then
                If mailItem.EntryID Is Nothing Then
                    Me.RibbonUI.ActivateTabMso("TabAddIns")
                End If
            End If
        End Sub
    .A
    End Class
    

    [C#]

    public partial class Ribbon1
    {
        private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
        {
            Microsoft.Office.Interop.Outlook.MailItem mailItem = 
                Globals.ThisAddIn.Application.ActiveInspector().CurrentItem;
            
            if (mailItem != null)
            {
                if (mailItem.EntryID == null)
                {
                    this.RibbonUI.ActivateTabMso("TabAddIns");
                }
    
            }
          
        }
    }

    From a Form Region

    This code handles a button on an adjoining form region. The code activates a custom tab. Note that the only difference between this example and the previous one is that it uses the Globals class to access a Ribbon collection, and then gets the Ribbon of the active Inspector.

    [VB]

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim RibbonCollection As ThisRibbonCollection = Globals.Ribbons _
        (Globals.ThisAddIn.Application.ActiveInspector())
            RibbonCollection.Ribbon1.RibbonUI.ActivateTab("MyCustomTab")
    
        End Sub
    

    [C#]

    private void button1_Click(object sender, EventArgs e)
    {
        ThisRibbonCollection ribbonCollection =
            Globals.Ribbons [Globals.ThisAddIn.Application.ActiveInspector()];
        ribbonCollection.Ribbon1.RibbonUI.ActivateTab("MyCustomTab");
       
    }

    From Anywhere ..

    So in honor of my favorite childhood author .. I will activate my Ribbon tabs with a mouse. I will activate them in a house.  I will activate them here or there. I will activate them anywhere.

    Wherever you are in your project (task pane, class file, etc.),  just use the Activatexxx methods to activate your tabs. Oh and there is also a cool method named ActivateTabQ. However, I’ll leave that one for you. My lunch break is over :-)

  • Office Development with Visual Studio

    Office Development with Visual Studio Tutorial Series – Part 3 (Beth Massi)

    • 0 Comments

    A couple months ago Robert Green, VSTO MVP, started a series of tutorials on building on Office 2007. Yesterday we published part 3 of his step-by-step tutorials. Thanks Robert!

    In this third part of the series of tutorials on Office Business Applications, learn how to create an Excel 2007 solution using Visual Studio 2008 that generates financing information. This tutorial shows you how to create a custom task pane to display data from a database, bind that data to Excel using ListObjects and NamedRanges, and perform calculations on that data. It also shows you how to easily print the data as a PDF. This step-by-step tutorial also includes full source code in Visual Basic & C#. Check out the tutorial on the VSTO Developer Center:

    Building an Office Business Application Part 3 – Generating Financing Information

    And if you missed the previous tutorials:

    These tutorials are becoming very popular so if you’re just getting started with Office development in Visual Studio, this is a great place to start.

    Enjoy,
    -Beth Massi, Visual Studio Community

  • Office Development with Visual Studio

    Add a Custom Task Pane to Project 2010 (Norm Estabrook)

    • 5 Comments

    Good news! Project 2010 supports custom task panes! Bad News! Getting one to appear in a Project 2010 is not so obvious. Good News! It’s a lot easier than I make it sound.

    In this post, I’ll show you how to add a custom task pane to Project 2010 by using a Project 2010 or Project 2007 project template in Visual Studio.

    The “skinny” on task panes in Microsoft Office Project

    Project 2007 does not support custom task panes. Project 2010 does support custom task panes. The thing that might throw you off is that the Project 2010 project template in Visual Studio does not expose that cool CustomTaskPanes field. You know, that field that enables you to access a CustomTaskPaneCollection object by typing this.CustomTaskPanes or Me.CustomTaskPanes?

    No worries. We can just create a CustomTaskPaneCollection object ourselves. The exact code that you use to accomplish that depends on the .NET Framework version that your project targets.

    For now, add a User Control item to your project. This provides the design surface for your custom task pane. Next, identify which version of the .NET Framework you are targeting. If you created a brand new Project 2010 project, then odds favor that your project targets the .NET Framework 4.  However, it never hurts to check. Here is a helpful topic that shows you how to examine that little piece of info -  How to: Target a Specific .NET Framework Version or Profile.

    After you do all of that, add this code:

    For projects that target the .NET Framework 4:

    [VB]

        Private myUserControl1 As MyUserControl
        Private myCustomTaskPane As Microsoft.Office.Tools.CustomTaskPane
        Private myCustomTaskPaneCollection As Microsoft.Office.Tools.CustomTaskPaneCollection
    
        Private Sub ThisAddIn_Startup() Handles Me.Startup
            myUserControl1 = New MyUserControl
            myCustomTaskPaneCollection = Globals.Factory.CreateCustomTaskPaneCollection _
            (Nothing, Nothing, "CustomTaskPanes", "CustomTaskPanes", Me)
            myCustomTaskPane = myCustomTaskPaneCollection.Add(myUserControl1, "My Task Pane")
            myCustomTaskPane.Visible = True
        End Sub
    
        Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
            myCustomTaskPaneCollection.Dispose()
        End Sub
    

    [C#]

        private MyUserControl myUserControl1;
        private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane;
        private Microsoft.Office.Tools.CustomTaskPaneCollection myCustomTaskPaneCollection;
    
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            myUserControl1 = new MyUserControl();
            myCustomTaskPaneCollection =
                Globals.Factory.CreateCustomTaskPaneCollection
                (null, null, "CustomTaskPanes", "CustomTaskPanes", this);
    
            myCustomTaskPane = myCustomTaskPaneCollection.Add(myUserControl1, "My Task Pane");
            myCustomTaskPane.Visible = true;
    
        }
    
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            myCustomTaskPaneCollection.Dispose();
        }
    For projects that target the .NET Framework 3.5:

    [VB]

        Private myUserControl1 As MyUserControl
        Private myCustomTaskPane As Microsoft.Office.Tools.CustomTaskPane
        Private myCustomTaskPaneCollection As Microsoft.Office.Tools.CustomTaskPaneCollection
    
        Private Sub ThisAddIn_Startup() Handles Me.Startup
            myUserControl1 = New MyUserControl
            myCustomTaskPaneCollection = New Microsoft.Office.Tools.CustomTaskPaneCollection _
                (Me.ItemProvider, Me.HostContext, "MyTaskPane", Me, "MyTaskPane")
            myCustomTaskPane = myCustomTaskPaneCollection.Add(myUserControl1, "My Task Pane")
            myCustomTaskPane.Visible = True
        End Sub
    
        Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
            myCustomTaskPaneCollection.Dispose()
        End Sub
    

    [C#]

        private MyUserControl myUserControl1;
        private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane;
        private Microsoft.Office.Tools.CustomTaskPaneCollection myCustomTaskPaneCollection;
    
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            myUserControl1 = new MyUserControl();
            myCustomTaskPaneCollection = new Microsoft.Office.Tools.CustomTaskPaneCollection
                    (this.ItemProvider, this.HostContext, "MyTaskPane", this, "MyTaskPane");
    
            myCustomTaskPane = myCustomTaskPaneCollection.Add(myUserControl1, "My Task Pane");
            myCustomTaskPane.Visible = true;
        }
    
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            myCustomTaskPaneCollection.Dispose();
        }

    Want to Use a Project 2007 project? No problem. Check this out.

    So what if you are using Visual Studio 2008 and you do not have a Project 2010 project template?  No problem. Project 2010 can host a Project 2007 add-in. All you have to do is configure your project settings to open the Project 2010 executable on start up. The name of that executable file is WINPROJ.exe.

    Right click your project node in Solution Explorer, click Properties, click the the Debug tab, and point away! Here is a screenshot of how I did it.

    image

    Norm E.

     

Page 1 of 1 (3 items)