合并 VBA 和文档级自定义项Combine VBA and document-level customizations

在属于 Microsoft Office Word 或 Microsoft Office Excel 的文档级自定义项的文档中,可以使用 Visual Basic for Applications (VBA) 代码。You can use Visual Basic for Applications (VBA) code in a document that is part of a document-level customization for Microsoft Office Word or Microsoft Office Excel. 可以从自定义程序集调用文档中的 VBA 代码,也可以将项目配置为使文档中的 VBA 代码能够调用自定义程序集中的代码。You can call VBA code in the document from the customization assembly, or you can configure your project to enable VBA code in the document to call code in the customization assembly.

适用于: 本主题中的信息适用于文档-级别 Excel 和 Word 的项目。Applies to: The information in this topic applies to document-level projects for Excel and Word. 有关详细信息,请参阅按 Office 应用程序和项目类型提供的功能For more information, see Features available by Office application and project type.

文档级自定义项中 VBA 代码的行为Behavior of VBA code in a document-level customization

在 Visual Studio 中打开项目时,文档将以设计模式打开。When you open your project in Visual Studio, the document is opened in design mode. 当文档处于设计模式时,VBA 代码不运行,因此你可以在不运行 VBA 代码的情况下处理文档和代码。VBA code does not run when the document is in design mode, so you can work on the document and code without running the VBA code.

运行解决方案时,VBA 和自定义程序集中的事件处理程序选取文档中引发的事件,并且两组代码都会运行。When you run the solution, event handlers in both VBA and the customization assembly pick up events that are raised in the document, and both sets of code run. 无法预先确定哪组代码将在另一组代码之前运行;必须在每个单独的情况下通过测试来确定这一点。You cannot determine beforehand which code will run before the other; you must determine this through testing in each individual case. 如果没有仔细调整和测试这两组代码,可能会得到意外的结果。You can get unexpected results if the two sets of code are not carefully coordinated and tested.

从自定义程序集调用 VBA 代码Call VBA code from the customization assembly

可以在 Word 文档中调用宏,也可以在 Excel 工作簿中调用宏和函数。You can call macros in Word documents, and you can call macros and functions in Excel workbooks. 若要执行此操作,请使用以下方法之一:To do this, use one of the following methods:

  • 对于 Word,调用 Application 类的 Run 方法。For Word, call the Run method of the Application class.

  • 对于 Excel,调用 Run 类的 Application 方法。For Excel, call the Run method of the Application class.

    对于每个方法,第一个参数标识要调用的宏或函数的名称,其余的可选参数指定要传递到宏或函数的参数。For each method, the first parameter identifies the name of the macro or function you want to call, and the remaining optional parameters specify the parameters to pass to the macro or function. 对于 Word 和 Excel,第一个参数的格式可能不同:The first parameter can have different formats for Word and Excel:

  • 对于 Word,第一个参数是一个字符串,该字符串可以是模板、模块和宏名称的任意组合。For Word, the first parameter is a string that can be any combination of template, module, and macro name. 如果指定文档名称,则你的代码只能运行与当前上下文相关的文档中的宏,而不是任何文档中的任何宏。If you specify the document name, your code can only run macros in documents related to the current context — not just any macro in any document.

  • 对于 Excel,第一个参数可以是指定宏名称的字符串、指示函数位置的 Range 或已注册 DLL (XLL) 函数的注册 ID。For Excel, the first parameter can be a string that specifies the macro name, a Range that indicates where the function is, or a register ID for a registered DLL (XLL) function. 如果传递字符串,则将在活动工作表的上下文中计算该字符串。If you pass a string, the string will be evaluated in the context of the active sheet.

    下面的代码示例演示如何从 Excel 文档级项目调用名为 MyMacro 的宏。The following code example shows how to call a macro named MyMacro from a document-level project for Excel. 此示例假定 MyMacro 是在 Sheet1中定义的 。This example assumes that MyMacro is defined in Sheet1.

