我正在尝试使用进度条来处理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()类的以下行:
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)).]
提前致谢
答案 0 :(得分:1)
经过长时间的研究和试验,我不认为使用Back ground worker control可以轻松解决这个问题。
所以我使用普通的方式来更新进度条而不是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();
}
}
答案 1 :(得分:0)
Office应用程序使用单线程单元模型(STA)。您不应该在辅助线程上使用它们。此外,Globals.Factory.GetVstoObject方法只能在基于VSTO的加载项中使用。
如果仅处理打开的XML文档,我建议使用Open XML SDK。或使用任何其他第三方组件。