在 Office 解决方案中编写代码Write code in Office solutions

编写 Office 项目代码与编写 Visual Studio 中其他类型的代码在某些方面存在不同。There are some aspects of writing code in Office projects that are different from other types of projects in Visual Studio. 其中许多差异与 Office 对象模型公开给托管代码的方式相关。Many of these differences are related to the way the Office object models are exposed to managed code. 其他差异与 Office 项目的设计相关。Other differences are related to the design of Office projects.

适用于: 本主题中的信息适用于文档-级别项目和 VSTO 添加-在项目中。Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects. 请参阅按 Office 应用程序和项目类型提供的功能See Features available by Office application and project type.

托管代码和 Office 编程Managed code and Office programming

使创建集成的 Microsoft Office 解决方案成为可能的关键技术是自动化,它是组件对象模型 (COM) 技术的一部分。The key technology that makes creating an integrated Microsoft Office solution possible is Automation, which is part of the Component Object Model (COM) technology. 自动化使你能够使用代码来创建和控制由任何应用程序、DLL 或支持相应编程接口的 ActiveX 控件公开的软件对象。Automation enables you to use code to create and control software objects exposed by any application, DLL, or ActiveX control that supports the appropriate programmatic interfaces.

了解主互操作程序集Understand primary interop assemblies

