Tải bản đầy đủ

Visual studio tools for office 2007

www.it-ebooks.info


Praise for Visual Studio Tools for Office 2007
“Visual Studio Tools for Office has always been one of my favorite technologies
to come out of Microsoft. There are millions of people who use Office applications all day, every day; with VSTO, you can create applications for them. Eric
Carter and Eric Lippert helped create VSTO, so they know as much about it as
anybody, making this book a must-have. After reading it, you’ll know everything needed to begin building solutions that take advantage of the .NET
Framework features, in the UI your users are familiar with.”
—Robert Green, senior consultant, MCW Technologies
“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 terms—topics 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
“Eric Carter and Eric Lippert 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, and Outlook solutions.”
—Vernon W. Hui, test lead, 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
“This book will help Office .NET Developers optimize their work. It goes
beyond providing an introduction to VSTO and the object models of Word,
Excel, and Outlook. The overview of other technologies available for interacting with Office assist in analyzing how to best approach any Office project. In
addition, the authors’ insights into the design of this RAD tool make it possible
to get the most out of VSTO applications.”
—Cindy Meister, Microsoft MVP for VSTO,
author of Word Programmierung, Das Handbuch

www.it-ebooks.info


“This book is an in-depth, expert, and definitive guide to programming using
Visual Studio Tools for Office 2007. 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
2007 using VSTO 2008,’ but until you see something like the example at the
start of Chapter 14, 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 2007.”
—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. The depth of their knowledge and
understanding of VSTO and Office is evident in this book. Professional developers architecting enterprise solutions using VSTO 2008 and Office system
2007 now have a new weapon in their technical arsenal.”
—Paul Stubbs, program manager, Microsoft Corporation
“This book, also known as ‘The Bible of VSTO,’ has been rewritten for Office
2007 and I was delighted to read the sections on new VSTO features that were
added in Visual Studio 2008. It explains how the VSTO team hid the plumbing
and cumbersome coding tasks to allow you to be more productive and to just
create excellent business applications. New or experienced in Office development, you will want to add this book to your library!”
—Maarten van Stam, Microsoft MVP, Visual Developer, VSTO,


http://blogs.officezealot.com/maarten
“This book covers all of the ins and outs of programming with Visual Studio
Tools for Office in a clear and concise way. Given the authors’ exhaustive experiences with this subject, you can’t get a more authoritative description of
VSTO than this book!”
—Paul Vick, principal architect, Microsoft Corporation

www.it-ebooks.info


Visual Studio Tools
for Office 2007

www.it-ebooks.info


Microsoft .NET Development Series
John Montgomery, Series Advisor • Don Box, Series Advisor • Brad Abrams, Series Advisor
The award-winning Microsoft .NET Development Series was established in 2002 to provide professional
developers with the most comprehensive and practical coverage of the latest .NET technologies. It is
supported and developed by the leaders and experts of Microsoft development technologies, including
Microsoft architects, MVPs, and leading industry luminaries. Books in this series provide a core resource of
information and understanding every developer needs to write effective applications.

Titles in the Series
Brad Abrams, .NET Framework Standard Library
Annotated Reference Volume 1: Base Class Library and
Extended Numerics Library, 978-0-321-15489-7
Brad Abrams and Tamara Abrams, .NET Framework
Standard Library Annotated Reference, Volume 2:
Networking Library, Reflection Library, and XML Library,
978-0-321-19445-9
Chris Anderson, Essential Windows Presentation Foundation
(WPF), 978-0-321-37447-9

Mark Michaelis, Essential C# 3.0: For .NET Framework 3.5,
978-0-321-53392-0
James S. Miller and Susann Ragsdale,
The Common Language Infrastructure Annotated Standard,
978-0-321-15493-4
Christian Nagel, Enterprise Services with the .NET
Framework: Developing Distributed Business Solutions
with .NET Enterprise Services, 978-0-321-24673-8

Bob Beauchemin and Dan Sullivan, A Developer’s Guide to
SQL Server 2005, 978-0-321-38218-4

Brian Noyes, Data Binding with Windows Forms 2.0:
Programming Smart Client Data Applications with .NET,
978-0-321-26892-1

