English 中文(简体)
Creating object instances globally
原标题:

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:	emp	est.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:	emp	est.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 :)

最佳回答

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).

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签