简体   繁体   English

[VSTO Word]PasteAndFormat后如何获取范围?

[英][VSTO Word]How to get the range after PasteAndFormat?

I have the code as follow:我的代码如下:

    Word.Selection selection = wordApp.Selection;
    Word.Range range = selection.Range;
    MessageBox.Show("Range :" + range.Text);
    range.Copy();
    range.PasteAndFormat(Word.WdRecoveryType.wdFormatPlainText);
    if (range == null)
        MessageBox.Show("The Range is null currently");
    else
        MessageBox.Show("Range :" + range.Text);

For instance, create a new Word document and input "ABC", if I select the entire "ABC" and run the program, the range won't be changed, but if I select a single character and run the program, the range will be null, why does this happen?比如新建一个Word文档,输入“ABC”,如果我select整个“ABC”运行程序,范围不会改变,但是如果我select单个字符运行程序,范围会变是null,为什么会这样? I have to edit the range furtherly.我必须进一步编辑范围。

I changed the code and now I can get the range after PasteAndFormat.我更改了代码,现在我可以在 PasteAndFormat 之后获取范围。

    Word.Selection selection = wordApp.Selection;
    int start = selection.Start;
    int count = selection.Text.Length;
    selection.Copy();
    selection.PasteAndFormat(Word.WdRecoveryType.wdFormatPlainText);
    int newStart = selection.Start;
    selection.SetRange(start, newStart);
    Word.Range range = selection.Range;
    if (range == null)
        MessageBox.Show("The Range is null currently");
    else
        MessageBox.Show("Range :" + range.Text);

I guess maybe the "Range" is a kind of format, it will be clear after PasteAndFormat(wdFormatPlainText).我想也许“范围”是一种格式,在 PasteAndFormat(wdFormatPlainText) 之后就会清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM