Visual Studio Tools for Office: Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath By Eric Carter, Eric Lippert Publisher: Addison Wesley Professional Pub Date: April 26, 2006 Print ISBN-10: 0-321-41175-7 Print ISBN-13: 978-0-321-41175-4 Pages: 1120 Table of Contents | Index "With the application development community so focused on the Smart Client revolution, a book that covers VSTO from A to Z is both important and necessary This book lives up to big expectations It is thorough, has tons of example code, and covers Office programming in general termstopics that can be foreign to the seasoned NET developer who has focused on ASP.NET applications for years Congratulations to Eric Lippert and Eric Carter for such a valuable work!" Tim Huckaby, CEO, InterKnowlogy, Microsoft regional director "This book covers in a clear and concise way all of the ins and outs of programming with Visual Studio Tools for Office Given the authors' exhaustive experiences with this subject, you can't get a more authoritative description of VSTO than this book!" Paul Vick, technical lead, Visual Basic NET, Microsoft Corporation "Eric and Eric really get it Professional programmers will love the rich power of Visual Studio and NET, along with the ability to tap into Office programmability This book walks you through programming Excel, Word, InfoPath, and Outlook solutions." Vernon W Hui, test lead, Microsoft Corporation "This book is an in-depth, expert, and definitive guide to programming using Visual Studio Tools for Office 2005 It is a must-have book for anyone doing Office development." Siew Moi Khor, programmer/writer, Microsoft Corporation "We don't buy technical books for light reading We buy them as a resource for developing a solution This book is an excellent resource for someone getting started with Smart Client development For example, it is common to hear a comment along the lines of, 'It is easy to manipulate the Task Pane in Office 2003 using VSTO 2005,' but until you see something like the example at the start of Chapter 15, it is hard to put 'easy' into perspective This is a thorough book that covers everything from calling Office applications from your application, to building applications that are Smart Documents It allows the traditional Windows developer to really leverage the power of Office 2003." Bill Sheldon, principal engineer, InterKnowlogy, MVP "Eric Carter and Eric Lippert have been the driving force behind Office development and Visual Studio Tools for Office 2005 The depth of their knowledge and understanding of VSTO and Office is evident in this book Professional developers architecting enterprise solutions using VSTO 2005 and Office System 2003 now have a new weapon in their technical arsenal." Paul Stubbs, program manager, Microsoft Corporation "This book is both a learning tool and a reference book, with a richness of tables containing object model objects and their properties, methods, and events I would recommend it to anyone considering doing Office development using the NET framework, especially people interested in VSTO programming." Rufus Littlefield, software design engineer/tester, Microsoft Corporation Visual Studio Tools for Office is both the first and the definitive book on VSTO 2005 programming, written by the inventors of the technology VSTO is a set of tools that allows professional developers to use the full power of Visual Studio NET and the NET Framework to put code behind Excel 2003, Word 2003, Outlook 2003, and InfoPath 2003 VSTO provides functionality never before available to the Office developer: data binding and data/view separation, design-time views of Excel and Word documents inside Visual Studio, rich support for Windows Forms controls in a document, the ability to create custom Office task panes, server-side programming support against Office, and much more Carter and Lippert cover their subject matter with deft insight into the needs of NET developers learning VSTO This book Explains the architecture of Microsoft Office programming and introduces the object models Teaches the three basic patterns of Office solutions: Office automation executables, Office add-ins, and code behind a document Explores the ways of customizing Excel, Word, Outlook, and InfoPath, and plumbs the depths of programming with their events and object models Introduces the VSTO programming model Teaches how to use Windows Forms in VSTO and how to work with the Actions Pane Delves into VSTO data programming and server data scenarios Explores NET code security and VSTO deployment Advanced material covers working with XML in Word and Excel, developing COM add-ins for Word and Excel, and creating Outlook add-ins with VSTO The complete code samples are available on the book's Web page Visual Studio Tools for Office: Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath By Eric Carter, Eric Lippert Publisher: Addison Wesley Professional Pub Date: April 26, 2006 Print ISBN-10: 0-321-41175-7 Print ISBN-13: 978-0-321-41175-4 Pages: 1120 Table of Contents | Index Copyright Praise for Visual Studio Tools for Office Microsoft NET Development Series Foreword Preface Acknowledgments About the Authors Part I: An Introduction to VSTO Chapter 1 An Introduction to Office Programming Why Office Programming? Office Object Models Properties, Methods, and Events The Office Primary Interop Assemblies (PIAs) Conclusion Chapter 2 Introduction to Office Solutions The Three Basic Patterns of Office Solutions Office Automation Executables Office Add-Ins Code Behind a Document Conclusion Part II: Office Programming in NET Chapter 3 Programming Excel Ways to Customize Excel Programming User-Defined Functions Introduction to the Excel Object Model Conclusion Chapter 4 Working with Excel Events Events in the Excel Object Model Events in Visual Studio 2005 Tools for Office Conclusion Chapter 5 Working with Excel Objects Working with the Application Object Working with the Workbooks Collection Working with the Workbook Object Working with the Worksheets, Charts, and Sheets Collections Working with Document Properties Working with the Windows Collections Working with the Window Object Working with the Names Collection and Name Object Working with the Worksheet Object Working with the Range Object Special Excel Issues Conclusion Chapter 6 Programming Word Ways to Customize Word Programming Research Services Introduction to the Word Object Model Conclusion Chapter 7 Working with Word Events Events in the Word Object Model Events in Visual Studio Tools for Office Conclusion Chapter 8 Working with Word Objects Working with the Application Object Working with the Dialog Object Working with Windows Working with Templates Working with Documents Working with a Document Working with the Range Object Working with Bookmarks Working with Tables Conclusion Chapter 9 Programming Outlook Ways to Customize Outlook Custom Property Pages Introduction to the Outlook Object Model Introduction to the Collaboration Data Objects Conclusion Chapter 10 Working with Outlook Events Events in the Outlook Object Model ApplicationLevel Events Outlook Item Events Other Events Conclusion Chapter 11 Working with Outlook Objects Working with the Application Object Working with the Explorers and Inspectors Collections Working with the Explorer Object Working with the Inspector Object Working with the NameSpace Object Working with the MAPIFolder Object Working with the Items Collection Properties and Methods Common to Outlook Items Outlook Issues Conclusion Chapter 12 Introduction to InfoPath What Is InfoPath? Getting Started Form Security Programming InfoPath Data Source Events Form Events, Properties, and Methods Conclusion Part III: Office Programming in VSTO Chapter 13 The VSTO Programming Model The VSTO Programming Model VSTO Extensions to Word and Excel Objects Dynamic Controls VSTO Extensions to the Word and Excel Object Models Conclusion Chapter 14 Using Windows Forms in VSTO Introduction Adding Windows Forms Controls to Your Document Writing Code Behind a Control The Windows Forms Control Hosting Architecture Properties Merged from OLEObject or OLEControl Adding Controls at Runtime Conclusion Chapter 15 Working with the Actions Pane Introduction to the Actions Pane Working with the ActionsPane Control Conclusion Chapter 16 Working with Smart Tags in VSTO Introduction to Smart Tags Creating Document-Level Smart Tags with VSTO Creating Application-Level Smart Tags Creating an Application-Level Smart Tag Class Library in Visual Studio Creating a Recognizer Class Creating an Action Class Registering and Trusting an Application-Level Smart Tag Class Library Running and Testing the Application-Level Smart Tag Debugging an Application-Level Smart Tag Conclusion Chapter 17 VSTO Data Programming Creating a Data-Bound Customized Spreadsheet with VSTO Creating a Data-Bound Customized Word Document with VSTO Datasets, Adapters, and Sources Another Technique for Creating Data-Bound Spreadsheets Caching Data in the Data Island Advanced ADO.NET Data Binding: Looking Behind the Scenes Binding-Related Extensions to Host Items and Host Controls Conclusion Chapter 18 Server Data Scenarios Populating a Document with Data on the Server Using ServerDocument and ASP.NET An Alternative Approach: Create a Custom Handler A Handy Client-Side ServerDocument Utility The ServerDocument Object Model Conclusion Chapter 19 .NET Code Security Code-Access Security Versus Role-Based Security Code-Access Security in NET Publisher Certificates Trusting the Document Conclusion Chapter 20 Deployment VSTO Prerequisites Deploying to an Intranet Shared Directory or Web Site Local Machine Deployment Without a Deployment Manifest Editing Manifests Creating Setup Packages Conclusion Part IV: Advanced Office Programming Chapter 21 Working with XML in Excel Introduction to Excel's XML Features Introduction to XML Schema Creation in Visual Studio An End-to-End Scenario Advanced XML Features in Excel Excel-Friendly XML Schemas VSTO Support for Excel Schema Mapping Conclusion Chapter 22 Working with XML in Word Introduction to Word's XML Features An End-to-End Scenario: Creating a Schema and Mapping It into a Word Document Exporting the Mapped XML in the Document to an XML Data File Importing an XML Data File into the Mapped Document The XML Options Dialog Box VSTO Support for Word Schema Mapping VSTO Support for the WordML File Format Conclusion Chapter 23 Developing COM AddIns for Word and Excel Introduction to AddIns Scenarios for Using AddIns How a COM AddIn Is Registered Implementing IDTExtensibility2 Writing a COM AddIn Using Visual Studio The Pitfalls of mscoree.dll COM Interop and regasm.exe Shimming: A Solution to the Problems with mscoree.dll Conclusion Chapter 24 Creating Outlook AddIns with VSTO Moving Away from COM AddIns Creating an Outlook AddIn in VSTO Conclusion Bibsrc Bibliography Security Office Programming Data Programming Forms Programming Infrastructure Index Copyright Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals The NET logo is either a registered trademark or trademark of Microsoft Corporation in the United States and/or other countries and is used under license from Microsoft The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests For more information, please contact: U.S Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com For sales outside the U.S., please contact: International Sales international@pearsoned.com Visit us on the Web: www.awprofessional.com Library of Congress Cataloging-in-Publication Data: Carter, Eric Visual studio tools for Office : using Visual Basic 2005 with Excel, Word, Carter, Eric Lippert p cm Includes bibliographical references and index ISBN 0-321-41175-7 (pbk : alk paper) 1 Microsoft Visual BASIC BASIC (Computer program language) Microsoft Office I Lippert, Eric II Title QA76.73.B3C345 2006 005.13'3dc22 2006001141 Copyright © 2006 Pearson Education, Inc All rights reserved Printed in the United States of America This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise For information regarding permissions, write to: Pearson Education, Inc Rights and Contracts Department 75 Arlington Street, Suite 300 Boston, MA 02116 Fax: (617) 848-7047 Text printed in the United States on recycled paper at Courier in Stoughton, Massachusetts First printing, April 2006 Visual Studio 2005 combining schema creation with Excel schema mapping creating XML schema for use in Word creating XML schemas support for XML schemas writing COM add-ins Visual Studio 2005 Tools (VSTO) additional Excel events raised in additional Word events raised in advantages of cached data vs custom properties with Worksheet objects code behind in Excel code behind in Word combining XML mapping with VSTO data binding customization and trust data programming [See Data programming.] deployment runtime prerequisites derived from Windows Forms controls introduction to LoadXml method locale issue and Outlook add-ins and programming model [See Programming model, VSTO.] Smart Tag support support for Word schema mapping support for WordML file format support for XML schema mapping supported/unsupported XML schemas XLA add-ins and XML schema added to VSTO project XMLNode control XMLNodes control VSTO add-ins add-ins compared with COM add-ins creating 2nd improvements to COM add-ins installing manifest update and Outlook shut down problems related to COM add-ins ReleaseCOMObject, avoiding security and Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Z] Wait parameter, in SendKeys method WdWindowState enumeration Web caching, deploying VSTO solutions and Web forms, ASPX Web pages, associating web view with folder in Explorer object Web sites, deploying VSTO solutions to What parameter, Word Range object Which parameter, Word Range object WholeStory method, Word Range object Wiki Window caption, Word Windows accessing window in Excel collection accessing window in Word collection Actions task pane [See Actions task pane.] Activate/Deactivate in Excel 2nd Activate/Deactivate in Word adding buttons/menus to Explorer window arranging in Excel arranging in Word creating in Word display settings in Excel Document object in Word Explorer 2nd Inspector iterating over open windows in Excel iterating over open windows in Word parameters for workbook protection positioning in Excel Window Sizing events in Excel window sizing events in Word windows events in Outlook Windows Forms controls AddControl method adding at runtime adding to Actions task pane adding to documents blurry code behind collection in collection typed as object control state not saved controls added at runtime not saved deleting at runtime on document surface hosting ActiveX control inserting into Excel inserting into Word layout on document/worksheet surface limitations of control hosting model Modal/Modeless modes for adding moving from ActiveX controls to not in toolbox overview programming [See Programming model, VSTO.] properties merged from OLEObject/OLEControl security in Smart Documents summary VSTO controls derived from WithEvents keyword declarative event handling dynamic event handling workbook event handling Word add-in solutions for adding host controls dynamically Bookmark host control COM add-ins in console application for automating data binding in Word documents deploying [See Deploying VSTO solutions.] Document class inserting Word controls into Excel Smart Tags in Smart Tags when Word is Outlook e-mail editor VSTO extensions to Word object models XML in [See XML, in Word.] XMLNode and host control classes Word Basic Word events Close Document CommandBar Document activation E-Postage Mail Merge mouse New/Open Document Print document Save document selection Start-up/Shutdown summary Sync Visual Studio event handlers in VSTO window sizing Word object model and XML Word objects Application object [See Application objects, Word.] Bookmarks Dialog Object Document collection [See Document collections, Word.] Document object [See Document object.] Range object [See Range object, Word.] summary Tables collection templates Windows WordML file format overview of VSTO support for XSLT file transform and Workbook events Activate/Deactivate Calculate Change Before Close Double-Click/Right-Click Follow Hyperlink NewWorkbook Open Before Print raising Resize events Before Save Workbooks accessing document properties accessing in collection adding XML schemas to creating new creating/activating windows embedding Excel in another application host item class iterating over open workbooks in collection mapping schemas to object default properties opening existing/closing all workbooks in collection printing properties that return active/selected objects properties that return collections protecting saving saving in XLA format sending as e-mail Worksheets [See also Sheets.] accessing in collection adding controls to [See Windows Forms controls.] adding to collection built-in WorksheetFunction property Change events ChartObjects in copying/moving custom properties Excel object model and host item class iterating over open sheets in collection layout of controls on worksheet surface lists managing names New Worksheet events OLEObjects properties protecting range of cells [See Range object, Excel.] shapes Wrappers, using PIAs to communicate Write-only property, in object model methods Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Z] XDocument object button events/view switching in forms focus vs selection methods in InfoPath OnContextChange event in XLA add-ins customizing Excel Install/Uninstall events XML caution in using ServerDocument object model to manipulate cache Import/Export events in XML data files manipulating serialized XML directly XML file formats for server data XML-based forms in InfoPath XML Document task pane XML Expansion Packs for customizing Excel for customizing Word XML Map Properties dialog box appending XML data to lists data formatting and layout for XML-mapped lists schema validation settings XML maps defined in Smart Documents using to change Actions pane contents XML Options dialog box Ignore Mixed Content check box overview of save options Show Advanced XML Error Messages Show Placeholder Text for All Empty Elements validation options view options XML schemas adding to VSTO project adding to Word document adding to workbook combining Visual Studio schema creation with Excel schema mapping combining Visual Studio schema creation with Word schema mapping creating for Excel creating for Word mapping schema data type to Excel cell formats mapping to Word documents mapping to workbooks supported/unsupported in Excel validating in Excel VSTO support for Excel schema mapping VSTO support for Word schema mapping XSLT file associated with XML Source task pane XML Structure task pane 2nd XML, in Excel adding schemas to workbooks adding schemas VSTO project appending data to lists combining Visual Studio schema creation with Excel schema mapping combining XML mapping with VSTO data binding creating schemas with Visual Studio 2005 data formatting and layout features importing XML files and refreshing XML data in spreadsheets ListObject controls mapping XML schema data types to Excel cell formats mapping XML schemas to workbooks supported/unsupported schemas validating XML schemas VSTO support for XML schema mapping XMLMappedRange controls XML, in Word adding schema to Word documents attributes combining Visual Studio schema creation with Word schema mapping creating and mapping schema into Word document creating document with mapped XML structure creating schemas with Visual Studio 2005 creating XSLT file data view only exporting mapped XML to XML data file features importing XML data file into mapped document LoadXml for loading XML programmatically manually converting book-order XML file using XSLT file raising XML events saving XML document VSTO support for Word schema mapping VSTO support for WordML file format XML data view only XML Options dialog box 2nd XMLNode control XMLNodes control XSLT file applied automatically to XML file XMLMappedRange controls 2nd XMLNode control dynamic creation of host controls in Word/Excel Word XML 2nd XMLNodes control XSD files .XSF files XSLT file applied automatically to XML file creating manually converting book-order XML file using XSLT file transform of WordML XSN files Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Z] Zones, code groups ... Eric Carter and Eric Lippert, Visual Studio Tools for Office: Using C# with Excel, Word, Outlook, and InfoPath, 0-321-33488-4 Eric Carter and Eric Lippert, Visual Studio Tools for Office: Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath, 0321-41175-7... Advanced material covers working with XML in Word and Excel, developing COM add-ins for Word and Excel, and creating Outlook add-ins with VSTO The complete code samples are available on the book's Web page Visual Studio Tools for Office: Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath. .. "Eric Carter and Eric Lippert have been the driving force behind Office development and Visual Studio Tools for Office 2005 The depth of their knowledge and understanding of VSTO and Office is evident in this book