在ms文档C#中使用backgroundworker时出错

Error occur during using backgroundworker in ms document C#

我想在MS word文档中使用进度条来表示循环操作进度,所以我在循环操作过程中使用backgroundworker来更新进度条,如下代码所示。

  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using Word = Microsoft.Office.Interop.Word;
  using Microsoft.Office.Tools.Word;

namespace prog
{
public partial class PGB : Form
{
    public PGB()
    {
        InitializeComponent();
    }

    private static int Mx;

    private void PGB_Load(object sender, EventArgs e)
    {
        Mx = 100;

        PG.Maximum = Mx;
        PG.Step = 1;
        PG.Value = 0;
        BGW.RunWorkerAsync();
    }

    private void BGW_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int j = 0; j <= Mx-1; j++)
        {
            Loop_Opt(j+1);
            BGW.ReportProgress((j));
        }
    }

    private void BGW_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        PG.Value = e.ProgressPercentage;
    }

    private void BGW_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("Done");
    }

    public static void Loop_Opt(int n)
    {
        Word.Application wordApp;
        Word.Document oDoc = null;

        wordApp = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
        oDoc = wordApp.ActiveDocument;
        Document DD = Globals.Factory.GetVstoObject(oDoc);

        for (int i = 1; i <= oDoc.Bookmarks.Count; i++)
        {//loop operation//}
      }
  }
 }

发生错误的行是 Loop_Opt() class 中的以下行:

   Document DD = Globals.Factory.GetVstoObject(oDoc);

报错信息如下:

      [Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IHostItemFactoryNoMAF'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{A0885C0A-33F2-4890-8F29-25C8DE7808F1}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).]

提前致谢

Office 应用程序使用单线程单元模型 (STA)。你不应该在辅助线程上使用它们。此外,Globals.Factory.GetVstoObject 方法只能在基于 VSTO 的加载项中使用。

如果只处理打开的 XML 文档,我建议使用 Open XML SDK。或者使用任何其他第三方组件。

经过长时间的研究和尝试,我认为使用Back ground worker控件没有一个简单的解决方案。

所以我使用普通方式更新进度条,而不是如下代码所示的后台工作者控件,它工作正常但与后台工作者控件的效率不一样。

public void Loop_Opt()
{
    Word.Application wordApp;
    Word.Document oDoc = null;

    wordApp = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
    oDoc = wordApp.ActiveDocument;
    Document DD = Globals.Factory.GetVstoObject(oDoc);

    PG.Maximum = oDoc.Bookmarks.Count;
    PG.Step = 1;
    PG.Value = 1;

    for (int i = 1; i <= oDoc.Bookmarks.Count; i++)
    {
       //loop operation//
       PG.PerformStep();
       Thread.Sleep(100);
       Application.DoEvents();
    }
  }