net sig presentation on visual studio tools for office – a primer by anthony maimone & ted...

46
.NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Upload: theodora-watts

Post on 25-Dec-2015

220 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

.NET SIG Presentation onVisual Studio Tools for Office – A Primer

by

Anthony Maimone & Ted Ogrean

Page 2: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

About Us

• Anthony– Developer– Product Lead for the Office Business Applications– 5 years experience at Hyland Software

• Ted– Development Manager– Product Lead for SharePoint Integrations– 6 years experience at Hyland Software

Page 3: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

What are we covering?

• The Basics of VSTO• Ribbon Extensibility• Office Application Specifics• Event Handling• Deployment Options• Lessons Learned

Page 4: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

The Basics

• Visual Studio Tools for Office (VSTO) – Create applications that extend Microsoft Office

2003, 2007, and 2010 using managed code.

– Think of as a bridge between VS and Office

– Visual Studio 2003, 2005, 2008, 2010

– VSTO 2003, 2005, 2005 SE, 3.0, 4.0•We are going to stick with 3.0 and 4.0

Page 5: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

The Basics

• VSTO 3.0 Prerequisites– Operating System

• Windows 7, Server 2003, Server 2008, Vista, XP

– .NET Framework 3.5 or 3.5 Client Profile

– Microsoft Office 2007

– Visual Studio 2008

– Office Primary Interop Assemblies (PIAs)

Page 6: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

The Basics

• VSTO 4.0 Prerequisites– Operating System

• Windows XP SP 3 or greater

– .NET Framework 3.5, 4, or 4 Client Profile

– Microsoft Office 2007 or Office 2010

– Visual Studio 2010

– Office Primary Interop Assemblies (PIAs)

Page 7: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

The Basics

• VSTO compatibility and Add-in functionality

VSTO/VSTO runtime version

Developed against Office 2007 Developed against Office 2010

.NET version Available asDocument-level Application-level Document-level Application-level

VSTO 3.0 Word, Excel

Word, Excel, Outlook, PowerPoint, Visio, InfoPath, Project, SharePoint 2007

Workflows

— — .NET 3.5

Built into Visual Studio

2008 Professional and above

VSTO 4.0 Word, ExcelWord, Excel, Outlook,

PowerPoint, Visio, InfoPath, Project

Word, Excel

Word, Excel, Outlook,

PowerPoint, Visio, InfoPath, Project

.NET 3.5 or 4.0

Built into Visual Studio

2010 Professional and above

Page 8: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Demo Solution

Lets see what a complete VSTO add-in based application looks like

Page 9: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

The Basics

• Project Types– Application

• Add-ins

– Document• Excel or Word

Template• Excel Workbook• Word Document

Page 10: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

The Basics

• Project Framework– Really easy to start a new project– New Project

• <Language> | Office | select Add-in from list• Create a new ribbon type class

– Designer or XML

– If XML, follow the Ribbon XML steps to get ribbon to load

– Handle events– Begin building your ribbon and handling

callbacks

Page 11: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

New Project Stub

Create a New VSTO Add-in for Word Demo

Page 12: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

The Basics

• Application Level Add-in– Accessing the Object model of the Office host

application– Customizing UI of the application– Custom task panes (Excel, InfoPath, Outlook,

PowerPoint, Word)– Exposing objects of your Add-in to other Office

solutions • AddInBase.RequestComAddInAutomationService

Page 13: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

The Basics

• Application Level Add-in– Registry Keys

• HKCU\Software\MS\Office\Word• HKLM\Software\MS\Office\Word

– Deployment Manifest• An XML file that describes a

ClickOnce Deployment• Identifies the current ClickOnce

version

– Application Manifest• An XML file that identifies (SxS)

assemblies, bound at runtime

Page 14: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

The Basics

• Document Level Add-in– When the document is opened, the code

extension loads.– Automate the application by using its object

model.– Add controls to the surface of the document.– Manage certain aspects of the document while on

a server without Microsoft Office installed.– Customize the UI of the application for the

specific document• Custom Xml parts

Page 15: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Ribbon Extensibility

Ribbon Designer and Ribbon XML Demo

Page 16: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Ribbon Designer

• Pros– Context

– Simple

– Intuitive

– Drag and Drop

– Designer support

– Similar to Win Forms

– No need to type out xml

– Can export to Ribbon XML

• Cons– Error handling– Encourages code

behind– Limited features

• No Backstage access• Cannot repurpose

built-in commands• Cannot define context

menus

Page 17: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Ribbon XML

• Pros– More Extensibility– Flexible– Faster– More Powerful– More features

• Customize Quick Access Toolbar (QAT)

• Context menus• Backstage (2010)• Repurpose built-in

commands

• Cons– Single callback file

• Per Add-in

– No context– Limited hooks– Tough to Debug– Need to Learn

Ribbon XML

Page 18: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Office Application Specifics

• Object Models– Classes and interfaces provided to interact with

the Office Applications– Each model is contained in the Primary Interop

Assembly or PIAs• More information on these a little later• Microsoft.Office.Interop.Word

