try
{
if (fbd.ShowDialog() == DialogResult.OK)
{
string[] files = Directory.GetFiles(fbd.SelectedPath);
string text = "";
pb.Maximum = (files.Length) - 1;
Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object nullobj = System.Reflection.Missing.Value;
for (int i = 0; i < files.Length; i++)
{
pb.Value = i;
Microsoft.Office.Interop.Word.ApplicationClass wordObject = new ApplicationClass();
object file = files[i]; //this is the path
object nullobject = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Document docs = wordObject.Documents.Open
(ref file, ref nullobject, ref nullobject, ref nullobject,
ref nullobject, ref nullobject, ref nullobject, ref nullobject,
ref nullobject, ref nullobject, ref nullobject, ref nullobject,
ref nullobject, ref nullobject, ref nullobject, ref nullobject
);
docs.ActiveWindow.Selection.WholeStory();
docs.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
text += data.GetData(DataFormats.Text).ToString()+Environment.NewLine;
}
mytxt = text;
((_Application)wordApp).Quit(ref nullobj,
ref nullobj,
ref nullobj);
wordApp = null;
//GC.Collect();
wlab.Text = (files.Length).ToString() + " Files Selected";
wlab.Visible = true;
MessageBox.Show("Text Selection Successful!", "Files Selected", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception exc)
{
MessageBox.Show("Please make sure!\nYour Folder should only contain Microsoft Office Word files!", "Files Selection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
pb.Value = 0;
}
所有工作正常但问题是,在第一次阅读word文件后它没有问题,但是当我想在第二次读取任何word文件时,它挂起我的办公室应用程序说&#34; THE文件正在使用&#34;
有人可以帮我识别我的问题或者可以提出一些建议。 提前致谢