Word实例在VB.net程序中立即打开 [英] Word instance is opening immediately in VB.net program

查看:186
本文介绍了Word实例在VB.net程序中立即打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个程序,除其他外,检查是否存在Word文档,如果存在,则打开它,或者如果已经打开则继续处理。



但是,一旦我启动程序,就会打开Word的一个实例,我可以在任务管理器中看到这个,并且当我稍后在程序中打开我的文档时,并不会使用这个实例。因此,我最终可能会同时运行六个Word进程...当我完成我的文档并关闭它时,其余的这些Word实例都很乐意继续运行!!!



有人能告诉我是什么导致这种情况以及如何阻止它发生,我需要在这里添加任何代码,我不确定甚至执行任何我可以告诉你的内容! !!

解决方案

根据您的描述,要检查的事情很少:



- 而不是设置一个断点并运行应用程序,使用 Debug / Step into 转到开头执行的每一行。通过这种方式,您可以看到开头所有代码的执行情况



- 打开项目属性并从调试表中检查您是否已定义启动word应用程序调试开始的时候。


这...



你似乎要定义一个变量Word互操作并立即实例化。看看行



  Dim  myMSWord  As   Word.Application 





如果您取出New关键字,则不应再进行创建。这同样适用于该文件。



加上这个...



由于Word实例不再在开头创建,因此您需要在使用前在代码中创建它。请考虑以下事项。



 myMSWord =  Word.Application 





...解决了我的问题,非常感谢Mika Wendelius的帮助: - )


I have written a program that, amongst other things, checks if a Word document exists and if it does, opens it or just continues processing if it is already open.

However, as soon as I start the program, an instance of Word is opened, I can see this in Task Manager, and it's not as if this instance is used when I open my document later in the program. Therefore I can end up with half-a-dozen Word processes running at the same time ... when I finish with my document & close it, the rest of these Word instances just happily continue running !!!

Can somebody tell me what causes this and how I can stop it from happening, is there any code I need to add here, I'm not sure anything even executes that I could show you !!!

解决方案

Based on your description, few things to check:

- Instead of setting a breakpoint and running the application, use Debug / Step into to go to each line that is executed in the beginning. This way you see the execution of all the code in the beginning

- open the project properties and from the debug sheet check that you haven't defined to start word application when the debugging starts.


This ...

You seem to define a variable for Word interop and instantiate it immediately. Have a look at line

Dim myMSWord As New Word.Application



If you take out New keyword, the creation should not happen anymore. The same applies to the document.

Plus this ...

since the Word instance isn't created in the beginning anymore, you need to create it in the code before using. Consider the following.

myMSWord = New Word.Application



... has resolved my problem, many thanks to Mika Wendelius for his help :-)


这篇关于Word实例在VB.net程序中立即打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