Adam Calderon, Joel Rumerman, Advanced ASP.NET
AJAX Server Controls: For .NET Framework 3.5,
978-0-321-51444-8

Brian Noyes, Smart Client Deployment with ClickOnce:
Deploying Windows Forms Applications with ClickOnce,
978-0-321-19769-6

Charlie Calvert and Dinesh Kulkarni, Essential LINQ,
978-0-321-56416-0

Fritz Onion with Keith Brown, Essential ASP.NET 2.0,
978-0-321-23770-5

Eric Carter and Eric Lippert, Visual Studio Tools for
Office 2007: VSTO for Excel, Word, and Outlook,
978-0-321-33488-6

Steve Resnick, Richard Crane, Chris Bowen, Essential
Windows Communication Foundation: For .NET Framework
3.5, 978-0-321-44006-8

Eric Carter and Eric Lippert, Visual Studio Tools for Office:
Using C# with Excel, Word, Outlook, and InfoPath,
978-0-321-33488-6
Eric Carter and Eric Lippert, Visual Studio Tools for
Office: Using Visual Basic 2005 with Excel, Word, Outlook,
and InfoPath, 978-0-321-41175-4
Steve Cook, Gareth Jones, Stuart Kent, Alan Cameron
Wills, Domain-Specific Development with Visual Studio
DSL Tools, 978-0-321-39820-8
Krzysztof Cwalina and Brad Abrams, Framework Design
Guidelines: Conventions, Idioms, and Patterns for Reusable
.NET Libraries, Second Edition, 978-0-321-54561-9

Scott Roberts and Hagen Green, Designing Forms
for Microsoft Office InfoPath and Forms Services 2007,
978-0-321-41059-7
Neil Roodyn, eXtreme .NET: Introducing eXtreme
Programming Techniques to .NET Developers,
978-0-321-30363-9
Chris Sells and Michael Weinhardt, Windows Forms 2.0
Programming, 978-0-321-26796-2
Dharma Shukla and Bob Schmidt, Essential Windows
Workflow Foundation, 978-0-321-39983-0

Joe Duffy, Concurrent Programming on Windows,
978-0-321-43482-1

Guy Smith-Ferrier, .NET Internationalization:
The Developer’s Guide to Building Global Windows
and Web Applications, 978-0-321-34138-9

Sam Guckenheimer and Juan J. Perez, Software
Engineering with Microsoft Visual Studio Team System,
978-0-321-27872-2

Will Stott and James Newkirk, Visual Studio Team
System: Better Software Development for Agile Teams,
978-0-321-41850-0

Anders Hejlsberg, Mads Torgersen, Scott Wiltamuth,
Peter Golde, The C# Programming Language, Third Edition,
978-0-321-56299-9

Paul Yao and David Durant, .NET Compact Framework
Programming with C#, 978-0-321-17403-1

Alex Homer and Dave Sussman, ASP.NET 2.0 Illustrated,
978-0-321-41834-0

Paul Yao and David Durant, .NET Compact Framework
Programming with Visual Basic .NET, 978-0-321-17404-8

Joe Kaplan and Ryan Dunn, The .NET Developer’s Guide to
Directory Services Programming, 978-0-321-35017-6

For more information go to
informit.com/msdotnetseries/
www.it-ebooks.info
Download from www.wowebook.com


Visual Studio Tools
for Office 2007
VSTO for Excel, Word, and Outlook

Eric Carter
Eric Lippert

Upper Saddle River, NJ • Boston • Indianapolis • San Francisco
New York • Toronto • Montreal • London • Munich • Paris • Madrid
Capetown • Sydney • Tokyo • Singapore • Mexico City

www.it-ebooks.info
Download from www.wowebook.com


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 a 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 United States, please contact:
International Sales
international@pearson.com
Visit us on the Web: informit.com/aw
Library of Congress Cataloging-in-Publication Data
Carter, Eric.
Visual Studio tools for Office 2007 : VSTO for Excel, Word, and
Outlook / Eric Carter, Eric Lippert. — 2nd ed.
p. cm.
Includes bibliographical references and index.
ISBN 978-0-321-53321-0 (pbk. : alk. paper)
1. Microsoft Visual BASIC. 2. BASIC (Computer program language) 3.
Microsoft Visual studio. 4. Microsoft Office. I. Lippert, Eric. II. Title.
QA76.73.B3C3452 2009
005.13'3—dc22
Copyright © 2009 Pearson Education, Inc.

