C#通过word书签,另存文件

原地址是这个 https://blog.csdn.net/u011131296/article/details/9167005,然后进行了修改。
1.引用dll
在这里插入图片描述
2.操作模板,先复制一份临时文件,然后对临时文件进行操作,最后另存为选择指定位置。
在这里插入图片描述

namespace WindowsFormsApp1 {
    public partial class Print : Form {
        /*=================================================*/
        Microsoft.Office.Interop.Word.Document doc;
        Microsoft.Office.Interop.Word.Application app;
        private static string TemplateFile1 = System.Windows.Forms.Application.StartupPath + "\\全水分测定原始记录.doc";
        private static string temppath = System.Windows.Forms.Application.StartupPath + "\\全水分测定原始记录_temp.doc";  //临时文件
        /*=================================================*/


        public Print() {
            InitializeComponent();
        }



        private void button2_Click(object sender, EventArgs e) {
            foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("WINWORD")) {
                p.Kill();
            }

            
            File.Copy(TemplateFile1, temppath,true);

            doc = new Microsoft.Office.Interop.Word.Document();
            app = new Microsoft.Office.Interop.Word.Application();
            //模板路径
            string TemplateFile = temppath;
            object Obj_FileName = temppath;
            object Visible = false;
            object ReadOnly = false;
            object missing = System.Reflection.Missing.Value;
            //C#操作Word书签
            //打开文件  
            doc = app.Documents.Open(ref Obj_FileName,
      ref missing, ref ReadOnly, ref missing,
      ref missing, ref missing, ref missing, ref missing,
      ref missing, ref missing, ref missing, ref Visible,
      ref missing, ref missing, ref missing,
      ref missing);
            doc.Activate();
            //标签
            object bookMark = "";
            if (app.ActiveDocument.Bookmarks.Exists("RQ") == true) {
                bookMark = "RQ";
                app.ActiveDocument.Bookmarks.get_Item
                  (ref bookMark).Range.Text = this.textBox1.Text;
            }
            if (app.ActiveDocument.Bookmarks.Exists("BZ") == true) {
                bookMark = "BZ";
                app.ActiveDocument.Bookmarks.get_Item
                  (ref bookMark).Range.Text = this.textBox2.Text;
            }


            //C#操作Word书签
            object IsSave = true;
            doc.Close(ref IsSave, ref missing, ref missing);


            SaveFileDialog _save = new SaveFileDialog();
            _save.InitialDirectory = "C:\\";
            _save.Filter = "*.doc|*.*";
            _save.FileName = "新建文档.doc";
            if (_save.ShowDialog() == DialogResult.OK) {
                  SaveAS(_save.FileName);
            }
        }

        /// <summary>
                /// 文档另存为
                /// </summary>
                /// <param name="FileName">保存的文件名名称</param>
                /// <returns></returns>
        public bool SaveAS(string FileName) {
            bool _bool = false;
            try {
                //模板路径
                string TemplateFile = temppath;
                //生成word文档            
                string FileName1 = FileName;
                File.Copy(TemplateFile, FileName);
                _bool = true;
                return _bool;
            } catch {
                return _bool;
            }
        }


    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值