– Each applications object model is different– Contains hundreds of objects

Page 19: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Office Application Specifics

• Outlook– Application Object

• The highest level object representing the Outlook App

– Explorer Object• The Main Outlook Window• Displays the content of a folder (mail, tasks, appt.)• Ribbon ID = Microsoft.Outlook.Explorer

– Inspector Object• Represents a window that displays a single item.• 23 total items each with a different Ribbon identifier

– Ex. Mail Compose, Mail Read, Contact, Distribution List, Meeting Request, Task, etc.

Page 20: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Office Application Specifics

• Outlook continued– Context Menus

• Using Ribbon XML, you have the ability to add options to the context menu

– Folder, Store, Mail Item, Appointments, etc.

– The Reading Pane• Allows users to view the selected item in the Explorer• We handle dragging and dropped items to a folder• Can cause problems with events

– The BeforeItemMove event does not get thrown when an item is already open in the Reading Pane.

Page 21: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Office Application Specifics

• Excel– Application

• The Excel application itself, options that are applied currently, and current users’ objects

– Workbook• A single workbook in the Excel application

– Worksheet• A worksheet within a workbook.

– Range• Represents a cell, row, column, selection or block of

cells for manipulation within Excel.

Page 22: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Office Application Specifics

• PowerPoint– Application

• Represents the PowerPoint application and it’s the parent of all other objects

• Control’s the PowerPoint environment– Application Events

» AfterNewPresentation, PresentationClose, etc.

– Presentation• Represents a presentation and all of its content

– Presentation methods and properties

» Save, SaveAs, FullName, Path

Page 23: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Office Application Specifics

• Word– Application

• Represents the Word application and is the parent of all other object

• Controls the Word environment– Application events

» DocumentBeforeSave, DocumentOpen, etc.

– Document• Represents a document and all of its content

– Document methods and properties

» Close, Save, SaveAs, FullName, etc.

Page 24: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Office Application Specifics

• Excel vs. PowerPoint vs. Word – Word is an SDI application– Excel and PowerPoint are MDI applications

• MDI applications and task panes– Mapping of task panes for each document

– Based on Window and Workbook Activate and Deactivate events

– Each object model is different• Be careful not too assume that Excel, PowerPoint, and

Word all behave the same• Ex. Save method looks similar, but parameters and

behavior are different for each application

Page 25: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Event Handling

Office Interop

Unmanaged / COM Interop

Office Automation

VSTO

Managed Code

Hosts Managed Add-ins

Page 26: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Event Handling

• Office Primary Interop Assemblies (PIAs)– Enables managed code to interact with Microsoft

Office application’s COM-based object model• Properties• Events• Add-in load and shut down

– Uses a Runtime Callable Wrapper (RCW) to handle interaction and memory model

– Visual Studio creates a reference when a new Office Project is created

Page 27: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Event Handling

• Office Primary Interop Assemblies (PIAs)– Included with Visual Studio– Must be installed and registered on end-user

computers to run Office solutions targeting .NET 3.5

– When targeting .NET 4.0, PIAs can be embedded into your solution (Embed Interop Types property)

– 32 or 64 bit are available.• AnyCPU for both

– Provides Intellisense and code samples

Page 28: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Event Handling

Unmanaged / COM Interop

Managed Code

RCW INew

IUnknownIDispatch

INew

Unmanaged Managed

Page 29: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Event Handling

• Memory Model– Managed

• Nondeterministic lifetime – Follows .NET Garbage Collection rules

• RCW handles marshaling multiple clients• Keeps an internal reference count

– Unmanaged • Deterministic lifetime

– Follows COM rules

• Single reference count

Page 30: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Event Handling

• Save Event– WorkbookBeforeSave( Excel.Workbook Wb, bool SaveAsUI,

ref bool cancel)– DocumentBeforeSave( Word.Document Doc, ref bool

SaveAsUI, ref bool Cancel)– PresentationBeforeSave( PowerPoint.Presentation Pres, ref

bool Cancel)

– In your best interest to NOT override or cancel the Save event.

• Interfere with macros, other Add-ins, etc.• Auto Save cannot be distinguished from Save

Page 31: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Event Demo

Save Event Demo

Page 32: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Deployment

• MSI– Generic Windows installer– Must check prerequisites VSTO and .NET

framework prior to installing Add-in

Page 33: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Deployment

• ClickOnce– Create self-updating Windows-based applications– Installed and run with minimal user interaction

from web, network share, or media (CD)– Major issues overcome

• Updating application, impact on the user’s machine, security permissions (CAS)

– Must install prerequisites through a separate installer

• Created a separate MSI to check for VSTO installation

Page 34: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Deployment

• VSTO deployments are generally only for the Current User– Can be made for All Users on a given machine

• When installing, must use the HKLM registry path– HKLM\Software\MS\Office\Word\Addins

1.You need administrative privilege on the machine in order to install an All User add-in.

2.An all user add-in cannot be deployed through ClickOnce and must be deployed through a Windows Installer MSI.

– manifest = “C:\Program Files\MyVSTOAddIn\MyVSTOAddIn.vsto|vstolocal“