2009000638

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
501 Boylston Street, Suite 900
Boston, MA 02116
Fax (617) 671-3447
ISBN-13: 978-0-321-53321-0
ISBN-10:
0-321-53321-6
Text printed in the United States on recycled paper at Courier in Stoughton, Massachusetts.
First printing, February 2009

www.it-ebooks.info
Download from www.wowebook.com


To my wife, Tamsyn, and our children Jason, Hayley,
Camilla, Rand, Elizabeth, and Miles.
—Eric Carter
To Leah Lippert, for embarking with me
on a fabulous adventure. And to David Lippert,
who taught me to expect the unexpected along the way.
—Eric Lippert

www.it-ebooks.info
Download from www.wowebook.com


This page intentionally left blank

www.it-ebooks.info
Download from www.wowebook.com


Contents at a Glance

Contents xi
Figures xxxi
Tables xlv
Foreword li
Preface lv
Acknowledgments lix
About the Authors lxi

PART I
1

An Introduction to VSTO

1

An Introduction to Office Programming 3

2 Introduction to Office Solutions 51

PART II

Office Programming in .NET

3 Programming Excel

87

89

4 Working with Excel Events 115
5 Working with Excel Objects 163
6 Programming Word
7

243

Working with Word Events 267

8 Working with Word Objects 311
9 Programming Outlook 405

www.it-ebooks.info

ix

Download from www.wowebook.com


x

Contents at a Glance

10 Working with Outlook Events 425
11 Working with Outlook Objects 475

PART III

Office Programming in VSTO

571

12 The VSTO Programming Model 573
13 Using Windows Forms and WPF in VSTO 627
14 Working with Document-Level Actions Panes 673
15 Working with Application-Level Custom Task Panes 701
16 Working with Outlook Form Regions 723
17 Working with the Ribbon in VSTO 777
18 Working with Smart Tags in VSTO

851

19 VSTO Data Programming 881
20 Server Data Scenarios 929
21 ClickOnce Deployment 955
Bibliography 1017
Index 1019

www.it-ebooks.info
Download from www.wowebook.com


Contents

Figures xxxi
Tables xlv
Foreword li
Preface lv
Acknowledgments lix
About the Authors lxi

PART I
1

An Introduction to VSTO

An Introduction to Office Programming

1
3

Why Office Programming? 3
Office Business Applications 3
Office Programming and the Professional Developer 4
Why .NET for Office? 4
Why Visual Studio Tools for Office? 5
How .NET Is It? 6

Office Object Models

7

Objects 8
Collections 11
Enumerations 13

Properties, Methods, and Events

14

Properties 15
Methods 21

www.it-ebooks.info

xi

Download from www.wowebook.com


xii

Contents
Optional Parameters in Word 24
Events 28

The Office Primary Interop Assemblies (PIAs)

39

Installing the PIAs 40
Referencing the PIAs 41
Browsing the PIAs 44

Conclusion

48

2 Introduction to Office Solutions

51

The Three Basic Patterns of Office Solutions

51

Hosted Code 52
How Code Gets Run After Startup 53
How Code Gets Unloaded 54

Office Automation Executables

54

Creating a Console Application That Automates an
Already Running Office Application 57
Creating a Console Application That Automates Word 59

Office Add-Ins

69

Creating an Outlook 2007 Add-In in VSTO 70
Changes Between Outlook 2003 VSTO Add-Ins
and Outlook 2007 VSTO Add-Ins 77

Code Behind a Document
Conclusion 86

PART II

78

Office Programming in .NET

3 Programming Excel

87

89

Ways to Customize Excel

89

Automation Executable 90
VSTO Add-Ins 90
Automation Add-Ins 92
Visual Studio Tools for Office Code Behind 92
Smart Documents and XML Expansion Packs 93
Smart Tags 94
XLA Add-Ins 97
Server-Generated Documents 97
Research Services 97

