簡體   English   中英

Word Interop:如何確定Word文檔選擇中是否包含段落標記?

[英]Word Interop: How can I determine if a Word document selection includes a paragraph mark?

我的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。

我還嘗試了“ 如何:創建范圍時以編程方式排除段落標記”中介紹的技術,但是如果選擇內容中不包含結尾的段落標記,則會保留最初選擇的文本的最后一個字符。 換句話說,我仍然需要知道所選內容中是否包含尾隨段落標記。

    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時,如何保留段落標記?

我把這個做的太難了。 我要做的就是詢問選擇的最后一個字符,看看它是否帶有段落標記。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM