Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 在Word Interop中调用函数时,库中的类名被损坏_Vb.net_Visual Studio_Dll_Office Interop_Class Library - Fatal编程技术网

Vb.net 在Word Interop中调用函数时,库中的类名被损坏

Vb.net 在Word Interop中调用函数时,库中的类名被损坏,vb.net,visual-studio,dll,office-interop,class-library,Vb.net,Visual Studio,Dll,Office Interop,Class Library,我的应用程序中的类库出现了一些奇怪的问题。我们有几十个ComVisible类,最近我发现通过COM公开的某个类的名称不再像以前那样常见 我能够在一个小样本程序中复制该问题,该问题与Microsoft Word Interop中涉及的一行相关。具体来说,我有一个类窗口。通常,该类作为“窗口”可见,但如果我在Word Interop中引用Document.ActiveWindow属性,则该类将作为“TestLibrary\u Window”可见。在我的实际应用程序中,我有100个引用窗口的地方,我不

我的应用程序中的类库出现了一些奇怪的问题。我们有几十个ComVisible类,最近我发现通过COM公开的某个类的名称不再像以前那样常见

我能够在一个小样本程序中复制该问题,该问题与Microsoft Word Interop中涉及的一行相关。具体来说,我有一个类窗口。通常,该类作为“窗口”可见,但如果我在Word Interop中引用Document.ActiveWindow属性,则该类将作为“TestLibrary\u Window”可见。在我的实际应用程序中,我有100个引用窗口的地方,我不想将它们全部更改为TestLibrary\u窗口,而且我想通过引用一个属性来了解可能发生的情况,即更改我的库如何公开自己

通过使用一个程序OleWoo()查看TLB文件,我可以很容易地演示结果。请注意,在结果1中,您会看到一个用于coclass窗口的条目,但在结果2中,您会看到一个用于coclass TestLibrary_窗口的条目。结果1是我期望TLB遇到的情况,如果代码中的故障行被注释,这就是我收到的结果。结果2是我取消注释故障行时得到的结果

下面是一个与我的问题重复的最小实现。如果TestClass中的注释行被保留注释,那么我没有问题,但是如果我取消注释该行,我就有问题。请注意,在我的示例代码中,我不需要Window类中的任何代码来演示该问题

文件1:TestClass.vb

Imports System.Runtime.InteropServices

<ComVisible(True)>
Public Class TestClass
    Public Sub testFunction()
        Dim oWord As Microsoft.Office.Interop.Word.Application = CreateObject("Word.Application")
        Dim oDoc As Microsoft.Office.Interop.Word.Document = oWord.Documents.Open("c:\temp\test.docx")

        'trouble line
        'oDoc.ActiveWindow.View.TableGridlines = True

        oDoc.Save()
    End Sub

End Class
结果2:错误的TLB文件

// Generated .IDL file (by OleWoo)
[
  uuid(b2effb21-a565-4092-bc8f-b92aa429952a),
  version(1.0),
  custom(90883f05-3d28-11d2-8f17-00a0c9a6186d, "TestLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=974b55dd4adecdf1")
]
library TestLibrary
{
    // Forward declare all types defined in this typelib
    dispinterface _TestClass
    interface _TestClass
    dispinterface _TestLibrary_Window
    interface _TestLibrary_Window
    [
      uuid(eb22957e-07c0-34b2-b813-48d0e9376d35)
    ]
    coclass TestClass {
        [default] interface _TestClass#i;
        interface _Object#i;
    };

    [
      uuid(2266afaa-2145-3508-bb4b-9f8579112b14)
    ]
    coclass TestLibrary_Window {
        [default] interface _TestLibrary_Window#i;
        interface _Object#i;
    };

    [
      uuid(a13ff8b0-ac7c-33e5-b0f3-5366304512ac),
      hidden,
      dual,
      oleautomation
    ]
    interface _TestClass : IDispatch#i {

    };

    [
      uuid(b81f8ed9-9e71-3248-b3a9-b7a104b3a597),
      hidden,
      dual,
      oleautomation
    ]
    interface _TestLibrary_Window : IDispatch#i {

    };

};

正如@TnTinMn在上面的评论中提到的,我遇到的问题确实与Microsoft.Office.Interop.Word引用中的“嵌入互操作类型”有关。通过将此选项切换为false,我的TLB现在正在按预期创建。

源代码管理是这里的基本工具,您希望将当前源代码与没有此问题的提交区分开来。查找添加的标识符(也称为“窗口”)。例如,可能是枚举成员。@HansPassant我能够在一个小样本程序中复制它。上面列出的两个类的内容是该示例程序中涉及的全部代码。我认为这是一致的,因为您正在“嵌入互操作类型”,并且MS Word
\u Window
接口与为您的Window类生成的接口冲突。阻力最小的方法是声明您自己的
IWindow
com可见接口,您的
Window
类将实现该接口,并避免代码生成命名冲突。@tnt这解决了我的问题,谢谢
// Generated .IDL file (by OleWoo)
[
  uuid(b2effb21-a565-4092-bc8f-b92aa429952a),
  version(1.0),
  custom(90883f05-3d28-11d2-8f17-00a0c9a6186d, "TestLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=974b55dd4adecdf1")
]
library TestLibrary
{
    // Forward declare all types defined in this typelib
    dispinterface _TestClass
    interface _TestClass
    dispinterface _Window
    interface _Window
    [
      uuid(eb22957e-07c0-34b2-b813-48d0e9376d35)
    ]
    coclass TestClass {
        [default] interface _TestClass#i;
        interface _Object#i;
    };

    [
      uuid(2266afaa-2145-3508-bb4b-9f8579112b14)
    ]
    coclass Window {
        [default] interface _Window#i;
        interface _Object#i;
    };

    [
      uuid(a13ff8b0-ac7c-33e5-b0f3-5366304512ac),
      hidden,
      dual,
      oleautomation
    ]
    interface _TestClass : IDispatch#i {

    };

    [
      uuid(b81f8ed9-9e71-3248-b3a9-b7a104b3a597),
      hidden,
      dual,
      oleautomation
    ]
    interface _Window : IDispatch#i {

    };

};
// Generated .IDL file (by OleWoo)
[
  uuid(b2effb21-a565-4092-bc8f-b92aa429952a),
  version(1.0),
  custom(90883f05-3d28-11d2-8f17-00a0c9a6186d, "TestLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=974b55dd4adecdf1")
]
library TestLibrary
{
    // Forward declare all types defined in this typelib
    dispinterface _TestClass
    interface _TestClass
    dispinterface _TestLibrary_Window
    interface _TestLibrary_Window
    [
      uuid(eb22957e-07c0-34b2-b813-48d0e9376d35)
    ]
    coclass TestClass {
        [default] interface _TestClass#i;
        interface _Object#i;
    };

    [
      uuid(2266afaa-2145-3508-bb4b-9f8579112b14)
    ]
    coclass TestLibrary_Window {
        [default] interface _TestLibrary_Window#i;
        interface _Object#i;
    };

    [
      uuid(a13ff8b0-ac7c-33e5-b0f3-5366304512ac),
      hidden,
      dual,
      oleautomation
    ]
    interface _TestClass : IDispatch#i {

    };

    [
      uuid(b81f8ed9-9e71-3248-b3a9-b7a104b3a597),
      hidden,
      dual,
      oleautomation
    ]
    interface _TestLibrary_Window : IDispatch#i {

    };

};