www.it-ebooks.info
Download from www.wowebook.com


Contents

Programming User-Defined Functions

xiii

98

Building a Managed Automation Add-In That Provides User-Defined
Functions 99
Using Your Managed Automation Add-In in Excel 101
Some Additional User-Defined Functions 106
Debugging User-Defined Functions in Managed Automation Add-Ins 107
Deploying Managed Automation Add-Ins 107

Introduction to the Excel Object Model 108
Conclusion 112

4 Working with Excel Events

115

Events in the Excel Object Model 115
New Workbook and Worksheet Events 116
Activation and Deactivation Events 120
Double-Click and Right-Click Events 128
Calculate Events 135
Change Events 139
Follow Hyperlink Events 141
Selection Change Events 145
WindowResize Events 146
Add-In Install and Uninstall Events 147
XML Import and Export Events 147
Before Close Events 148
Before Print Events 148
Before Save Events 149
Open Events 150
Toolbar and Menu Events 153
Additional Events 158
Events in Visual Studio 2008 Tools for Office 159

Conclusion

162

5 Working with Excel Objects

163

Working with the Application Object 163
Controlling Excel’s Screen Updating Behavior 164
Controlling the Dialog Boxes and Alerts that Excel Displays 165
Changing the Mouse Pointer 166
Displaying a Message in Excel’s Status Bar 166

www.it-ebooks.info
Download from www.wowebook.com


xiv

Contents
A Property You Should Never Use 167
Controlling the Editing Experience in Excel 168
Controlling the Look of Excel 169
Controlling File and Printer Settings 170
Properties That Return Active or Selected Objects 171
Properties That Return Important Collections 172
Controlling the Calculation of Workbooks 172
Using Built-In Excel Functions in Your Code 173
Selecting and Activating a Range of Cells 174
Spell Checking 174
Sending a Workbook in E-mail 175
Quitting Excel 176
Undo in Excel 176
Sending Keyboard Commands to Excel 176

Working with the Workbooks Collection

178

Iterating over the Open Workbooks 178
Accessing a Workbook in the Workbooks Collection 179
Creating a New Workbook 179
Opening an Existing Workbook 180
Closing All the Open Workbooks 181

Working with the Workbook Object 181
Properties That Return Active or Selected Objects 181
Properties That Return Important Collections 182
Accessing Document Properties 183
Saving an Excel Workbook 184
Naming Ranges of Cells 186
Creating and Activating Windows 188
Printing a Workbook 188
Protecting a Workbook 188

Working with the Worksheets, Charts,
and Sheets Collections 189
Iterating over the Open Sheets 190
Accessing a Sheet in the Collection 190
Adding a Worksheet or Chart Sheet 191
Copying a Sheet 192
Moving a Sheet 192

www.it-ebooks.info
Download from www.wowebook.com


Contents

Working with Document Properties

xv

192

Iterating over the DocumentProperties Collection 192
Accessing a DocumentProperty in the DocumentProperties Collection 193
Adding a DocumentProperty 194

Working with the Windows Collections 195
Iterating over the Open Windows 196
Accessing a Window in the Collection 196
Arranging Windows 197

Working with the Window Object 199
Positioning a Window 199
Display Settings Associated with a Window 200

Working with the Names Collection and Name Object

202

Iterating over the Names Collection 202
Accessing a Name in the Names Collection 202
The Name Object 203

Working with the Worksheet Object

204

Worksheet Management 204
Working with Names 206
Working with Worksheet Custom Properties 206
Protecting a Worksheet 208
Working with OLEObjects 214
Working with Shapes 215
Working with ChartObjects 216
Working with Tables 217

Working with the Range Object 219
Getting a Range Object for a Particular Cell or Range of Cells 219
Working with Addresses 223
Creating New Ranges Using Operator Methods 226
Working with Areas 227
Working with Cells 228
Working with Rows and Columns 229
Working with Regions 230
Selecting a Range 230
Editing the Values in a Range 231
Copying, Clearing, and Deleting Ranges 231

www.it-ebooks.info
Download from www.wowebook.com


xvi

Contents
Finding Text in a Range 232
Formatting a Range of Cells 234

