Excel VBA 和 C# DLL:实例化 WinForms 对象只能执行一次。第二次尝试有错误
Excel VBA and C# DLL: Instantiate WinForms Object can only be done once. At second try there is an error
我构建了一个包含 Winform 的 C# dll。我想从 Excel VBA 打开表格。第一次尝试总是成功的。它打开并执行我想让它执行的所有操作。然后我用右上角的红色X关闭它。
当我再次尝试打开它时,我总是收到错误消息:
"SetCompatibleTextRenderingDefault must be called before the first IWin32Window object is created in the application." 来源:System.Windows.Forms
WinForms class 是由 Visual Studio 生成的标准 class。我用不同的 class:
实例化它
namespace Schnittstellenvererbung
{
[ComVisible(true), Guid("5D16EABF-B89F-45A1-8E4D-ACFAA084BF6F")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface ISchnittstellenvererbung
{
string ShowForm1();
}
[ComVisible(true), Guid("6A9EF0BB-BFF3-456E-B025-CB6A25F81F59")]
[ProgId("Test.SchnittstellenvererbungExecuteProgram")]
[ClassInterface(ClassInterfaceType.None)]
public class ExecuteProgram : ISchnittstellenvererbung
{
public string ShowForm1()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 Oberflaeche;
Oberflaeche = new Form1();
Oberflaeche.ShowDialog();
}
catch(Exception e)
{
MessageBox.Show(e.Message + e.Source);
}
return "Done";
}
}
}
在Excel VBA中调用非常简单:
Sub test()
Dim y As String
Dim x As New Schnittstellenvererbung.ExecuteProgram
y = x.ShowForm1
End Sub
当我关闭 Excel 并重新打开它时,它在第一次尝试时再次运行,但第二次尝试再次失败。
我想知道为什么会出现这个错误,因为在实例化表单之前总是调用 SetCompatibleTextRenderingDefault 方法。
想法?
我在msdn中找到了解决办法。 Windows 表单在单独的线程中运行。示例代码是 VB,但转换为 C# 并不难。如果有人需要帮助,可以使用 teleriks 代码转换器进行转换。
链接:
MSDN 解决方案:https://msdn.microsoft.com/en-us/library/ms229591%28v=vs.110%29.aspx
我构建了一个包含 Winform 的 C# dll。我想从 Excel VBA 打开表格。第一次尝试总是成功的。它打开并执行我想让它执行的所有操作。然后我用右上角的红色X关闭它。
当我再次尝试打开它时,我总是收到错误消息: "SetCompatibleTextRenderingDefault must be called before the first IWin32Window object is created in the application." 来源:System.Windows.Forms
WinForms class 是由 Visual Studio 生成的标准 class。我用不同的 class:
实例化它namespace Schnittstellenvererbung
{
[ComVisible(true), Guid("5D16EABF-B89F-45A1-8E4D-ACFAA084BF6F")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface ISchnittstellenvererbung
{
string ShowForm1();
}
[ComVisible(true), Guid("6A9EF0BB-BFF3-456E-B025-CB6A25F81F59")]
[ProgId("Test.SchnittstellenvererbungExecuteProgram")]
[ClassInterface(ClassInterfaceType.None)]
public class ExecuteProgram : ISchnittstellenvererbung
{
public string ShowForm1()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 Oberflaeche;
Oberflaeche = new Form1();
Oberflaeche.ShowDialog();
}
catch(Exception e)
{
MessageBox.Show(e.Message + e.Source);
}
return "Done";
}
}
}
在Excel VBA中调用非常简单:
Sub test()
Dim y As String
Dim x As New Schnittstellenvererbung.ExecuteProgram
y = x.ShowForm1
End Sub
当我关闭 Excel 并重新打开它时,它在第一次尝试时再次运行,但第二次尝试再次失败。
我想知道为什么会出现这个错误,因为在实例化表单之前总是调用 SetCompatibleTextRenderingDefault 方法。
想法?
我在msdn中找到了解决办法。 Windows 表单在单独的线程中运行。示例代码是 VB,但转换为 C# 并不难。如果有人需要帮助,可以使用 teleriks 代码转换器进行转换。
链接:
MSDN 解决方案:https://msdn.microsoft.com/en-us/library/ms229591%28v=vs.110%29.aspx