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