Special Excel Issues

235

The Excel Locale Issue for Automation Executables and COM Add-Ins 235
Converting Excel Dates to DateTime 239

Conclusion

241

6 Programming Word

243

Ways to Customize Word

243

Automation Executable 243
VSTO Add-Ins 244
Visual Studio Tools for Office Code Behind 245
Smart Documents and XML Expansion Packs 246
Smart Tags 247
Server-Generated Documents 248

Programming Research Services

249

Getting Started with Research Services 250
Registering the Research Service with Word 255
Using the Research Service 256
More Research Service Resources

261

Introduction to the Word Object Model 261
Conclusion 262

7

Working with Word Events

267

Events in the Word Object Model 267
Why Are There Multiple Application and Document Event Interfaces? 267
Visual Studio Generation of Event Handlers 270
Startup and Shutdown Events 271
New and Open Document Events 273
Document Close Events 276
Document Save Events 278
Document Activation Events 279
Document Print Events 282
Mouse Events 283
Selection Events 285
Window Sizing Events 287

www.it-ebooks.info
Download from www.wowebook.com


Contents

xvii

XML Events 287
Sync Events 288
EPostage Events 288
Mail Merge Events 288
ContentControl Events 297
CommandBar Events 303

Events in Visual Studio Tools for Office
Conclusion 310

8 Working with Word Objects

307

311

Working with the Application Object 311
Controlling Word’s Screen Updating Behavior 312
Controlling the Dialogs and Alerts That Word Displays 313
Changing the Mouse Pointer 314
Displaying a Message in Word’s Status Bar or Window Caption 315
Controlling the Look of Word 316
Properties That Return Active or Selected Objects 319
Properties That Return Important Collections 320
Accessing Items in Collections 323
Navigating a Document 324
Working with Word’s Options 326
Working with the File Save Format Options 328
Working with File Dialogs 328
User Information 330
Checking Grammar and Spelling 330
Exiting Word 331

Working with the Dialog Object

332

Showing the Dialog and Letting Word Execute Actions 332
Selecting the Tab on a Dialog Box 333
Showing the Dialog and Preventing Word from Executing Actions 333
Getting and Setting Fields in a Dialog 334

Working with Windows 338
Creating New Windows 338
Iterating over the Open Windows 338

www.it-ebooks.info
Download from www.wowebook.com


xviii

Contents
Accessing a Window in the Collection 339
Arranging Windows 339

Working with Templates 341
Working with Documents 343
Iterating over the Open Documents 343
Accessing a Document in the Documents Collection 344
Creating a New Document 345
Opening an Existing Document 345
Closing All Open Documents 347
Saving All Open Documents 348

Working with a Document

348

Preserving the Dirty State of a Document 349
Closing and Saving a Document 350
Working with Windows Associated with a Document 352
Changing the Template Attached to a Document 353
Important Collections Associated with Both Document and Range 353
Important Collections Associated with Document Only 356
Working with Document Properties 357
Checking Spelling and Grammar in Documents and Ranges 362
Printing a Document 364
Working with Read-Only Document Protection 365
Working with Password Protection 367
Undo and Redo 369

Working with the Range Object 369
Getting a Range 370
Identifying a Range 373
Changing a Range 374
Moving a Range 377
Ranges and Stories 380
Navigating a Range 380
Collapsing a Range 382
Getting Text from a Range 382
Setting the Text in a Range 384
Inserting Nonprinting Characters and Breaks 385

www.it-ebooks.info
Download from www.wowebook.com


Contents

xix

Working with Formatting 387
Find and Replace 389

Working with Bookmarks 392
Working with Tables 394
Working with Content Controls

396

Working with Content Controls Programmatically 400
Properties and Methods of Content Control 402

Conclusion

403

9 Programming Outlook

405

Ways to Customize Outlook 405
Automation Executable 405
Add-Ins 406
Smart Tags 408
Custom Property Pages 410

Introduction to the Outlook Object Model 419
Conclusion 422

10 Working with Outlook Events

425