3.Trusting the Add-In for all users

Page 35: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Lessons Learned

• Create separate add-ins for each Office application– This is the general idea behind VSTO

• Be very careful of start-up and load times• Lazy initialization is your friend

• If you let a COM object leave scope, do not release it. Better yet, don’t let COM objects leave local scope.

• Do not release a COM object if you are not the owner.

• When using a foreach loop to iterate over COM objects, release each item properly

Page 36: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Lessons Learned

• Be explicit with your code. Each “.” may create another reference that needs to be released– Example (VB)

• var app = new Application();• app.Workbooks.Add();• app.Visible = true;• app.Quit()

– app.Workbooks creates a RCW reference that we cannot release.

Page 37: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Lessons Learned

• Office PIAs do not always clean up event interfaces– Can lead to Office application not closing properly

• Major issue with Terminal Services or Citrix

– Created our own Interop project for each version• Referenced each Office Interop

– Excel, PowerPoint, Word

• Extended each set of events and events provider– Properly release each COM Object passed as a parameter

– Fixed our issues with event interfaces not properly being cleaned.

Page 38: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Lessons Learned

• Converting Ribbon Designer to Ribbon XML– Follow the rules in the new ribbon file.– After copying over your handlers or click events

• Change from private to public• Change parameters to proper Ribbon XML

– Generally to IRibbonControl control

Page 39: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Lessons Learned

• VSTO_SUPPRESSDISPLAYALERTS– Environment variable, set to 0 (zero)– Displays VSTO errors as a message box

• Extremely useful when debugging.

• VSTO_LOGALERTS– Environment variable, set to 1 (one)– Writes the errors to a log file

• Exists in the folder with the deployment manifest or %Temp%

– Add-in name.vsto.log

Page 40: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Lessons Learned

• After upgrading from VSTO 4.0 to 4.0 SP1, our add-ins are failing to load?– If you generate your own installer files, you

specify how to write the manifest key to the registry.

• manifest = “C:\Program Files\MyVSTOAddIn\MyVSTOAddIn.vsto|vstolocal“

– In SP1, that path changed to be• “file:///C:Program Files\...”

Page 41: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Links

• VSTO 3.0 - http://www.microsoft.com/download/en/details.aspx?id=23656

• VSTO 4.0 http://www.microsoft.com/download/en/details.aspx?id=20479

• Office Development in VSTO - http://msdn.microsoft.com/en-us/office/hh133430

• Office Development in Visual Studio 2010 - http://msdn.microsoft.com/en-us/library/d2tx7z6d.aspx

• Features Available by Office Application and Project Type - http://msdn.microsoft.com/en-us/library/aa942839(v=VS.100).aspx –

• Outlook 2010 User Interface - http://msdn.microsoft.com/en-us/library/ee692172.aspx#OfficeOLExtendingUI_Explorer

• Custom Task Panes Overview - http://msdn.microsoft.com/en-us/library/aa942864.aspx -

Page 42: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Links

• Word Object Model Overview - http://msdn.microsoft.com/en-us/library/kw65a0we.aspx

• Excel Object Model Overview - http://msdn.microsoft.com/en-us/library/wss56bz7.aspx

• PowerPoint 2010 Developer Reference - http://msdn.microsoft.com/en-us/library/ff746846.aspx

• Outlook 2010 Object Model - http://msdn.microsoft.com/en-us/library/ms268893.aspx

• ClickOnce Security and Deployment - http://msdn.microsoft.com/en-us/library/t71a733d.aspx

• Walkthrough: Creating your first Word Add-in http://msdn.microsoft.com/en-us/library/cc442946.aspx

Page 43: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Links

• Office Fluent User Interface Control Identifiers - http://www.microsoft.com/download/en/details.aspx?id=6627

• RequestCOMAddinAutomationService - http://msdn.microsoft.com/en-us/library/microsoft.office.tools.addinbase.requestcomaddinautomationservice.aspx

• Deploying your VSTO add-in to All Users - http://blogs.msdn.com/b/vsto/archive/2010/03/08/deploying-your-vsto-add-ins-to-all-users-saurabh-bhatia.aspx

• VSTO SP1 Fastpath issue - http://blogs.msdn.com/b/vsod/archive/2011/06/14/vsto-4-0-sp1-will-cause-a-vsto-addin-to-not-find-its-config-file.aspx

Page 44: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Links

• Customizing the 2007 Office Fluent Ribbon for Developers 1/3 - http://msdn.microsoft.com/en-us/library/aa338202(v=office.12).aspx

• Deploying a Customized Ribbon and Quick Access Toolbar - http://msdn.microsoft.com/en-us/library/ee704589.aspx

• Customizing Context Menus for Office 2010 - http://msdn.microsoft.com/en-us/library/ee691832.aspx

• Customizing the Office 2010 Backstage View - http://msdn.microsoft.com/en-us/library/ee815851.aspx

Page 45: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

Questions?

Page 46: NET SIG Presentation on Visual Studio Tools for Office – A Primer by Anthony Maimone & Ted Ogrean

time to make a difference.