c# - Process.Start with Word 运行失败等待退出

标签 c#

我有一个方法可以让一个word文档在word中打开,然后等待word退出后再完成。

如果 word 尚未运行,则一切正常。

如果 word 正在运行,进程会立即退出,所以我不能等待退出。

如果 word 已经在运行,我有什么想法可以等待文档关闭吗?

这是在 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();
            }
        }
    }

最佳答案

可以获取当前运行的Word Process并附加到事件

我得到了一些信息 here

这是一个附加文本并将其放入文档的示例...

希望这对您有所帮助。

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];
    }

关于c# - Process.Start with Word 运行失败等待退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19575961/

相关文章:

c# - 当前上下文中不存在名称 'Installers'

c# - 表单提交后 MVC 从 View 返回填充模型

c# - 分离事件 (C#)

c# - ASP.Net MVC 多个列表从 Controller 返回

c# - 枚举 Linq 查询结果

c# - Azure Web 应用程序未从配置选项卡中获取值

c# - .NET:如何创建文件图标覆盖

c# - 始终在 Windows 应用程序的数据 GridView 中显示滚动条 - C#

C# 无法从 API 调用读取 json 响应

c# - 解析 CoDeSys .pro 文件