c#word interop查找并替换所有内容

时间:2013-10-08 15:41:40

标签: c# visual-studio-2010 ms-word office-interop office-2010

我有一些代码可以替换单词2010 docx中的文本。

        object fileName = Path.Combine(System.Windows.Forms.Application.StartupPath, "document.docx");

        Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = true };

        Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(ref fileName, ReadOnly: false, Visible: true);

        aDoc.Activate();

        Microsoft.Office.Interop.Word.Find fnd = wordApp.ActiveWindow.Selection.Find;

        fnd.ClearFormatting();
        fnd.Replacement.ClearFormatting();
        fnd.Forward = true;

        fnd.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;

        fnd.Text = "{id}";
        fnd.Replacement.Text = "123456";
        fnd.Execute(Replace: WdReplace.wdReplaceAll);

这没有格式化。但是当{id}被格式化时,它不会替换文本。

如何使此代码忽略格式化?

5 个答案:

答案 0 :(得分:29)

我使用此功能来查找和替换。您可以指定任何选项。

private void FindAndReplace(Microsoft.Office.Interop.Word.Application doc, object findText, object replaceWithText)
{
    //options
    object matchCase = false;
    object matchWholeWord = true;
    object matchWildCards = false;
    object matchSoundsLike = false;
    object matchAllWordForms = false;
    object forward = true;
    object format = false;
    object matchKashida = false;
    object matchDiacritics = false;
    object matchAlefHamza = false;
    object matchControl = false;
    object read_only = false;
    object visible = true;
    object replace = 2;
    object wrap = 1;
    //execute find and replace
    doc.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
        ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
        ref matchKashida ,ref matchDiacritics, ref matchAlefHamza, ref matchControl);                
}

使用方法是:

object fileName = Path.Combine(System.Windows.Forms.Application.StartupPath, "document.docx");
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = true };
Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(fileName, ReadOnly: false, Visible: true);
aDoc.Activate();
FindAndReplace(wordApp, "{id}", "12345");

你可以一遍又一遍地使用FindAndReplace函数。
希望这可以帮助。

答案 1 :(得分:4)

如果字符串包含超过255个字符,则对字符串进行分割的方法。

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner), FMyPropertyValue( "somevalue" ){}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    UnicodeString s1 = MyPropertyValue;
    UnicodeString s2 = MyPropertyValue.IsEmpty() ? UnicodeString( "emptyValue" ) : MyPropertyValue;
    UnicodeString s3 = MyPropertyValue.IsEmpty() ? UnicodeString( "emptyValue" ) : FMyPropertyValue;
    UnicodeString s4 = System::Sysutils::EmptyStr;

    if ( MyPropertyValue.IsEmpty() )
    {
        s4 = UnicodeString( "emptyValue" );
    }
    else
    {
        s4 = MyPropertyValue;
    }
}

答案 2 :(得分:2)

你可以试试这个:

var doc =  new Microsoft.Office.Interop.Word.Application().Documents.Open("document.docx");

doc.Content.Find.Execute( "{id}", false, true, false, false, false, true, 1, false,  "12345", 2,
false, false, false, false);
doc.Save();

答案 3 :(得分:1)

从Visual Studio 2013中,您可以执行以下操作:

Microsoft.Office.Interop.Word.Range range = this.Application.ActiveDocument.Content;
range.Find.ClearFormatting();
range.Find.Execute(FindText: "find text", ReplaceWith: "replace text", Replace: Word.WdReplace.wdReplaceAll);

(为了任何人的利益而发布,像我一样,遇到了这个问题,但未必使用与OP相同版本的工具。)

答案 4 :(得分:1)

如果有人在 2021 年寻找答案并使用 VS2019, 您可以使用此方法,包括替换文本框等形状内的文本。

 private void FindAndReplace(Application app,Document doc, string findText, string replaceWithText)
        {
            Find findObject = app.Selection.Find;
            findObject.ClearFormatting();
            findObject.Text = findText;
            findObject.Replacement.ClearFormatting();
            findObject.Replacement.Text = replaceWithText;
            object missing = System.Reflection.Missing.Value;
            object replaceAll = WdReplace.wdReplaceAll;
            findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing);

            var shapes = doc.Shapes;
            foreach (Shape shape in shapes)
            {
                if (shape.TextFrame.HasText != 0)
                {
                    var initialText = shape.TextFrame.TextRange.Text;
                    var resultingText = initialText.Replace(findText, replaceWithText);
                    if (initialText != resultingText)
                    {
                        shape.TextFrame.TextRange.Text = resultingText;
                    }
                }
            }
        }