Globals.Sheet1.Application.Run("MyMacro")
Globals.Sheet1.Application.Run("MyMacro", missing, missing, missing,
    missing, missing, missing, missing, missing, missing, missing,
    missing, missing, missing, missing, missing, missing, missing,
    missing, missing, missing, missing, missing, missing, missing,
    missing, missing, missing, missing, missing, missing);

Note

有关使用全局missing变量代替 Visual C#中的可选参数的信息,请参阅在Office 解决方案中编写代码For information about using the global missing variable in place of optional parameters in Visual C#, see Write code in Office solutions.

从 VBA 调用文档级自定义项中的代码Call code in document-level customizations from VBA

可以配置 Word 或 Excel 的文档级项目,以便文档中的 Visual Basic for Applications (VBA) 代码能够调用自定义程序集中的代码。You can configure a document-level project for Word or Excel so that Visual Basic for Applications (VBA) code in the document can call code in the customization assembly. 这在以下应用场景中很有用:This is useful in the following scenarios:

  • 你希望使用与某个文档相关联的文档级自定义项中的功能扩展同一文档中的现有 VBA 代码。You want to extend existing VBA code in a document by using features in a document-level customization that is associated with the same document.

  • 你希望将你使用文档级自定义项开发的服务提供给能够通过在文档中编写 VBA 代码来访问服务的最终用户。You want to make services that you develop in a document-level customization available to end users who can access the services by writing VBA code in the document.

    Visual Studio 中的 Office 开发工具可为 VSTO 外接程序提供相似的功能。如果你正在开发 VSTO 外接程序,可以从其他 Microsoft Office 解决方案调用 VSTO 外接程序中的代码。The Office development tools in Visual Studio provide a similar feature for VSTO Add-ins. If you are developing a VSTO Add-in, you can call code in your VSTO Add-in from other Microsoft Office solutions. 有关详细信息,请参阅从其他 Office 解决方案调用 VSTO 外接程序中的代码For more information, see Call code in VSTO Add-ins from other Office solutions.

Note

此功能无法在 Word 模板项目中使用。This feature cannot be used in Word template projects. 它只能在 Word 文档、Excel 工作簿或 Excel 模板项目中使用。It can be used only in Word document, Excel workbook, or Excel template projects.

要求Requirements

你的项目必须满足以下要求,然后才能使 VBA 代码调入自定义项程序集:Before you can enable VBA code to call into the customization assembly, your project must meet the following requirements:

  • 文档必须具有以下文件扩展名之一:The document must have one of the following file name extensions:

    • 对于 Word: . docm.docFor Word: .docm or .doc

    • 对于 Excel: . xlsm. xltm.xls.xltFor Excel: .xlsm, .xltm, .xls, or .xlt

  • 文档必须已经包含其中有 VBA 代码的 VBA 项目。The document must already contain a VBA project that has VBA code in it.

  • 必须在不提示用户启用宏的情况下允许文档中的 VBA 代码运行。VBA code in the document must be allowed to run without prompting the user to enable macros. 通过在 Word 或 Excel 的“信任中心”设置中将 Office 项目的位置添加到受信任位置列表中,可以信任要运行的 VBA 代码。You can trust VBA code to run by adding the location of the Office project to the list of trusted locations in the Trust Center settings for Word or Excel.

  • Office 项目必须至少包含一个公共类,该类包含一个或多个要向 VBA 公开的公共成员。The Office project must contain at least one public class that contains one or more public members that you are exposing to VBA.

    可以向 VBA 公开方法、属性和事件。You can expose methods, properties, and events to VBA. 公开的类可以是主机项类(如 Word 的 ThisDocument 或 Excel 的 ThisWorkbookSheet1 )或你在项目中定义的其他类。The class that you expose can be a host item class (such as ThisDocument for Word, or ThisWorkbook and Sheet1 for Excel) or another class that you define in your project. 有关宿主项的详细信息,请参阅主机项和主机控件概述For more information about host items, see Host items and host controls overview.

