简体   繁体   English

Word运行的Process.Start无法等待退出

[英]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. 我有一种方法,可以在Word中打开Word文档,并在完成之前等待Word退出。

If word is not already running all works well. 如果单词尚未运行,则一切正常。

If word is running the process exits immediately so I can;t wait for exit. 如果word正在运行,则该过程将立即退出,因此我无法等待退出。

Any ideas how I can wait for the document to close if word is already running? 有什么想法可以在Word已经运行的情况下如何等待文档关闭?

This is on Windows 8.1 这是在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();
            }
        }
    }

you can get the current running Word Process and attach to the events 您可以获得当前正在运行的Word Process并附加到事件

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];
    }
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 您可以通过后台线程进行循环,以不断测试Word是否正在运行,并在未运行时触发事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM