简体   繁体   中英

Error occur during using backgroundworker in ms document C#

I am trying to use a progress bar for a loop operation progress in a MS word document, so I used the backgroundworker to update the progress bar during the loop operation as shown in the following code.

  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//}
      }
  }
 }

The line that occurs the error is the following line at the Loop_Opt() class:

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

The error message is as follow :

      [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)).]

Thanks in advance

After a long time of researches and trials, I don't think that there is an easy solution for this with using the Back ground worker control.

So I used the ordinary way to update the progress bar instead of the Back ground worker control as shown in the following code, it works fine but not with the same efficient of the background worker control.

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

Office applications use the single threaded apartment model (STA). You shouldn't use them on secondary threads. Moreover, the Globals.Factory.GetVstoObject method can be used in the VSTO based add-ins only.

I'd recommend using the Open XML SDK if deal with open XML documents only. Or use any other third-party components.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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