在MS Word 2007以&lt替换粗体文本; B>文字< / B>使用C#.NET [英] Replace bold text in MS Word 2007 with <b>text</b> using C#.NET

查看:163
本文介绍了在MS Word 2007以&lt替换粗体文本; B>文字< / B>使用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以&lt替换粗体文本; B&GT;文字&lt; / B&GT;使用C#.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