替换参考ID

时间:2013-08-07 06:44:26

标签: c# replace ms-word

我试图用文本替换这个上标但是当我尝试我得到属性或索引器'Microsoft.Office.Interop.Word.Find.Replacement' cannot be assigned to - -it is read only的错误。对不起,我刚刚处理word文档。

        wordDoc.ActiveWindow.Selection.Find.Font.Superscript = -1;
        object forward = true;
        object wrap = WdFindWrap.wdFindStop;
        object format = true;
        object matchCase = false;
        object matchWholeWord = false;
        object matchWildcards = false;
        object matchSoundsLike = false;
        object matchAllWordForms = false;


        // Search all numeric superscripts
        while (wordDoc.ActiveWindow.Selection.Find.Execute(ref missing, ref matchCase, ref matchWholeWord, ref matchWildcards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing))
        {

            // Look for Numbered References
            if (Regex.IsMatch((wordDoc.ActiveWindow.Selection.Text).Trim(), @"^(\d+|\d+.?\d+?)$"))
            {
                object reference = wordDoc.ActiveWindow.Selection.Range;
                string refNo = Regex.Match((wordDoc.ActiveWindow.Selection.Text).Trim(), @"^(\d+|\d+.?\d+?)$").Value.ToString();
                MessageBox.Show(refNo);
                object replaceAll = Word.WdReplace.wdReplaceOne;

                wordDoc.ActiveWindow.Selection.Find.Text = refNo;
                wordDoc.ActiveWindow.Selection.Find.ClearFormatting();
                wordDoc.ActiveWindow.Selection.Find.Replacement = "Here"; 

1 个答案:

答案 0 :(得分:0)

要查找上标文字,您需要为.Find object添加其他设置。

//after this line of your code...
wordDoc.ActiveWindow.Selection.Find.Text = refNo;

//...add the following two:
wordDoc.ActiveWindow.Selection.Find.Superscript = true;
wordDoc.ActiveWindow.Selection.Find.Format = true;

//and the rest of your code unchanged