使 VBA 代码能够调入自定义程序集Enable VBA code to call into the customization assembly

可通过两种不同方式向文档中的 VBA 代码公开自定义项程序集中的成员:There are two different ways that you can expose members in a customization assembly to VBA code in the document:

  • 可以向 VBA 公开 Visual BasicVisual Basic 项目中某个主机项类的成员。You can expose members of a host item class in a Visual BasicVisual Basic project to VBA. 若要这样做,请在设计器中打开主机项(即文档、工作表或工作簿),并在 “属性” 窗口中将主机项的 EnableVbaCallers 属性设置为 TrueTo do this, set the EnableVbaCallers property of the host item to True in the Properties window while the host item (that is, the document, worksheet, or workbook) is open in the designer. Visual Studio 会自动执行使 VBA 代码能够调用类成员所需的所有工作。Visual Studio automatically performs all of the work required to enable VBA code to call members of the class.

  • 可以向 VBA公开 Visual C# 项目中任何公共类的成员或 Visual BasicVisual Basic 项目中非主机项类的成员。You can expose members in any public class in a Visual C# project, or members in a non-host item class in a Visual BasicVisual Basic project, to VBA. 通过此选项,你可以更自由地选择要向 VBA 公开的类,但它也需要更多的手动步骤。This option provides you with more freedom to choose which classes you expose to VBA, but it also requires more manual steps.

    为此,必须执行下列主要步骤:To do this, you must perform the following main steps:

    1. 向 COM 公开该类。Expose the class to COM.

    2. 替代项目中主机项类的 GetAutomationObject 方法,以返回要向 VBA 公开的类的实例。Override the GetAutomationObject method of a host item class in your project to return an instance of the class that you are exposing to VBA.

    3. 将项目中任何主机项类的 ReferenceAssemblyFromVbaProject 属性设置为 TrueSet the ReferenceAssemblyFromVbaProject property of any host item class in the project to True. 这会将自定义项程序集的类型库嵌入程序集,并将对该类型库的引用添加到文档中的 VBA 项目。This embeds the customization assembly's type library into the assembly and adds a reference to the type library to the VBA project in the document.

    有关详细说明,请参阅如何:在 Visual Basic 项目中向 VBA 公开代码, 以及如何:在 Visual C#项目中向 VBA 公开代码。For detailed instructions, see How to: Expose code to VBA in a Visual Basic project and How to: Expose code to VBA in a Visual C# project.

    EnableVbaCallersReferenceAssemblyFromVbaProject 属性仅在设计时在 “属性” 窗口中可用,无法在运行时使用。The EnableVbaCallers and ReferenceAssemblyFromVbaProject properties are available only in the Properties window at design time; they cannot be used at run time. 若要查看这些属性,请在 Visual StudioVisual Studio中打开主机项的设计器。To view the properties, open the designer for a host item in Visual StudioVisual Studio. 有关设置这些属性时 Visual Studio 执行的特定任务的详细信息,请参阅由主机项属性执行的任务For more information about the specific tasks that Visual Studio performs when you set these properties, see Tasks performed by the host item properties.

Note

如果工作簿或文档尚未包含 VBA 代码,或者如果运行文档中的 VBA 代码时不信任该代码,则在将 “EnableVbaCallers”“ReferenceAssemblyFromVbaProject” 属性设置为 “True” 时,你将收到一条错误消息。If the workbook or document does not already contain VBA code or if VBA code in the document is not trusted to run, you will receive an error message when you set the EnableVbaCallers or ReferenceAssemblyFromVbaProject property to True. 这是因为在这种情况下,Visual Studio 无法修改文档中的 VBA 项目。This is because Visual Studio cannot modify the VBA project in the document in this situation.

使用 VBA 代码中的成员调入自定义项程序集Use members in VBA code to call into the customization assembly

