Word Interop:如何确定Word文档选择中是否包含段落标记?

时间:2019-07-19 16:49:17

标签: c# vsto selection paragraph word-interop

我的VSTO外接程序提供了一个功能区按钮,单击该按钮时会调用ObjButtonAddFoo_Click,该按钮将所选文本替换为文字字符串:

    private bool ReplaceText(string textToInsert)
    {
        bool retval = false;

        Microsoft.Office.Interop.Word.Selection currentSelection = Globals.ThisAddIn.Application.Selection;

        // if (currentSelection.ContainsTrailingParagraphMark)
        // {
        //    retval = true;
        // }

        currentSelection.Range.Text = textToInsert;
        currentSelection.EndKey(WdUnits.wdStory);

        return retval;
    }

    private void ObjButtonAddFoo_Click(object sender, RibbonControlEventArgs e)
    {
        if (ReplaceText("REPLACEMENT TEXT"))
        {
            Microsoft.Office.Interop.Word.Selection currentSelection = Globals.ThisAddIn.Application.Selection;
            currentSelection.TypeParagraph();
        }
    }

问题

注意:对于下面的屏幕截图,选中了File-> Options-> Display-> Show All Formatting Marks。

如果用户选择包含尾随段落标记的文本:

enter image description here

然后,执行代码currentSelection.Range.Text = textToInsert时,段落标记丢失。因此,如果所选内容包括结尾的段落标记,我想通过执行currentSelection.TypeParagraph()

来替换它

我尝试查看currentSelection.Paragraphs.Count,但无论选择项是否包含段落标记,其值为1。

我也尝试了How to: Programmatically exclude paragraph marks when creating ranges中描述的技术,但是,如果选择内容中不包含结尾的段落标记,则会保留最初选择的文本的最后一个字符。换句话说,我仍然需要知道所选内容中是否包含尾随段落标记。

    private bool ReplaceText(string textToInsert)
    {
        bool retval = false;

        Microsoft.Office.Interop.Word.Selection currentSelection = Globals.ThisAddIn.Application.Selection;

        // if (currentSelection.ContainsTrailingParagraphMark)
        // {
        //    retval = true;
        // }

        //Remove the paragraph mark from the range to preserve it
        object charUnit = Microsoft.Office.Interop.Word.WdUnits.wdCharacter;
        object move = -1;  // move left 1
        currentSelection.MoveEnd(ref charUnit, ref move);
        currentSelection.Range.Text = textToInsert;

        currentSelection.EndKey(WdUnits.wdStory);

        return retval;
    }

在我的ReplaceText方法中,如何确定currentSelection是否带有尾部段落标记。或者,执行currentSelection.Range.Text = textToInsert时如何保留段落标记?

1 个答案:

答案 0 :(得分:0)

我太难了。我要做的就是询问所选内容的最后一个字符,以查看其是否为段落标记。

    private void ReplaceText(string textToInsert)
    {
        // https://docs.microsoft.com/en-us/visualstudio/vsto/how-to-programmatically-exclude-paragraph-marks-when-creating-ranges?view=vs-2019

        Microsoft.Office.Interop.Word.Selection currentSelection = Globals.ThisAddIn.Application.Selection;

        // if current selection has a trailing paragraph mark, then 
        // modify the selection to omit it before replacing the text
        if (currentSelection.Range.Text.Substring(currentSelection.Range.Text.Length-1, 1).Equals("\r"))
        {
            object charUnit = Microsoft.Office.Interop.Word.WdUnits.wdCharacter;
            object move = -1;
            currentSelection.MoveEnd(ref charUnit, ref move);
        }

        currentSelection.Range.Text = textToInsert;
    }