Contents

Foreword xxi Preface xxv Introduction xxvii Acknowledgments xxxiii About the Authors xxxv

PART I INTRODUCTION TO VSTO 1

Chapter 1 Getting Started with VSTO 3

Topics Covered in This Chapter 3 What Is VSTO? 3 Why Use VSTO Instead of VBA? 5 Features of VSTO 7

Host Items and Host Controls 8 Windows Forms Controls 10 Smart Tags 12 Customizing Task Panes 12 Caching Data in a Document 13 Managed Add-ins 13 Security and Deployment 14 Ribbon Support 15 Enhanced Development Environment 15 Creating VSTO Solutions 15

Creating Office Solutions with VBA 15

Creating Office Solutions with VSTO 16 Project Types 18 How VSTO Integrates with Visual Studio 22 Word and Excel as Designers 22 Code Files in VSTO 23 Summary 25 Review Questions 26

Chapter 2 The Programming Environment 27

Topics Covered in This Chapter 27 Introduction to the Visual Studio IDE 27 Environment Settings 28

Creating a Visual Studio Tools for Office Project 30 Granting Access to the VBA Project System 31 Menus and Toolbars 32 Viewing IDE Windows 35 Code View 35

Exploring Solution Explorer 37 Properties Window 44 Browsing the Object Browser 46 Project Properties 48 Viewing Other Visual Studio Windows 50 Tools for Writing Code 52 Using IntelliSense 53 Edit and Continue 61 Building and Running Code 61 Debugging Your Code 63 Setting Breakpoints 67 Stepping Into, Over, and Out of Code 70 Trying Out the Debugging Tools 73 Using Debugging Windows 76 Locating and Using Help 82 Table of Contents 82 Index 83 Search 84

How Do I 85 Dynamic Help 87 Summary 87 Review Questions 88

Chapter 3 Introduction to Managed Code 89

Topics Covered in This Chapter 89 What Is Managed Code? 89

Introduction to Object-Oriented Programming 91 Understanding Classes and Objects 91 Partial Classes 101 Generic Classes 103 Interfaces 104 Object Orientation 106 Exploring the .NET Framework 108 Assemblies 109 Namespaces 109 Application Domains 111 Common Language Runtime 111 Common Language Specification 114 .NET Framework Class Library 116 VSTO and Managed Code 120 Primary Interop Assemblies 120 Solution Assemblies 124 Running VSTO Solutions 125 Summary 126 Review Questions 127

Chapter 4 Moving from VBA to VSTO and Visual Basic 2005 129

Topics Covered in This Chapter 129 Moving to Visual Basic 2005 129 New Features of Visual Basic 2005 130 The My Objects 131 IntelliSense Code Snippets 135

Statements 136 Operators 138 Language Differences of VBA and Visual Basic 2005 140 Data Types 140 Methods 145

Variable Declaration and Scope 147 Default Properties 148 Enumerations 149 Exception Handling 150 UserForms Versus Windows Forms 152

Comparison of ActiveX Controls to Windows Forms

Controls 154 Changes to Control Functionality 162 Summary 170 Review Questions 170

PART II WORD AND EXCEL 171

Chapter 5 Customizing Word and Excel Task Panes 173

Topics Covered in This Chapter 173 What Is a Task Pane? 173

Customizing the Document Actions Task Pane 175

Using the ActionsPane Object 176 Managing the Actions Pane 177

Managing the Size and Position of the Actions Pane 178 Showing and Hiding the Actions Pane 179 Designing Actions Pane Solutions 180 Creating an Actions Pane Solution 182 Adding an Actions Pane Control 183 Accessing the Actions Pane from a Document 184 Accessing the Document from the Actions Pane 186 Managing Control Order 190 Creating Context-Sensitive Solutions 194 Creating an XML Schema 194