在将项目配置为使 VBA 代码能够调入自定义项程序集后,Visual Studio 会将以下成员添加到文档中的 VBA 项目:After you configure your project to enable VBA code to call into the customization assembly, Visual Studio adds the following members to the VBA project in the document:

  • 对于所有项目,Visual Studio 都会添加一个名为 GetManagedClass的全局方法。For all projects, Visual Studio adds a global method named GetManagedClass.

  • 对于在其中使用 Visual BasicVisual Basic 属性公开主机项类成员的 “属性” 项目,Visual Studio 还会将一个名为 CallVSTOAssembly 的属性添加到 VBA 项目中的 ThisDocumentThisWorkbookSheet1Sheet2Sheet3 模块。For Visual BasicVisual Basic projects in which you expose members of a host item class by using the EnableVbaCallers property, Visual Studio also adds a property named CallVSTOAssembly to the ThisDocument, ThisWorkbook, Sheet1, Sheet2, or Sheet3 module in the VBA project.

    可以使用 CallVSTOAssembly 属性或 GetManagedClass 方法来访问向项目中的 VBA 代码公开的类的公共成员。You can use the CallVSTOAssembly property or GetManagedClass method to access public members of the class that you exposed to VBA code in the project.

Note

在开发和部署解决方案时,可以在多个不同的文档副本中添加 VBA 代码。While you develop and deploy your solution, there are several different copies of the document where you can add the VBA code. 有关详细信息,请参阅向文档添加 VBA 代码的准则For more information, see Guidelines for adding VBA code to the document.

在 Visual Basic 项目中使用 CallVSTOAssembly 属性Use the CallVSTOAssembly property in a Visual Basic project

使用 CallVSTOAssembly 属性来访已添加到主机项类的公共成员。Use the CallVSTOAssembly property to access public members that you added to the host item class. 例如,下面的 VBA 宏调用一个名为 MyVSTOMethod 的方法,该方法是在 Excel 工作簿项目的 Sheet1 类中定义的。For example, the following VBA macro calls a method named MyVSTOMethod that is defined in the Sheet1 class in an Excel workbook project.

Sub MyMacro()
    Sheet1.CallVSTOAssembly.MyVSTOMethod()
End Sub

与直接使用 GetManagedClass 方法相比,使用此属性,可以更加方便地调入自定义项程序集。This property is a more convenient way to call into the customization assembly than using the GetManagedClass method directly. CallVSTOAssembly 返回一个对象,该对象表示向 VBA 公开的主机项类。CallVSTOAssembly returns an object that represents the host item class that you exposed to VBA. 所返回对象的成员和方法参数将出现在 IntelliSense 中。The members and method parameters of the returned object appear in IntelliSense.

CallVSTOAssembly 属性具有类似于以下代码的声明。The CallVSTOAssembly property has a declaration that is similar to the following code. 此代码假定已向 VBA 公开 Excel 工作簿项目中的名为 Sheet1ExcelWorkbook1 主机项类。This code assumes that you have exposed the Sheet1 host item class in an Excel workbook project named ExcelWorkbook1 to VBA.

Property Get CallVSTOAssembly() As ExcelWorkbook1.Sheet1
    Set CallVSTOAssembly = GetManagedClass(Me)
End Property

使用 GetManagedClass 方法Use the GetManagedClass method

若要使用全局 GetManagedClass 方法,请传入与主机项类相对应的 VBA 对象,该主机项来包含 GetAutomationObject 方法的替代。To use the global GetManagedClass method, pass in the VBA object that corresponds to the host item class that contains your override of the GetAutomationObject method. 然后,使用返回的对象访问向 VBA 公开的类。Then, use the returned object to access the class that you exposed to VBA.

例如,下面的 VBA 宏调用一个名为 MyVSTOMethod 的方法,该方法是在名为 Sheet1 的 Excel 工作簿项目的 ExcelWorkbook1主机项类中定义的。For example, the following VBA macro calls a method named MyVSTOMethod that is defined in the Sheet1 host item class in an Excel workbook project named ExcelWorkbook1.

