我的示例程序
//创建一个新的Microsoft Word应用程序对象 Microsoft.Office.Interop.Word.Application字=新的Microsoft.Office.Interop.Word.Application(); word.Visible = true; // C#没有可选参数,因此我们需要一个虚拟值 对象oMissing = System.Reflection.Missing.Value; //获取指定目录下的Word文件列表 DirectoryInfo dirInfo =新的DirectoryInfo(@“ C:\ temp”); FileInfo [] wordFiles = dirInfo.GetFiles(“ *。doc”); //word.Visible = false; //word.ScreenUpdating = false; foreach(wordFiles中的FileInfo wordFile) { 如果(!wordFile.FullName.Contains(“ $”)) { //转换为Open方法的对象 对象文件名=(Object)wordFile.FullName; //将虚拟值用作可选参数的占位符 Microsoft.Office.Interop.Word.Document doc = word.Documents.Open(ref filename,ref oMissing, ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing, ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing, ref oMissing,ref oMissing,ref oMissing,ref oMissing); //doc.Activate(); 对象是什么= WdGoToItem.wdGoToPage; 对象,其中= WdGoToDirection.wdGoToFirst; 对象计数= 1; 范围startRange = word.Selection.GoTo(ref what,ref which,ref count,ref oMissing); 对象count2 =(int)count +1; 范围endRange = word.Selection.GoTo(ref what,ref which,ref count2,ref oMissing); endRange.SetRange(startRange.Start,endRange.End-1); endRange.Select(); word.Selection.Copy(); //word.Documents.Close(); //word.Quit(); word.Documents.Add(); word.Selection.Paste(); //Microsoft.Office.Interop.Word.Application word1 =新的Microsoft.Office.Interop.Word.Application(); 对象outputFileName = wordFile.FullName.Replace(“。docx”,“ .pdf”); 对象fileFormat = WdSaveFormat.wdFormatPDF; //将文档保存为PDF格式 word.ActiveDocument.SaveAs(ref outputFileName, ref fileFormat,ref oMissing,ref oMissing, ref oMissing,ref oMissing,ref oMissing,ref oMissing, ref oMissing,ref oMissing,ref oMissing,ref oMissing, ref oMissing,ref oMissing,ref oMissing,ref oMissing); //关闭Word文档,但保持Word应用程序打开。 //必须将doc强制转换为_Document类型,以便找到 //正确的Close方法。 对象saveChanges = WdSaveOptions.wdDoNotSaveChanges; word.Documents.Close(ref saveChanges,ref oMissing,ref oMissing); doc = null; releaseObject(doc); } } //必须将单词强制转换为_Application类型,以便找到 //正确的Quit方法。 ((_Application)word).Quit(ref oMissing,ref oMissing,ref oMissing); 字=空; releaseObject(word);
另一种尝试循环并检查当前页面是否包含特定文本的尝试。但是两者都不起作用。
long pageCount = doc.ComputeStatistics(Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages); 对象是什么= WdGoToItem.wdGoToPage; 对象,其中= WdGoToDirection.wdGoToFirst; object count = 0;
object count2 = (int)count + 1;
for (long i = 1; i < pageCount; i++)
{
count = (int)count + 1;
Range startRange = word.Selection.GoTo(ref what, ref which, ref count, ref oMissing);
count2 = (int)count + 1;
Range endRange = word.Selection.GoTo(ref what, ref which, ref count2, ref oMissing);
endRange.SetRange(startRange.Start, endRange.End - 1);
endRange.Select();
word.Selection.Copy();
word.Documents.Add();
word.Selection.Paste();
if (word.Selection.Find.Execute("Something"))
{
word.Selection.Copy();
word.Documents.Add();
word.Selection.Paste();
}
}
感谢任何帮助。预先谢谢你。
我需要使用C#和office Interop库阅读多个单词文档,每个单词文档具有多个页面,请检查每个页面是否包含特定单词,如果是,则添加所有包含相同单词的页面...