【问题标题】:Word Interop: How can I determine if a Word document selection includes a paragraph mark?Word 互操作:如何确定 Word 文档选择是否包含段落标记?
【发布时间】:2019-07-19 16:49:17
【问题描述】:

我的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();
        }
    }

问题

注意:对于下面的屏幕截图,文件->选项->显示->显示所有格式标记被选中。

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

然后在执行代码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时如何保留段落标记?

【问题讨论】:

  • 你在处理一个不稳定的逻辑。我的建议是手动尝试操作,然后自动化。使用Selection.TypeText() 而不是Range.Text = or currentSelection.TypeParagraph() 的好命令。让我们尝试像在键盘上打字一样;在这种情况下,Word 会处理事情......

标签: c# vsto selection paragraph word-interop


【解决方案1】:

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

    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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多