Sub CallVSTOMethod
    Dim VSTOSheet1 As ExcelWorkbook1.Sheet1
    Set VSTOSheet1 = GetManagedClass(Sheet1)
    VSTOSheet1.MyVSTOMethod
End Sub

GetManagedClass 方法具有以下声明。The GetManagedClass method has the following declaration.

GetManagedClass(pdispInteropObject Object) As Object

此方法返回一个对象,该对象表示向 VBA 公开的类。This method returns an object that represents the class that you exposed to VBA. 所返回对象的成员和方法参数将出现在 IntelliSense 中。The members and method parameters of the returned object appear in IntelliSense.

向文档添加 VBA 代码的准则Guidelines for adding VBA code to the document

可在多个不同的文档副本中添加调入文档级自定义项的 VBA 代码。There are several different copies of the document where you can add VBA code that calls into the document-level customization.

在开发和测试解决方案时,对于当你在 Visual Studio 中调试或运行项目时将打开的文档(即位于生成输出文件夹中的文档),你可以在其中编写 VBA 代码。As you develop and test your solution, you can write VBA code in the document that opens while you debug or run your project in Visual Studio (that is, the document in the build output folder). 但是,在下次生成项目时,添加到此文档的任何 VBA 代码都将被覆盖,因为 Visual Studio 会将生成输出文件夹中的文档替换为主项目文件夹中文档的副本。However, any VBA code you add to this document will be overwritten the next time that you build the project, because Visual Studio replaces the document in the build output folder with a copy of the document from the main project folder.

如果要保存在调试或运行解决方案时添加到文档的 VBA 代码,请将 VBA 代码复制到项目文件夹内的文档中。If you want to save the VBA code that you add to the document while debugging or running the solution, copy the VBA code into the document in the project folder. 有关生成过程的详细信息,请参阅生成 office 解决方案For more information about the build process, see Build office solutions.

准备好部署解决方案时,可以在三个主要文档位置中添加 VBA 代码。When you are ready to deploy your solution, there are three main document locations in which you can add the VBA code.

开发计算机上的项目文件夹中In the project folder on the development computer

如果你能够完全控制文档中的 VBA 代码和自定义项代码,则此位置非常方便。This location is convenient if you have complete control over both the VBA code in the document and the customization code. 由于文档位于开发计算机上,因此,如果更改自定义项代码,你可以轻松地修改 VBA 代码。Because the document is on the development computer, you can easily modify the VBA code if you change the customization code. 当你生成、调试和发布解决方案时,添加到此文档副本的 VBA 代码将保留在文档中。VBA code that you add to this copy of the document remains in the document when you build, debug, and publish your solution.

当文档在设计器中处于打开状态时,无法向其中添加 VBA 代码。You cannot add the VBA code to the document while it is open in the designer. 必须先在设计器中关闭文档,然后在 Word 或 Excel 中直接打开该文档。You must first close the document in the designer, and then open the document directly in Word or Excel.

Caution

如果添加在可以打开文档时运行的 VBA 代码,在极少数情况下,此代码可能损坏文档,或或使文档无法在设计器中打开。If you add VBA code that runs when the document is opened, in rare cases this code might corrupt the document or prevent it from opening in the designer.

在发布或安装文件夹中In the publish or installation folder

在某些情况下,可能适合将 VBA 代码添加到发布文件夹或安装文件夹中的文档。In some cases, it might be suitable to add the VBA code to the document in the publish or installation folder. 例如,如果 VBA 代码是由其他开发人员在未安装 Visual Studio 的计算机上编写和测试的,你可以选择此选项。For example, you might choose this option if the VBA code is written and tested by a different developer on a computer that does not have Visual Studio installed.

