首页 > 编程语言 >C# word 替换

C# word 替换

时间:2022-11-18 10:22:10浏览次数:43  
标签:Selection word C# object oMissing Find oWord ref 替换

private void CreateOneRiZhi(string newPath, string theDate, string info)
        {
            System.IO.File.Copy(System.Windows.Forms.Application.StartupPath + @"\template\项目日志.docx", newPath, true);

            object oMissing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application();
            oWord.Visible = true;
            Microsoft.Office.Interop.Word._Document oDoc;
            //oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            object oNewPath = (object)newPath;
            object readOnly = false;
            object isVisible = true;
            object objFalse = false;

            oDoc = oWord.Documents.Open(ref oNewPath, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            oDoc.Activate();

            object replaceAll = WdReplace.wdReplaceAll;
            oWord.Selection.Find.ClearFormatting();
            oWord.Selection.Find.Text = "a";
            oWord.Selection.Find.Replacement.ClearFormatting();
            oWord.Selection.Find.Replacement.Text = "项目日志";

            oWord.Selection.Find.Execute(
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);


            oWord.Selection.Find.ClearFormatting();
            oWord.Selection.Find.Text = "b";
            oWord.Selection.Find.Replacement.ClearFormatting();
            oWord.Selection.Find.Replacement.Text = theDate;

            oWord.Selection.Find.Execute(
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);



            oWord.Selection.Find.ClearFormatting();
            oWord.Selection.Find.Text = "c";
            oWord.Selection.Find.Replacement.ClearFormatting();
            oWord.Selection.Find.Replacement.Text = info;

            oWord.Selection.Find.Execute(
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);



            object format = MSWord.WdSaveFormat.wdFormatDocument;
            //将wordDoc 文档对象的内容保存为DOC 文档,并保存到path指定的路径
            oDoc.Save();
            //oDoc.SaveAs(ref oNewPath, ref format, 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);
            //关闭wordDoc文档
            oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
            //关闭wordApp组件对象
            oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
        }

 

标签:Selection,word,C#,object,oMissing,Find,oWord,ref,替换
From: https://www.cnblogs.com/adamgq/p/16902332.html

相关文章

  • ArcGIS简化线Smooth和Generalize的区别
    generalize 抽稀处理工具 smooth 光滑处理工具参考:https://zhuanlan.zhihu.com/p/429857468参考2:https://www.cnblogs.com/hans_gis/archive/2010/11/15/1877605.html......
  • Oracle重建索引
    创建表createtablestudent(student_idnumber,namevarchar2(240))tablespaceschool_data;创建索引createindexstudent_n1onstuden(name);查看......
  • nacos的使用
    一:下载nacos打开github搜索nacos,选择历史版本,建议下载1.4版本的,较稳定https://github.com/alibaba/nacos   二:下载完后解压文件,两种方式......
  • MBR10200FAC-ASEMI塑封肖特基二极管MBR10200FAC
    编辑:llMBR10200FAC-ASEMI塑封肖特基二极管MBR10200FAC型号:MBR10200FAC品牌:ASEMI封装:ITO-220AC特性:肖特基二极管正向电流:10A反向耐压:200V恢复时间:5ns引脚数量:2芯片个数:1芯片......
  • Arcgis热连接照片等
    1、添加字段并编辑字段路径2、设置超链接字段3、用超链接工具浏览弹出路径下的照片......
  • vscode搭建简单java运行环境
    打开官网​​​https://code.visualstudio.com/docs/languages/java​​安装这几个就够了(好像也可以通过安装JavaExtensionPack这个插件来安装上面的几个)正常安装能get到......
  • 好用的在线CURL转代码工具
    工具链接​​​https://www.toolfk.com/tool-online-curl​​步骤1、找到要爬取数据的网站拷贝url2、粘贴到CURL转代码工具......
  • No Python interpreter configured for the project
    ......
  • excel表格多列数据中查找重复行数据
    1、做透视表统计2、设置显示格式3、取消分类汇总4、拷贝到原数据后面5、排序......
  • Selenium的使用,安装chromedriver
    注意chrome和ChromeDriver版本对应1、安装32位电脑版chrome默认路径"C:\ProgramFiles(x86)\Google\Chrome\Application\chrome.exe"2、下载好的chromedriver.exe直接拷......