在MS Word 2007以<替换粗体文本; B>文字< / B>使用C#.NET [英] Replace bold text in MS Word 2007 with <b>text</b> using C#.NET
问题描述
我要搜索中的 MS Word 2007中文档所有粗体文字出现,然后替换每个大胆的文字是 <文本>
像下面的伪代码
在WordDocument
{
串replacedText的foreach boldText =<+ boldText +>中;
WordDocument.replace(boldText,replacedText);
}
WordDocument.save();
你可以做的是这样的:
私人无效ReplaceBoldText(Microsoft.Office.Interop.Word.Document DOC)
{
的foreach(微软。在doc.StoryRanges Office.Interop.Word.Range RNG)
{
的foreach(Microsoft.Office.Interop.Word.Range rngWord在rng.Words)
{
如果( !rngWord.Bold = 0)
{
rngWord.Bold = 0;
rngWord.Text =< B>中+ rngWord.Text +&下; / B个;
}
}
}
}
这会改变每一个文字以< b>文字< / b>
。如果您要检查每一个字符,看它是否是大胆的,你需要通过 rngWord.Characters
进行迭代。您可能需要一些额外的工作来封装连续的粗体字,但依据如上。
如果你只担心整个单词,然后上面将正常工作。
希望这有助于。
I want to search all bold text occurrences in MS Word 2007 document, and replace each bold "text" with "< text >"
Like following pseudo-code
foreach boldText in WordDocument
{
string replacedText = "< " + boldText + " >";
WordDocument.replace(boldText ,replacedText );
}
WordDocument.save();
What you could do is something like this:
private void ReplaceBoldText(Microsoft.Office.Interop.Word.Document doc)
{
foreach(Microsoft.Office.Interop.Word.Range rng in doc.StoryRanges)
{
foreach (Microsoft.Office.Interop.Word.Range rngWord in rng.Words)
{
if (rngWord.Bold != 0)
{
rngWord.Bold = 0;
rngWord.Text = "<b>" + rngWord.Text + "</b>";
}
}
}
}
This will change every TEXT to <b>TEXT</b>
. If you want to check each character to see if it is bold you would need to iterate through rngWord.Characters
. You may need some extra work to encapsulate consecutive bold characters, but the basis is as above.
If you are only worrying about whole words then the above will work fine.
Hope this helps.
这篇关于在MS Word 2007以<替换粗体文本; B>文字< / B>使用C#.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!