如果用户直接从发布文件夹安装解决方案,则你必须在每次发布解决方案时将 VBA 代码添加到文档。If users install the solution directly from the publish folder, you must add the VBA code to the document every time you publish the solution. 发布解决方案时,Visual Studio 将覆盖位于发布位置中的文档。Visual Studio overwrites the document in the publish location when you publish the solution.

如果用户从发布文件夹以外的安装文件夹安装解决方案,则可以避免在每次发布解决方案时向文档中添加 VBA 代码。If users install the solution from an installation folder that is different from the publish folder, you can avoid adding the VBA code in the document every time you publish the solution. 准备将发布更新从发布文件夹移动到安装文件夹时,请将除文档之外的所有文件复制到安装文件夹中。When a publish update is ready to be moved from the publish folder to the installation folder, copy all of the files to the installation folder except for the document.

在最终用户计算机上On the end-user computer

如果最终用户是 VBA 开发人员并且将要调入你在文档级自定义项中提供的服务,则可以告诉他们如何通过在其文档副本中使用 CallVSTOAssembly 属性或 GetManagedClass 方法来调用你的代码。If the end users are VBA developers who are calling into services that you provide in the document-level customization, you can tell them how to call your code by using the CallVSTOAssembly property or the GetManagedClass method in their copies of the document. 当你将更新发布到解决方案时,不会覆盖最终用户计算机上的文档中的 VBA 代码,因为发布更新不会修改该文档。When you publish updates to the solution, VBA code in the document on the end-user computer will not be overwritten, because the document is not modified by publish updates.

由主机项属性执行的任务Tasks performed by the host item properties

使用 EnableVbaCallersReferenceAssemblyFromVbaProject 属性时,Visual Studio 会执行几组不同的任务。When you use the EnableVbaCallers and ReferenceAssemblyFromVbaProject properties, Visual Studio performs different sets of tasks.

“属性”EnableVbaCallers

当你在 Visual Basic 项目中将主机项的 EnableVbaCallers 属性设置为 True 时,Visual Studio 将执行以下任务:When you set the EnableVbaCallers property of a host item to True in a Visual Basic project, Visual Studio performs the following tasks:

  1. 向主机项类添加 ComClassAttributeComVisibleAttribute 特性。It adds the ComClassAttribute and ComVisibleAttribute attributes to the host item class.

  2. 替代主机项类的 GetAutomationObject 方法。It overrides the GetAutomationObject method of the host item class.

  3. 将主机项的 ReferenceAssemblyFromVbaProject 属性设置为 TrueIt sets the ReferenceAssemblyFromVbaProject property of the host item to True.

    当你将 EnableVbaCallers 属性重新设置为 False时,Visual Studio 将执行以下任务:When you set the EnableVbaCallers property back to False, Visual Studio performs the following tasks:

  4. ComClassAttribute 类删除 ComVisibleAttributeThisDocument 特性。It removes the ComClassAttribute and ComVisibleAttribute attributes from the ThisDocument class.

  5. 从主机项类删除 GetAutomationObject 方法。It removes the GetAutomationObject method from the host item class.

    Note

    Visual Studio 不会自动将 ReferenceAssemblyFromVbaProject 属性重新设置为 FalseVisual Studio does not automatically set the ReferenceAssemblyFromVbaProject property back to False. 可以使用 “属性” 窗口将此属性手动设置为 FalseYou can set this property to False manually by using the Properties window.

ReferenceAssemblyFromVbaProjectReferenceAssemblyFromVbaProject

