C# office 控件编程 由于word中排版造成word复制【含书签】容易遇到的问题 .docx

本文介绍了一种复制Word文档的方法,确保即使在包含书签的文档中进行横向和纵向复制时也能完整保留书签信息。通过全文复制而非按节复制的方式避免了书签丢失的问题。

问题:

场景再现:将word复制到另一个文档,且该文档为含书签的项目,书签在复制时候会遇到,当含横向和纵向copy时,如用下面方式copy会存在书签丢失的问题。

此复制word方法由于按照节去copy存在如果书签跨两节,丢失问题

            //1.创建新doc

           Word.Application newapp = new Word.Application();

           Word.Document newdoc;

           object nothing = System.Reflection.Missing.Value;//用于作为函数的默认参数

           newdoc = newapp.Documents.Add(ref nothing, ref nothing, ref nothing, refnothing);//生成一个word文档

           //2.word可视化

           newapp.Visible = true;

           //3.复制旧doc

           this.Select();

           //4.黏贴到新doc

           Object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;

 

           int SectionSize = this.Sections.Count;

           for (int i = 1; i <= SectionSize; i++)

           {

                this.Sections[i].Range.Copy();

               newdoc.Sections[i].Range.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);

           }

 

解决处理方式:

缺点:处理速度没有前面方法快速

            //1.创建新doc

           Word.Application newapp = new Word.Application();

           Word.Document newdoc;

           object nothing = System.Reflection.Missing.Value;//用于作为函数的默认参数

           newdoc = newapp.Documents.Add(ref nothing, ref nothing, ref nothing, refnothing);//生成一个word文档

           //2.word可视化

           newapp.Visible = true;

 

           this.Application.Selection.WholeStory();

           this.Application.Selection.Copy();

           newdoc.Application.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值