Events in the Outlook Object Model 425
Application-Level Events 427
Startup and Quit Events 429
Activation Events 432
New Window Events 434
Window Events 434
Close Events 436
View and Selection Change Events 436
Folder Change Events 439
Context Menu Events 442
Form Region Events 447

Outlook Item Events

448

Item Addition, Deletion, Change, and Move Events 448
Copy, Paste, Cut, and Delete Events 453
Property Change Events 455
Open, Read, Write, and Close Events 456
Item Load, Unload, and BeforeAutoSave Event 459

www.it-ebooks.info
Download from www.wowebook.com


xx

Contents
E-mail Events 461
Attachment Events 466
Custom Action Events 467

Other Events 470
Conclusion 473

11 Working with Outlook Objects

475

Working with the Application Object 475
Methods and Properties That Return Active or Selected Objects 476
Properties That Return Important Collections 477
Performing a Search and Creating a Search Folder 479
Copying a File into an Outlook Folder 485
Quitting Outlook 486

Working with the Explorers and Inspectors Collections 486
Working with the Explorer Object 488
Working with the Selected Folder, View, and Items 489
Working with an Explorer Window 494
Adding Buttons and Menus to an Explorer Window 495
Working with the Navigation Pane Associated with the Explorer
Window 496
Associating a Web View with a Folder 499
Setting and Clearing the Active Search for an Explorer Window 500

Working with the Inspector Object 501
Working with the Outlook Item Associated with the Inspector 501
Working with an Inspector Window 501
Working with an Inspector’s Word Editor 501
Adding Buttons and Menus to an Inspector Window 505

Working with the NameSpace Object 506
Working with the Root Folders of the Open Outlook Stores 507
Another Way to Iterate over Open Outlook Stores 508
Adding and Removing Outlook Stores 509
Managing the Master Category List 509
Checking Whether Outlook Is Offline 510
Getting Standard Folders Such As the Inbox Folder 510
Getting a Folder, Outlook Item, Store, or AddressEntry by ID 510

www.it-ebooks.info
Download from www.wowebook.com


Contents

xxi

Determining the Current User and an Introduction to Recipient,
AddressEntry, ContactItem, ExchangeUser, and
ExchangeDistributionList 514
Accessing Address Books and Address Entries 515
Displaying the Outlook Select Folder Dialog Box 518
Displaying the Select Names Dialog Box 518

Working with the Folder Object 519
MAPIFolder and Folder 519
Other Identifiers for a Folder 519
Getting the Store for a Folder 520
Accessing Subfolders Contained in a Folder 520
Accessing Items Contained in a Folder 523
Working with a Folder’s View Settings 529
Working with StorageItem Objects 531
Copying or Moving a Folder to a New Location 534
Displaying a Folder in an Explorer View 534

Working with the Items Collection

534

Iterating over Outlook Items 534
Finding an Outlook Item 537
Adding an Outlook Item to an Items Collection 541
Working with the Table Object 545

Properties and Methods Common to Outlook Items

548

Creating an Outlook Item 548
Identifying the Specific Type of an Outlook Item 551
Other Properties Associated with All Outlook Items 556
Copying or Moving an Outlook Item to a New Location 558
Deleting an Outlook Item 558
Displaying an Outlook Item in an Inspector View 560
Working with Built-In and Custom Properties Associated
with an Outlook Item 561
PropertyAccessor: Accessing Advanced Properties 565
Saving an Outlook Item 566
Showing the Color Categories Dialog Box for an Outlook Item 566
Mail Properties and Methods 566

Conclusion

569

www.it-ebooks.info
Download from www.wowebook.com


xxii

Contents

PART III

Office Programming in VSTO

12 The VSTO Programming Model

571

573

The VSTO Programming Model for Documents 573
Separation of Data and View 574
Model-View-Controller 575
Benefits of Separation 576

VSTO Extensions to Word and Excel Document Objects 576
Aggregation, Inheritance, and Implementation 576
Hooking Up the Aggregates 578
Obtaining the Aggregated Object 580
Aggregation and Windows Forms Controls 581
Improving C# Interoperability 582
The Tag Field 583
Event Model Improvements 583

Dynamic Controls in the Document

584