Microsoft Office 应用程序向自动化公开其大部分功能。Microsoft Office applications expose much of their functionality to Automation. 但是,不能直接使用托管代码(如 Visual Basic 或 C# )来自动执行 Office 应用程序。However, you cannot use managed code (such as Visual Basic or C#) directly to automate Office applications. 若要使用托管代码来自动执行 Office 应用程序,必须使用 Office 主互操作程序集 (PIA)。To automate Office applications by using managed code, you must use the Office primary interop assemblies (PIAs). 主互操作程序集使托管代码可与 Office 应用程序的基于 COM 的对象模型进行交互。The primary interop assemblies enable managed code to interact with the COM-based object model of the Office applications.

每个 Microsoft Office 应用程序都有 PIA。Every Microsoft Office application has a PIA. 当你在 Visual Studio 中创建 Office 项目时,则对相应 PIA 的引用将自动添加到项目中。When you create an Office project in Visual Studio, a reference to the appropriate PIA is automatically added to the project. 为了自动执行来自项目的其他 Office 应用程序的功能,必须手动添加对相应 PIA 的引用。To automate the features of other Office applications from the project, you must add a reference to the appropriate PIA manually. 有关详细信息,请参阅如何:通过主互操作程序集面向 Office 应用程序。For more information, see How to: Target Office applications through primary interop assemblies.

在设计时和运行时使用主互操作程序集Use primary interop assemblies at design time and runtime

必须在开发计算机上的全局程序集缓存中安装并注册 Office PIA 才能执行大多数开发任务。You must have the Office PIAs installed and registered in the global assembly cache on your development computer to perform most development tasks. 有关详细信息,请参阅配置计算机以开发 Office 解决方案For more information, see Configure a computer to develop Office solutions.

若要运行面向 .NET Framework 4.NET Framework 4 或更高版本的 Office 解决方案,无需在最终用户计算机上安装 Office PIA。The Office PIAs are not required on end-user computers to run Office solutions that target the .NET Framework 4.NET Framework 4 or later. 有关详细信息,请参阅设计和创建 Office 解决方案For more information, see Design and create Office solutions.

在主互操作程序集中使用类型Use types in primary interop assemblies

Office PIA 包含类型组合,该类型组合公开 Office 应用程序和不应直接在代码中使用的其他基础结构类型的对象模型。The Office PIAs contain a combination of types that expose the object model of the Office applications and additional infrastructure types that are not intended to be used directly in your code. 有关 Office Pia 中的类型的概述,请参阅office 主互操作程序集中的类和接口概述For an overview of the types in the Office PIAs, see Overview of classes and interfaces in the Office primary interop assemblies.

由于 Office PIA 中的类型对应于基于 COM 的对象模型中的类型,因此使用这些类型的方式通常不同于其他托管类型。Because the types in the Office PIAs correspond to types in the COM-based object models, the way you use these types is often different from other managed types. 例如,调用在 Office 主互操作程序集中具有可选参数的方法的方式取决于你正在项目中使用的编程语言。For example, the way you call methods that have optional parameters in an Office primary interop assembly depends on the programming language you are using in your project. 有关详细信息,请参阅下列主题:For more information, see the following topics:

Office 项目的程序模型Program model of Office projects

所有 Office 项目都包括一个或多个为你的代码提供入口点的生成类。All Office projects include one or more generated classes that provide the entry point for your code. 这些类还提供对主机应用程序的对象模型的访问以及对操作窗格和自定义任务窗格等功能的访问。These classes also provide access to the object model of the host application and access to features like actions panes and custom task panes.

了解生成的类Understand the generated classes

在 Excel 和 Word 的文档级项目中,生成的类类似于应用程序对象模型中的顶级对象。In document-level projects for Excel and Word, the generated class resembles a top-level object in the application's object model. 例如,Word 文档项目中生成的 ThisDocument 类与 Word 对象模型中的 Document 类提供相同的成员。For example, the generated ThisDocument class in a Word document project provides the same members as the Document class in the Word object model. 有关文档级项目中生成的类的详细信息,请参阅程序文档级自定义项For more information about the generated classes in document-level projects, see Program document-level customizations.

VSTO 外接程序项目提供一个名为 ThisAddIn的生成类。VSTO Add-in projects provide a generated class called ThisAddIn. 此类与主机应用程序的对象模型中的类不同。This class does not resemble a class in the host application's object model. 此类表示 VSTO 外接程序本身,并且它提供可用于访问主机应用程序的对象模型和访问 VSTO 外接程序提供的其他功能的成员。有关详细信息,请参阅PROGRAM VSTO 外接程序Instead, this class represents the VSTO Add-in itself, and it provides members you can use to access the object model of the host application and access other features available to VSTO Add-ins. For more information, see Program VSTO Add-ins.

Office 项目中的所有生成类均包括 StartupShutdown 事件处理程序。All generated classes in Office projects include Startup and Shutdown event handlers. 若要开始编写代码,通常将代码添加到这些事件处理程序中。To get started writing code, you typically add code to these event handlers. 若要初始化 VSTO 外接程序,可以将代码添加到 Startup 事件处理程序中。To initialize your VSTO Add-in, you can add code to the Startup event handler. 若要清理 VSTO 外接程序使用的资源,可以将代码添加到 Shutdown 事件处理程序中。To clean up resources used by your VSTO Add-in, you can add code to the Shutdown event handler. 有关详细信息,请参阅Office 项目中的事件For more information, see Events in Office projects.

在运行时访问生成的类Access the generated classes at run time

加载 Office 解决方案后, Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime 将实例化项目中的每个生成类。When an Office solution is loaded, the Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime instantiates each of the generated classes in your project. 可以通过使用 Globals 类从项目中的任何代码访问这些对象。You can access these objects from any code in your project by using the Globals class. 例如,可以使用Globals类从 VSTO 外接程序中功能区按钮的事件处理程序调用ThisAddIn类中的代码。For example, you can use the Globals class to call code in the ThisAddIn class from an event handler of a Ribbon button in a VSTO Add-in.

有关详细信息,请参阅对 Office 项目中对象的全局访问For more information, see Global access to objects in Office projects.

Office 解决方案中的命名空间注意事项Namespace considerations in Office solutions

创建项目后,不能更改 Office 项目的 “默认命名空间” (或 Visual Basic 中的 “根命名空间” )。You cannot change the default namespace (or root namespace in Visual Basic) of an Office project after you create the project. 创建项目后,默认命名空间将始终与你指定的项目名匹配。The default namespace will always match the project name you specified when you created the project. 如果你重命名项目,将不会更改默认命名空间。If you rename your project, the default namespace does not change. 有关项目中的默认命名空间的详细信息,请参阅应用程序页(、#项目设计器 C应用(程序)页、项目设计器 Visual BasicFor more information about the default namespace in projects, see Application Page, Project Designer (C#) and Application Page, Project Designer (Visual Basic).

更改项目中C#主机项类的命名空间Change the namespace of host item classes in C# projects

主机项类(例如, ThisAddInThisWorkbookThisDocument 类)在 Visual C# Office 项目中具有自己的命名空间。Host item classes (for example, the ThisAddIn, ThisWorkbook, or ThisDocument classes) have their own namespaces in Visual C# Office projects. 默认情况下,在创建项目后,项目中的主机项的命名空间与你指定的项目名相匹配。By default, the namespace for host items in your project matches the project name you specified when you created the project.

若要更改 Visual C# Office 项目中的主机项的命名空间,请使用 “主机项的命名空间” 属性。To change the namespace of the host items in a Visual C# Office project, use the Namespace for Host Item property. 有关详细信息,请参阅Office 项目中的属性For more information, see Properties in Office projects.

Office 项目中受支持的编程语言Supported programming languages in Office projects

Visual Studio 中的 Office 项目模板仅支持 Visual Basic 和 Visual C# 编程语言。The Office project templates in Visual Studio support only the Visual Basic and Visual C# programming languages. 因此,这些项目模板只能在 中的 “新建项目” 对话框的 “Visual Basic” “Visual C#” Visual StudioVisual Studio节点下找到。Therefore, these project templates are available only under the Visual Basic and Visual C# nodes of the New Project dialog box in Visual StudioVisual Studio. 有关详细信息,请参阅如何:在 Visual Studio中创建 Office 项目。For more information, see How to: Create Office projects in Visual Studio.

语言选择和 Office 编程Language choice and Office programming

Microsoft Office 和 Visual Basic for Applications (VBA) 被开发为协同工作以优化应用程序自定义项的工作流。Microsoft Office and Visual Basic for Applications (VBA) were developed to work together to optimize the workflow of application customization. Visual Basic 已继承其中一些开发。Visual Basic has inherited some of those developments. 例如,Visual Basic 支持可选的参数,这意味着你在调用 Microsoft Office 主互操作程序集中的一些方法时编写的代码比使用 Visual C# 时少。For example, Visual Basic supports optional parameters, which means that you can write less code when calling some methods in the Microsoft Office primary interop assemblies than when you use Visual C#.

具有 Visual Basic 的程序与Office C#解决方案中的视觉对象Program with Visual Basic vs. Visual C# in Office solutions

你可以通过使用 Visual Basic 或 Visual C# 创建 Office 解决方案。You can create Office solutions by using either Visual Basic or Visual C#. 由于 Microsoft Office 对象模型被设计为与 Microsoft Visual Basic for Applications (VBA) 一起使用,因此 Visual Basic 开发人员可以轻松使用由 Microsoft Office 应用程序公开的对象。Because the Microsoft Office object models were designed to be used with Microsoft Visual Basic for Applications (VBA), Visual Basic developers can work comfortably with the objects exposed by the Microsoft Office applications. Visual C# 开发人员可以使用与 Visual Basic 开发人员相同的大多数功能,但在某些情况下,他们必须编写附加代码才能使用 Office 对象模型。Visual C# developers can use most of the same features as Visual Basic developers, but there are some cases where they must write additional code to use the Office object models. Office 开发中的基本编程功能与在 Visual Basic 和 C# 中编写的托管代码之间也有一些差异。There are also some differences between basic programming features in Office development and managed code written in Visual Basic and C#.

Visual Basic 和视觉对象之间的主要差异C#Key differences between Visual Basic and Visual C#

下表显示了 Office 开发中 Visual Basic 与 Visual C# 之间的主要差异。The following table shows key differences between Visual Basic and Visual C# in Office development.

功能Feature 描述Description Visual Basic 支持Visual Basic support Visual C# 支持Visual C# support
可选参数Optional parameters 许多 Microsoft Office 方法具有调用该方法时不需要的参数。Many Microsoft Office methods have parameters that are not required when you call the method. 如果没有为参数传递任何值,则使用默认值。If no value is passed for the parameter, a default value is used. Visual Basic 支持可选参数。Visual Basic supports optional parameters. Visual C# 在大多数情况下支持可选参数。Visual C# supports optional parameters in most cases. 有关详细信息,请参阅Office 解决方案中的可选参数For more information, see Optional parameters in Office solutions.
按引用传递参数Passing parameters by reference 大多数 Microsoft Office 主互操作程序集中的可选参数可以按值传递。Optional parameters in most of the Microsoft Office primary interop assemblies can be passed by value. 但是,在某些主互操作程序集中,接受引用类型的可选参数必须按引用传递。However, in some primary interop assemblies, optional parameters that accept reference types must be passed by reference.

有关值和引用类型参数的详细信息,请参阅按值和通过引用(传递参数) Visual Basic (对于 Visual Basic)和传递(参数# C 编程)指南For more information about value and reference type parameters, see Pass arguments by value and by reference (Visual Basic) (for Visual Basic) and Pass parameters (C# programming guide).
按引用传递参数不需要完成任何额外工作。No additional work is needed to pass parameters by reference. Visual Basic 编译器在必要时会自动按引用传递参数。The Visual Basic compiler automatically passes the parameters by reference when necessary. 大多数情况下,Visual C# 编译器在必要时会自动按引用传递参数。In most cases, the Visual C# compiler automatically passes the parameters by reference when necessary. 有关详细信息,请参阅Office 解决方案中的可选参数For more information, see Optional parameters in Office solutions.
参数化属性Parameterized properties 某些属性接受参数,并充当只读函数。Some properties accept parameters, and act as read-only functions. Visual Basic 支持接受参数的属性。Visual Basic supports properties that accept parameters. Visual C# 支持接受参数的属性。Visual C# supports properties that accept parameters.
后期绑定Late binding 后期绑定涉及在运行时确定对象的属性,而不是在设计时将变量强制转换为对象类型。Late binding involves determining the properties of objects at run time, instead of casting variables to the object type at design time. Option Strict 处于关闭状态时,Visual Basic 执行后期绑定。Visual Basic performs late binding when Option Strict is off. Option Strict 处于启用状态时,必须显式转换对象并使用 System.Reflection 命名空间中的类型访问后期绑定成员。When Option Strict is on, you must explicitly convert objects and use types in the System.Reflection namespace to access late-bound members. 有关详细信息,请参阅Office 解决方案中的后期绑定For more information, see Late binding in Office solutions. Visual C# 在面向 .NET Framework 4.NET Framework 4的项目中执行后期绑定。Visual C# performs late binding in projects that target the .NET Framework 4.NET Framework 4. 有关详细信息,请参阅Office 解决方案中的后期绑定For more information, see Late binding in Office solutions.

Office 开发和托管代码之间的主要差异Key differences between Office development and managed code

下表显示了 Office 开发代码与在 Visual Basic 或 Visual C# 中编写的托管代码之间的主要差异。The following table shows key differences between Office development and managed code written in Visual Basic or Visual C#.

功能Feature 描述Description Visual Basic 和 Visual C# 支持Visual Basic and Visual C# support
数组索引Array indexes Microsoft Office 应用程序中集合的数组下限从 1 开始。The lower array bound of collections in Microsoft Office applications begins with 1. Visual Basic 和 Visual C# 使用基于 0 的数组。Visual Basic and Visual C# use 0-based arrays. 有关详细信息,请参阅(数组# C 编程)指南Visual Basic 中的数组For more information, see Arrays (C# programming guide) and Arrays in Visual Basic. 若要访问 Microsoft Office 应用程序对象模型中某个集合的第一项,请使用索引 1,而不是 0。To access the first item of a collection in the object model of a Microsoft Office application, use the index 1 instead of 0.

请参阅See also