全局创建对象实例

发布于 2024-08-11 11:05:30 字数 1106 浏览 4 评论 0原文

这可能是我应该知道的事情,但我对此感到困惑。 我正在尝试创建一些对象并能够全局访问和修改这些对象。

我尝试创建一个公共模块并在其中声明一些对象。 我可以从另一个子程序访问这些对象,但是在构建和运行进程并尝试修改这些对象后,我收到异常错误。

如果我在公共类 Form1 中声明该对象,也会发生同样的情况。例如这样:

Public Class Form1
Public appWord = New Microsoft.Office.Interop.Word.Application
Public wordDoc as Microsoft.Office.Interop.Word.Document

现在,在我的按钮中,我得到了这个:

wordDoc = appWord.Documents.Open("c:\temp\test.dot")
appWord.Quit()
appword = Nothing

在构建项目并按下按钮后,我收到此错误:

Type mismatch. (Exception from HRESULT: 0x80020005(DISP_E_TYPEMISMATCH))

我想做的是启动 Winword.exe,然后在其中加载模板并从模板中读取一些宏。然后我希望能够替换应用程序中的宏代码,然后将更改存储到宏中。我有一个公共子程序,它读取宏并将文本放入文本框中。然后我有一个单独的公共子来进行替换。然而,我正在尝试找出一种巧妙的方法来一次性创建这些对象,这样我就不必一遍又一遍地这样做。

编辑:如果我在与按钮相同的子中声明对象,则不会发生这种情况,如下所示:

Private sub Button1_Click
Dim appWord = New Microsoft.Office.Interop.Word.Application
Dim wordDoc as Microsoft.Office.Interop.Word.Document

wordDoc = appWord.Documents.Open("C:\temp\test.dot")
appWord.Quit()
End sub

编辑:我现在可以使用它了。没有错误,只是该文档不存在..我是个白痴:)

this is probably something I should know, but I'm puzzled by this.
I'm trying to create some objects and being able to access and modify these globally.

I tried to create a Public Module and declare a few objects in this.
I am able to access these from another sub, but I get an exception error when after building and runing the process and trying to modify these object.

The same thing happens if I declare the object in the Public Class Form1. For example like this:

Public Class Form1
Public appWord = New Microsoft.Office.Interop.Word.Application
Public wordDoc as Microsoft.Office.Interop.Word.Document

Now, in my button, I've got this:

wordDoc = appWord.Documents.Open("c:\temp\test.dot")
appWord.Quit()
appword = Nothing

After I've built the project and press the button, I get this error:

Type mismatch. (Exception from HRESULT: 0x80020005(DISP_E_TYPEMISMATCH))

What I'm trying to do is to start a Winword.exe and then load a template in that and read some macros from the template. Then I want to be able to replace the macrocode from my application and then store the changes to the macro. And I've got a Public sub which reads the macro and puts the text in a textbox. And then I've got a separate Public Sub which does the replacement. However, I'm trying to figure out a clever way of creating these objects one time so I don't have to do it over and over again.

Edit: This doesn't happen if I declare the object in the same sub as the button, like this:

Private sub Button1_Click
Dim appWord = New Microsoft.Office.Interop.Word.Application
Dim wordDoc as Microsoft.Office.Interop.Word.Document

wordDoc = appWord.Documents.Open("C:\temp\test.dot")
appWord.Quit()
End sub

Edit: I got it working now. There wasn't an error, it was just that the document didn't exist.. I'm an idiot :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再见回来 2024-08-18 11:05:30

您遇到的问题与您声明“全局变量”的方式无关。这是一个 Dispatch 接口错误,告诉您传递的类型不是互操作类所期望的类型,也就是说,对 Open 的调用可能是错误的。根据 此引用 应通过引用传递(因此文字字符串无效)。

The problem you're getting has nothing to do with the way you declare your "globals". It's a Dispatch interface error telling you the type you're passing is not what the interop class is expecting, that is, the call to Open is probably wrong. According to this reference it should be passed by reference (so a literal string is not valid).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文