c# word interop 查找并替换所有内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19252252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# word interop find and replace everything
提问by Tom van den Berk
I have some code to replace text inside a word 2010 docx.
我有一些代码来替换 word 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);
This works without formatting. But when {id} is formatted it does not replace the text.
这无需格式化即可工作。但是当 {id} 被格式化时,它不会替换文本。
How can I make this code ignore formatting?
如何使此代码忽略格式?
采纳答案by joecop
I use this function to find and replace. you can specify any of the options.
我用这个函数来查找和替换。您可以指定任何选项。
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);
}
And usage would be :
用法是:
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");
And you can use the FindAndReplace function over and over....
Hope this helps.
你可以一遍又一遍地使用 FindAndReplace 函数......
希望这会有所帮助。
回答by Ph?m M?nh
You can try this :
你可以试试这个:
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();
回答by Captain Normal
From Visual Studio 2013 you can do this:
从 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);
(Posted for the benefit of anyone, like me, who came across this question but is not necessarily using the same versions of the tools as the OP.)
(为了像我这样遇到这个问题但不一定使用与 OP 相同版本的工具的人的利益而发布。)
回答by ladone
A method that divides a string if the string contains more than 255 characters.
如果字符串包含超过 255 个字符,则该方法将字符串分割。
void FindAndReplace(Microsoft.Office.Interop.Word.Application doc, string findText, string replaceWithText)
{
if (replaceWithText.Length > 255)
{
FindAndReplace(doc, findText, findText + replaceWithText.Substring(255));
replaceWithText = replaceWithText.Substring(0, 255);
}
//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(findText, ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, replaceWithText, ref replace,
ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
}