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

来源:互联网 发布:水果超市软件 编辑:程序博客网 时间:2024/05/21 18:32

问题:

场景再现:将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);

原创粉丝点击