The Controls Collection 585
Enumerating and Searching the Collection 585
Adding New Word and Excel Host Controls Dynamically 586
Removing Controls 590
Dynamic Controls Information Is Not Persisted 590

Advanced Topic: Class Hookup and Cookies 591
Advanced Topic: Inspecting the Generated Code 594
The Startup and Shutdown Sequences 596
The Globals Class in Excel 598

VSTO Extensions to the Word and Excel Object Models

599

The Word Document Class 599
The Word Bookmark Host Control 605
The Word XMLNode and XMLNodes Host Control Classes 607
The Word Content Control Classes 609
The Excel Workbook Host Item Class 613
The Excel Worksheet Host Item Class 614
The Excel Chart Sheet Host Item Class and Chart Host Control 615
The Excel NamedRange, XmlMappedRange, and ListObject
Host Controls 615

The VSTO Programming Model for Add-Ins

619

www.it-ebooks.info
Download from www.wowebook.com


xxiii

Contents

Using VSTO Document Features in Application-Level Add-Ins

621

Preparing an Add-In Project to Use Document Features 621
Getting a VSTO Host Item Object for a Document, Worksheet,
or Workbook 622
Getting a VSTO Host Item Control for ListObject and Other
Host Item Controls 623
Other Applications and Limitations 623

Advanced Topic: Creating Worksheets Dynamically
Conclusion 625

13 Using Windows Forms and WPF in VSTO
Introduction

624

627

627

Moving from ActiveX to Windows Forms 627
Using WPF Controls via the ElementHost Control 628
When to Use Controls on the Document Surface 629
When to Use a Modal or Modeless Form 631

Adding Windows Forms Controls to Your Document

634

Modes for Adding Controls 635
Controls That Are Not in the Control Toolbox 636
Control Insertion Behavior in Word 638
Control Insertion Behavior in Excel 640
Layout of Controls on the Document or Worksheet Surface 641

Writing Code Behind a Control 641
The Windows Forms Control Hosting Architecture

643

The Windows Forms Control Host ActiveX Control 644
Why Are VSTO Controls Derived from Windows Forms Controls? 645
Security Implications of the VSTO Control Hosting Model 648
Limitations of the Control Hosting Model 649
Control State Is Not Saved in the Document 650
Why Are Controls Sometimes Slightly Blurry? 653

Properties Merged from OLEObject or OLEControl

654

Excel Control Properties That Are Added from OLEObject 654
Word Control Properties Added from OLEControl 654

Adding Controls at Runtime 658
Working with the Controls Collection 661
AddControl 663

www.it-ebooks.info
Download from www.wowebook.com


xxiv

Contents
Deleting Controls at Runtime 664
Why Are Controls Added at Runtime Not Saved in the Document? 664
Why Are Controls in the Controls Collection Typed as Object
Instead of Control? 666
Creating Controls in a Document Using an Application-Level Add-In 666

Using WPF Controls in the Document 669
Conclusion 671

14 Working with Document-Level Actions Panes

673

Introduction to the Document Actions Task Pane
Working with the ActionsPane Control 680

673

The ActionsPane Architecture 680
Adding Windows Forms Controls to the Actions Pane 681
Adding a Custom User Control to the Actions Pane 684
Contextually Changing the Actions Pane 686
Detecting the Orientation of the Actions Pane 689
Scrolling the Actions Pane 691
Showing and Hiding the Actions Pane 691
Attaching and Detaching the Actions Pane 695
Some Methods and Properties to Avoid 697

Using WPF Controls in an Actions Pane
Conclusion 699

697

15 Working with Application-Level Custom Task Panes

701

Introduction to the Application-Level Custom Task Panes
Working with Custom Task Panes 704

701

The CustomTaskPane Architecture 704
The CustomTaskPanes Collection 705
The CustomTaskPane Object 706
Adding a Custom User Control to the Custom Task Pane 708

Custom Task Panes and Application Windows

710

Custom Task Panes and Outlook-Specific Issues 711
Custom Task Panes and Word-Specific Issues 714
Custom Task Panes and Excel-Specific Issues 717

Using WPF Controls in a Custom Task Pane
Conclusion 722

719

www.it-ebooks.info
Download from www.wowebook.com


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay

×