从 VB.NET 2010 启动并观看进程
我正在开发一个应用程序,需要启动 Microsoft Word,然后在用户关闭 Word 时恢复。下面的代码应该可以工作,但事实并非如此。我得到一个“对象未设置为对象的实例”,
1 Dim pInfo As New ProcessStartInfo
2 Dim P As New Process
3 pInfo.FileName = "C:\test\LLR.doc"
4 P = Process.Start(pInfo)
5 ''# Here is where it goes bad
6 P.WaitForInputIdle()
7 P.WaitForExit()
我将 p
放入监视窗口,它在第 2 行之后的监视中显示了一个 system.diagnostics.process
,但在第 4 行之后,它返回“NOTHING”。该进程已启动,但我无法再使用第 6 行和第 6 行监视它。 7. 这是 Visual Studio 2010 的“限制”还是我犯了操作错误? MS 帮助不显示 2010 版本中可用的进程(它在 Visual Studio 2005 和 Visual Studio 2008 中)。
--根据反馈进行编辑-最终解决方案
Private Function StartWord(ByVal NewFileName As String) As Boolean
MessageBox.Show("When you have finished editing the report, save and close word to complete operation")
Dim wapp As Application
wapp = New Microsoft.Office.Interop.Word.Application
wapp.Documents.Open(NewFileName)
wapp.Visible = True
wapp.WindowState = WdWindowState.wdWindowStateMaximize
wapp.Caption = "Large Loss Report"
Try
While wapp.Documents.Count > 0
System.Windows.Forms.Application.DoEvents()
End While
wapp.Quit()
Catch ex As Exception
End Try
Return True
End Function
I am working on an application that needs to launch Microsoft Word, and then resume when the user closes Word. The code below should work, but it does not. I get an 'object not set to an instance of an object'
1 Dim pInfo As New ProcessStartInfo
2 Dim P As New Process
3 pInfo.FileName = "C:\test\LLR.doc"
4 P = Process.Start(pInfo)
5 ''# Here is where it goes bad
6 P.WaitForInputIdle()
7 P.WaitForExit()
I put p
into the watch window and it shows a a system.diagnostics.process
in the watch after line2, but after line 4 it return to NOTHING. The process launches, but I can not monitor it any longer with lines 6 & 7. Is this a 'limitation' of Visual Studio 2010 or am I making an operator error? The MS Help does not show process available in the 2010 version (it is in Visual Studio 2005 and Visual Studio 2008).
--Edit based on feedback - final solution
Private Function StartWord(ByVal NewFileName As String) As Boolean
MessageBox.Show("When you have finished editing the report, save and close word to complete operation")
Dim wapp As Application
wapp = New Microsoft.Office.Interop.Word.Application
wapp.Documents.Open(NewFileName)
wapp.Visible = True
wapp.WindowState = WdWindowState.wdWindowStateMaximize
wapp.Caption = "Large Loss Report"
Try
While wapp.Documents.Count > 0
System.Windows.Forms.Application.DoEvents()
End While
wapp.Quit()
Catch ex As Exception
End Try
Return True
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Microsoft Word 是一个大程序。运行它的多个副本很快就会使普通消费者级别的机器崩溃。为了避免这种情况,Word 确保只运行一个实例来处理所有文档。所谓的单实例应用程序。
因此,如果您像这样启动 Word 并且 Word 已经在运行,那么您启动的第二个副本只会要求第一个实例打开文档。并立即退出。制作你的代码炸弹。这也会阻止您执行您想要执行的操作,您无法判断用户何时关闭第二个文档,只能判断她关闭所有文档的时间。一种不完美的解决方法是尝试定期打开 .doc 文件。只要 Word 打开,它就会被锁定。
Microsoft Word is a Big Program. Running several copies of it would quickly make the average consumer level machine keel over. To avoid this, Word makes sure that only one instance ever runs, taking care of all documents. A so-called single-instance app.
So if you start Word like you do and Word is already running then the 2nd copy you start only asks the 1st instance to open the document. And immediately exits. Making your code bomb. This also stops you from doing what you are trying to do, you cannot tell when the user closes the 2nd document, only when she closes all documents. One imperfect workaround would be to try to open the .doc file periodically. It is locked as long as Word has it opened.