Search code examples
c#excelvb.netprocess.startwaitforexit

Process.Start wait for excel to exit error


Here is my code:

Dim sTemplateSharedPath As String = sSharedDrive & ":\Excel Templates\ImportTemplate.xlsx"     
Dim objProcess As System.Diagnostics.Process
                    Try
                        objProcess = New System.Diagnostics.Process()
                        objProcess.StartInfo.FileName = sTemplateSharedPath
                        objProcess.Start()
                        objProcess.WaitForExit()
                    Catch
                        MessageBox.Show("Could not start process " & sTemplateSharedPath, "Error")
                    End Try

Excel opens up the file I requested, but the system crashes at the objProcess.WaitForExit() part.The error I get is the following:

No process is associated with this object.

I want the system to open the template, have the user add his/her data, save and close MS Excel. Once the system detects the process is not running anymore, point to the sTemplateSharedPath and import said changes into the system. It's fine if the UI of the app is non responsive while the user is editing the excel document.

I have added the following to the code above before I start the process:

objProcess.StartInfo.UseShellExecute = false

But I then get this error:

the specified executable is not a valid application for this os platform

I do hope someone will be able to asssist me with this issue.

Thanks in advance.


Solution

  • Did you try to open an excel instance first with the file as parameter?

    Dim sSharedDrive = "C"
        Dim sTemplateSharedPath As String = "excel.exe"
        Dim FileParam As String = ControlChars.Quote + sSharedDrive + ":\test new\test.xlsx" + ControlChars.Quote
        Dim objProcess As System.Diagnostics.Process
        Try
            objProcess = New System.Diagnostics.Process()
            objProcess.StartInfo.FileName = sTemplateSharedPath
            objProcess.StartInfo.Arguments = FileParam
            objProcess.Start()
            objProcess.WaitForExit()
        Catch
            Console.WriteLine("Could not start process " & sTemplateSharedPath, "Error")
            Console.ReadLine()
        End Try
    

    I dont know if the .xlsx filetype is definied in your os. Can you open the .xlsx manually ?