Mapping XML Elements to Your Document 196 Creating Multiple Actions Pane Controls 198 Showing and Hiding Controls on the Actions Pane 199 Adding Functionality to the Actions Pane Controls 201 Summary 204 Review Questions 205

Chapter 6 Customizing Word with VSTO 207

Topics Covered in This Chapter 207 Programming in Word 207

Converting a Recorded VBA Macro to Visual Basic in VSTO 209 Overriding Built-In Commands 213 Word Host Items and Host Controls 218 Adding Host Controls to the Document 220 Accessing a Host Control from an Underlying Interop Object 229 Accessing an Interop Object from a Host Control 232 Data Binding to Host Controls on Documents 233 Binding Data to Controls 233 Caching Data in the Document 239 Programming Against Events 240 Bookmark Control Events 240 XMLNode Control Events 251 XMLNodes Control Events 257 Document Events 257 Special Enhancements to the Bookmark 265

Overlapping Bookmarks 268 Making Word Documents Smart 269 Summary 270 Review Questions 271

Chapter 7 Customizing Excel with VSTO 273

Topics Covered in This Chapter 273 Programming in Excel 273

Learning About Excel's Object Model 277

Converting a Recorded VBA Macro to Visual Basic in VSTO 279 Writing Callbacks from VBA into VSTO Code 281

Excel Host Items and Host Controls 285 Host Items 285 Host Controls 286

Adding Host Controls to the Worksheet 286 Removing Host Controls 296 Adding Host Controls at Run Time 297 Control Persistence 301

Accessing a Host Control from an Underlying Interop Object 303 Accessing an Interop Object from a Host Control 305 Adding Host Items in Excel 306 The Excel Locale Issue 306 Data Binding to Host Controls on Worksheets 308 Creating a Data Source 308

Adding Data-Bound Controls to the Worksheet 311 Working with Related Data 318 Caching Data in the Workbook 319 Programming Against Events 320 NamedRange Control Events 320 ListObject Control Events 324 XMLMappedRange Control Events 331 Chart Control Events 333 Worksheet Events 336 Chartsheet Events 341 Workbook Events 342 Making Excel Smarter 348 Summary 349 Review Questions 350

Chapter 8 Controls in Word and Excel 351

Topics Covered in This Chapter 351 About Controls 351

Adding Controls to Toolbars and Menus 352 Creating Toolbar Buttons 352 Creating Menus and Menu Items 361

Using Windows Forms Controls 365

Available Windows Forms Controls 368 Adding Controls to a Windows Form 378

Creating a User Control 380 Adding Controls to the Task Pane 388 Adding Controls to Excel and Word Documents 390 Windows Forms Controls in Word 391 Windows Forms Controls in Excel 395 Adding Data-Bound Controls 396 Adding Custom Controls to a Document 402 Control Type 404 Adding Controls at Run Time 406

Functionality of Windows Forms Controls on Documents 413 Summary 415 Review Questions 416

Chapter 9 Smart Tags in Word and Excel 417

Topics Covered in This Chapter 417 What Is a Smart Tag? 417

Creating Word and Excel Smart Tags with VSTO 422 Understanding Smart Tag Properties 426 Actions Property 426 Caption Property 427 Expressions Property 427 SmartTagType Property 428 Terms Property 428 Taking Action on a Smart Tag 428

Events Available for an Action 434 Introduction to Regular Expressions 436

Using Regular Expressions to Recognize Phrases 436 Recognizing Patterns of Text 440 Overriding Smart Tag Recognizers 446 Creating the Word Project 447 Running the Code 451 Going Offline 453

Summary 455 Review Questions 455

PART III OUTLOOK AND BEYOND 457

Chapter 10 Creating Add-ins for Outlook with VSTO 459

Topics Covered in This Chapter 459 Application-Level Customizations Using VSTO 459