当 Visual Basic 项目或 Visual C# 项目中任何主机项的 ReferenceAssemblyFromVbaProject 属性设置为 True时,Visual Studio 将执行以下任务:When the ReferenceAssemblyFromVbaProject property of any host item in a Visual Basic or Visual C# project is set to True, Visual Studio performs the following tasks:

  1. 为自定义程序集生成一个类型库,并将该类型库嵌入程序集。It generates a type library for the customization assembly and embeds the type library in the assembly.

  2. 在文档内的 VBA 项目中添加对以下类型库的引用:It adds a reference to the following type libraries in the VBA project in the document:

    • 自定义程序集的类型库。The type library for your customization assembly.

    • Microsoft Visual Studio Tools for Office Execution Engine 9.0 类型库。The Microsoft Visual Studio Tools for Office Execution Engine 9.0 Type Library. 此类型库包含在 Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime中。This type library is included in the Visual Studio Tools for Office RuntimeVisual Studio Tools for Office runtime.

    ReferenceAssemblyFromVbaProject 属性重新设置为 False时,Visual Studio 将执行以下任务:When the ReferenceAssemblyFromVbaProject property is set back to False, Visual Studio performs the following tasks:

  3. 文档内的 VBA 项目中删除类型库引用。It removes the type library references from the VBA project in the document.

  4. 从程序集中删除嵌入的类型库。It removes the embedded type library from the assembly.

疑难解答Troubleshoot

下表列出了一些常见错误以及修复错误的建议。The following table lists some common errors and suggestions for fixing the errors.

ErrorError 建议Suggestion
设置了 EnableVbaCallersReferenceAssemblyFromVbaProject 属性后,一条错误消息指明文档未包含 VBA 项目,或者你没有访问文档中的 VBA 项目的权限。After you set the EnableVbaCallers or ReferenceAssemblyFromVbaProject property, an error message states that the document does not contain a VBA project, or you do not have permission to access the VBA project in the document. 确保项目中的文档至少包含一个 VBA 宏、VBA 项目具有运行所需的足够的信任级别,并且 VBA 项目未受密码保护。Ensure that the document in the project contains at least one VBA macro, the VBA project has sufficient trust to run, and the VBA project is not protected by a password.
设置了 “属性”ReferenceAssemblyFromVbaProject 属性后,一条错误消息指明缺少 GuidAttribute 声明或该声明已损坏。After you set the EnableVbaCallers or ReferenceAssemblyFromVbaProject property, an error message states that the GuidAttribute declaration is missing or corrupted. 确保声明位于项目中的 AssemblyInfo.cs 或AssemblyInfo文件中,并且此特性设置为有效的 GUID。 GuidAttributeEnsure that the GuidAttribute declaration is located in the AssemblyInfo.cs or AssemblyInfo.vb file in your project, and that this attribute is set to a valid GUID.
设置了 “属性”ReferenceAssemblyFromVbaProject 属性后,一条错误消息指明 AssemblyVersionAttribute 指定的版本号无效。After you set the EnableVbaCallers or ReferenceAssemblyFromVbaProject property, an error message states that the version number specified by the AssemblyVersionAttribute is not valid. 确保项目中AssemblyVersionAttributeAssemblyInfo.csAssemblyInfo文件中的声明设置为有效的程序集版本号。Ensure that the AssemblyVersionAttribute declaration in the AssemblyInfo.cs or AssemblyInfo.vb file in your project is set to a valid assembly version number. 有关有效的程序集版本号的信息,请参见 AssemblyVersionAttribute 类。For information about valid assembly version numbers, see the AssemblyVersionAttribute class.
重命名自定义程序集后,调入自定义程序集的 VBA 代码将停止工作。After you rename the customization assembly, VBA code that calls into the customization assembly stops working. 如果在向 VBA 代码公开自定义程序集之后更改其名称,则文档中的 VBA 项目与自定义程序集之间的链接将断开。If you change the name of the customization assembly after you expose it to VBA code, the link between the VBA project in the document and your customization assembly is broken. 若要修复此问题,请将项目中的 ReferenceFromVbaAssembly 属性更改为 False ,并随后更改回 True,然后将 VBA 代码中对旧程序集名称的任何引用替换为新程序集名称。To fix this issue, change the ReferenceFromVbaAssembly property in your project to False and then back to True, and then replace any references to the old assembly name in the VBA code with the new assembly name.

请参阅See also