无法复制Word文档的特定页面 [英] Not able to copy specific pages of word document

查看:133
本文介绍了无法复制Word文档的特定页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图剪切我的word文档(.docx)的特定页面,比如说2、4.我正在使用for循环遍历根据根据,进行拆分的页面.下面是代码相同的

I am trying to cut specific pages of my word document(.docx), say 2, 4. I am using for loop to traverse as per the page gave splitting it based on ,.Below is the code for the same

if (startEnd.Contains(','))
{
    arrSpecificPage = startEnd.Split(',');

    for (int i = 0; i < arrSpecificPage.Length; i++)
    {
        range.Start = doc.GoTo(WdGoToItem.wdGoToPage, WdGoToDirection.wdGoToAbsolute, arrSpecificPage[i]).Start;
        range.End = doc.GoTo(WdGoToItem.wdGoToPage, WdGoToDirection.wdGoToAbsolute, arrSpecificPage[i]).End;
        range.Copy();
        newDocument.Range().Paste();                    
    }
    newDocument.SaveAs(outputSplitDocpath);
}

,但是此代码的问题在于它仅将最后一页仅复制到新文档,即在这种情况下为4.如何添加2?代码有什么问题?

but the issue with this code is that its just copying the last page only to the new document i.e 4 in this case. How to add 2 as well? What's wrong in the code?

推荐答案

由于您始终将整个文档范围"指定为目标,因此每次粘贴文档的全部内容都会被替换.

Since you always specify the entire document "range" as the target, each time you paste the entire content of the document is replaced.

使用Range对象而不是选择对象是正确的,但是如果您思考诸如选择对象之类的范围,则会有所帮助.如果选择所有内容(Ctrl + A)然后粘贴,则所选内容将替换为粘贴的内容.分配给范围的任何内容都将替换范围的内容.

It's correct that you work with a Range object and not with a selection, but it helps if you think about a Range like a selection. If you select everything (Ctrl+A) then paste, what was selected is replaced by what is pasted. Whatever is assigned to a Range will replace the content of the Range.

解决此问题的方法是折叠"范围-就像按右箭头或左箭头键将选择折叠"到其起点或终点一样.在对象模型中,这是Collapse方法,该方法带有一个参数,该参数指示是折叠到起点还是终点(请参见下面的代码).

The way to solve this is to "collapse" the Range - think of it like pressing the Right-arrow or left-arrow key to "collapse" a selection to its start or end point. In the object model, this is the Collapse method that takes a parameter indicating whether to collapse to the start or end point (see the code below).

请注意,我还更改了代码以使用document.Content而不是Document.Range. Content是一个属性,它返回文档的整个主体; Range是一种期望定义Range的起点和终点的方法.使用属性是整个文档的首选方法.

Note that I've also changed the code to use document.Content instead of Document.Range. Content is a property that returns the entire body of the document; Rangeis a method that expects a start and end point defining a Range. Using the property is the preferred method for the entire document.

if (startEnd.Contains(','))
{
    arrSpecificPage = startEnd.Split(',');

    for (int i = 0; i < arrSpecificPage.Length; i++)
    {
        range.Start = doc.GoTo(WdGoToItem.wdGoToPage, WdGoToDirection.wdGoToAbsolute, arrSpecificPage[i]).Start;
        range.End = doc.GoTo(WdGoToItem.wdGoToPage, WdGoToDirection.wdGoToAbsolute, arrSpecificPage[i]).End;
        range.Copy();
        Word.Range targetRange = newDocument.Content
        targetRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
        targetRange.Paste();                    
    }
    newDocument.SaveAs(outputSplitDocpath);
}

这篇关于无法复制Word文档的特定页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