Differences Between Shared Add-ins and VSTO Add-ins 461 The Outlook Add-in Project Template 463 Converting an Outlook Macro to a VSTO Add-in 467 Creating a Simple Add-in 470 IntelliSense in Visual Studio 472 Outlook Object Model Overview 475 Application Object 475 NameSpace Object 476 Explorer Object 477 Inspector Object 478 Outlook Folders 478 Outlook Items 479 Events 481 Customizing Menus and Toolbars in Outlook 482 Debugging Add-ins 486 Disabled Add-ins 487 Security in Outlook Add-ins Created with VSTO 492

Restricted Methods 493 Summary 496 Review Questions 497

Chapter 11 Security and Deployment 499

Topics Covered in This Chapter 499 VSTO Security Model 499 Code Access Security 500

Deploying Word and Excel Solutions 505 Client Requirements 506

Microsoft PSS VSTO 2005 Client Troubleshooter 507 Deployment Models 509 Application and Deployment Manifests 510 Deployment Tasks 512 Publish Wizard 513 Update and Rollback 523 Microsoft Installer Project 525 Deploying Outlook Solutions 528 Summary 529 Review Questions 529

Chapter 12 Migrating VBA Solutions to VSTO 531

Topics Covered in This Chapter 531 Migration Overview 531 Migration Strategies 532 Simple Migration of a Word VBA Project 534 Advanced Migration of a Word VBA Project 537 Copying the Word Template 538

Removing the VBA Code and the UserForm from the Template 538 Creating a Windows Form 538 Starting the Form 541 Running the Solution 542 Redesigning the Solution 543 Bookmark View Controls 543 Using the Actions Pane 544 VBA and VSTO Interoperability 547

Creating an Interop Excel Application 548 Summary 553 Review Questions 553

Chapter 13 Advanced Topics in VSTO 555

Topics Covered in This Chapter 555 ServerDocument Overview 555

Attaching and Detaching Document-Based Customizations 556 Detaching a Solution from a Document Using

ServerDocument 556 Attaching a Solution to a Document Using ServerDocument 557 Attaching a Solution to a Document Using Custom Document Properties 559 Reading and Writing the Data Cache 560 Cached Data Structure 560 Creating a Test Document with Cached Data 561 Reading and Writing the Document Data Cache 564 Running the Solution 568 Clearing the Data Cache 571 Summary 572 Review Questions 572

Chapter 14 VSTO 2005 SE and the 2007 Microsoft Office System 575

Topics Covered in This Chapter 575 Introduction to VSTO 2005 SE 575 Features in All Releases 576 VSTO 2005 SE Add-in Model 579 Creating Add-ins 584

Creating an Add-in 585 Customizing the Ribbon 585 The Ribbon Item 586 Working with XML 592 Customizing the Built-In Ribbon 596 Adding Other Controls to the Ribbon 602 Quick Access Toolbar 610 Unsupported Ribbon Functionality 612 Ribbons in Outlook 613 Creating Custom Task Panes 622

Creating an Add-in for PowerPoint 622 Opening and Closing the Custom Task Pane 624

Managing Task Panes on Multiple Word Documents 627 Converting a Document-Level Customization to a

VSTO 2005 SE Add-in 630 Displaying Multiple Task Panes 636 Creating a Custom Form Region 638

Creating an Outlook Form Region File 638 Creating a New Form Region 639 Saving the Form Region 639

Creating the VSTO 2005 SE Outlook Add-in Project 640 Embedding the Form Region in Your Project 640 Creating and Registering the Form Region Manifest 642 Creating the Form Region Registration 643 Creating a Form Region Startup Class 643 Hooking Up Form Region Controls 646 Hooking Up the Form Region 647 Running the Project 648 Summary 650 Review Questions 650

Appendix A Creating Code Snippets 651

Importing the Code Snippet 653 Inserting the Code Snippet 654 Using the Code Snippets Manager 655

Appendix B Creating Inspector CommandBars 657 Index 665

This page intentionally left blank

0 0

Post a comment