none
Problem to start ms word 2013 and wait for exit! RRS feed

  • General discussion

  • Hi

    In my app, there is a section which extract some information from db, generate word doc, save on client machine and start ms word then wait for exit to save in db.

    this is code which works for ms word 2007 :

    FileInfo fInfo = new FileInfo(fullPath);
    ProcessStartInfo psi = new ProcessStartInfo(fullPath);
    Process p = Process.Start(psi);
    
    if (p == null)
    {
         // Word is already running, and handled the file above...
         p = Process.GetProcessesByName("WINWORD")[0];
    }
    
    if (p == null)
    {
         // Handle error case...
         // ie: Word may not be installed, or something else is handling this file...
    }
    
    p.WaitForExit(); // not works for word 2013!!!
    p.Close();
    
    // load new order and save to datarow                                
    
    // delete from disk
    File.Delete(fullPath);

    but in ms word 2013, after start ms word 2013, it does not stand on waitForExit method (whereas in ms word 2007, it blocked until the end user closed the word).

    where is the problem and how to solve it ?

    I'm using ms visual studio 2008 & .net 2.0

    thanks in advance


    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx

    • Moved by CoolDadTx Saturday, September 20, 2014 10:23 PM Office related
    Saturday, September 20, 2014 5:33 PM

All replies

  • Do you mean that it does not stop at WaitForExit if Word is already started before your program?

    Probably the processes are managed differently. Check if this experiment works in your case:

    Process p = Process.Start( psi );
    
    if( p.HasExited )
    {
        p = Process.GetProcessesByName( "WINWORD" )[0];
    }
    
    p.WaitForExit();
    p.Close();


    • Edited by Viorel_MVP Saturday, September 20, 2014 6:47 PM
    Saturday, September 20, 2014 6:46 PM
  • Thanks for reply

    I think it solved the first problem but I'm facing the same problem and this warning from word:

    Sorry, we couldn't find your file> Is it possible it was moved, renamed or deleted?
    (My genaretd dcox word file)

    Note that this warning only is for the first time and next time it works correctly or when I run my project I debug mode (line by line) irt works correctly!!!

    where is the problem ?


    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx

    Saturday, September 20, 2014 7:29 PM
  • Hi again

    sorry, after testing again, I think the first problem was not solved yet!!!

    after line p.WaitForExit(), next codes run immediately whereas ms word already opened!!!


    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx

    Saturday, September 20, 2014 7:35 PM
  • Invoking Word via the EXE isn't a reliable approach.  Office is a single instance app so if Word is already running then starting another instance is going to cause your code problems.  In most cases the instance you started will likely terminate while the already running instance will be brought to the front.  Additionally when a window is closed the process will continue running until all the other windows are closed.  So it isn't really reliable to associate the life of the process with the life of your doc. 

    In general you'll use the Word API to interact with Word.  If you need to invoke Word so the user can interact with it then the API may not work out as well for you.  I think this question may be better answered in the Office forums so I'll move it to there.  The Office experts may be able to tell you how to get this to work correctly for your scenario.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Saturday, September 20, 2014 10:23 PM
  • Hello Hamed,

    Why do you need to start an instance of the Word application?

    Did you consider using the Open XML SDK for generating Word documents based on the Db datа?

    Sunday, September 21, 2014 10:52 AM
  • Hi

    Why do you need to start an instance of the Word application?

    This code works for me for several years!! but I don't know why I'm facing the this problem for ms word 2013!

    I use word app, because it is user friendly and have many tools for end user(s)


    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx


    • Edited by Hamed_1983 Sunday, September 21, 2014 6:41 PM
    Sunday, September 21, 2014 6:39 PM
  • Are applications run with the same privilege level?

    Did you have a chance to check the list of running processes? Is the Word.exe process closed? Or does it continue running in the background after closing the window?

    Sunday, September 21, 2014 6:59 PM
  • Hi again!

    now, I've uninstalled word 2013 and install word 2007 and this code works correctly :

    FileInfo fInfo = new FileInfo(fullPath);
    ProcessStartInfo psi = new ProcessStartInfo(fullPath);
    Process p = Process.Start(psi);
    
    if (p == null)
    {
         // Word is already running, and handled the file above...
         p = Process.GetProcessesByName("WINWORD")[0];
    }
    
    if (p == null)
    {
         // Handle error case...
         // ie: Word may not be installed, or something else is handling this file...
    }
    
    p.WaitForExit(); // not works for word 2013!!!
    p.Close();
    
    // load new order and save to datarow                                
    
    // delete from disk
    File.Delete(fullPath);


    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx

    Sunday, September 21, 2014 7:35 PM