我正在研究需要启动Microsoft Word的应用程序,然后在用户关闭Word时继续。下面的代码应该可以工作,但它不会。我得到一个“对象不设置到对象的实例”从VB.NET 2010启动并观看过程
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。这个过程启动了,但我不能再用线监视它了。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
[This Link](http://social.msdn.microsoft.com/Forums/en-US/vbinterop/thread/9629b7b1-b2fd-4575-8dd1-589681c1262c/)有几个可以使用的示例函数。它出现;一目了然,该线程上的Op能够捕获OnDocumentClosed事件。我建议在你发布的循环中使用事件。 – Smudge202 2011-04-19 14:46:24