Steve Chadbourne
Steve Chadbourne

Reputation: 6953

Process.Start with Word running fails to wait for exit

I have a method that allows a word document to be opened in word and waits for word to exit before completing.

If word is not already running all works well.

If word is running the process exits immediately so I can;t wait for exit.

Any ideas how I can wait for the document to close if word is already running?

This is on Windows 8.1

    public void ShowExternalReference(string externalRef, bool waitForCompletion)
    {
        if (externalRef.NotEmpty())
        {
            var pInfo = new ProcessStartInfo {FileName = externalRef};

            // Start the process.
            Process p = Process.Start(pInfo);

            if (waitForCompletion)
            {
                // Wait for the window to finish loading.
                p.WaitForInputIdle();

                // Wait for the process to end.
                p.WaitForExit();
            }
        }
    }

Upvotes: 3

Views: 2511

Answers (2)

Chad Dienhart
Chad Dienhart

Reputation: 5204

you can get the current running Word Process and attach to the events

I got some info here

Here is an example for attaching and putting text into the doc...

Hope this helps.

using Word = Microsoft.Office.Interop.Word;


namespace WindowsFormsApplication1
{

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load_1(object sender, EventArgs e)
    {
    }

    public void ShowExternalReference(string externalRef, bool waitForCompletion)
    {
        if (externalRef.Length > 0)
        {
            var pInfo = new ProcessStartInfo { FileName = externalRef };
            bool isrunning = false;
            Process [] pList = Process.GetProcesses();
            foreach(Process x in pList)
            {
                if( x.ProcessName.Contains("WINWORD"))
                {
                    isrunning = true;
                    Word.Application myWordApp =
                          System.Runtime.InteropServices.Marshal.GetActiveObject(
                          "Word.Application") as Word.Application;
                    if(myWordApp.ActiveDocument.FullName.Contains(externalRef))
                        // do something
                        myWordApp.ActiveDocument.Content.Text = " already open";
                }
            }
            if(!isrunning)
            {
                // Start the process.
                Process p = Process.Start(pInfo);

                if (waitForCompletion)
                {
                    // Wait for the window to finish loading.
                    p.WaitForInputIdle();

                    // Wait for the process to end.
                    p.WaitForExit();
                }
            }
        }
    }


    private void button1_Click(object sender, EventArgs e)
    {
        string myWordFile = @"C:\Temp\test.docx";
        ShowExternalReference(myWordFile, true);
    }


    private void listView1_ItemChecked(object sender, ItemCheckEventArgs e)
    {
        listView1.Items[e.Index].Group = listView1.Groups[e.NewValue == CheckState.Checked ? 0 : 1];
    }

Upvotes: 2

Jonesopolis
Jonesopolis

Reputation: 25370

Process[] pname = Process.GetProcessesByName("winword.exe");
if(pname.Length == 0)
{
   //not running..
}

you could loop this through a background thread perhaps to continually test if Word is running, and fire an event back when it isn't

Upvotes: 0

Related Questions