使用C#.net编辑单词段落

时间:2013-09-18 06:41:22

标签: .net ms-word interop range paragraphs

正在使用C#.net,Interop.Word以编程方式处理word文档,我在本文档中有段落以“#”开头和结尾。

示例:

曾几何时,有一个#little女孩住在森林附近的一个村庄里。每当她外出时,这个小女孩都穿着红色斗篷,所以村里的每个人都叫她小红帽。#

一天早上,小红帽#ased她的母亲是否可以去看望她的祖母,因为他们已经见过彼此了。#/ p> “这是一个好主意,”她的母亲说。 #所以他们为小红帽打包了一个漂亮的篮子给她的祖母。#

现在所有#我需要文字为粗体

3 个答案:

答案 0 :(得分:0)

Sub Macro1()
'
' Macro1 Macro
'
'
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Dim done As Boolean
    done = False
While Not done
    With Selection.Find
        .Text = "#*#"
        .Replacement.Text = "*"
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With

 Selection.Find.Execute

 If Selection.Find.Found = False Then
    done = True
 Else
    Selection.Text = Mid(Selection.Text, 2, Len(Selection.Text) - 2)
    Selection.Font.Bold = wdToggle
    Selection.EndKey Unit:=wdLine
End If

Wend

End Sub

答案 1 :(得分:0)

oWord.Selection.ClearFormatting();

 bool done = false;
 while (!done)
 {
    object txt = "#*#";
    object oFalse = false;
     object oTrue = true;
    object wdWrap =      Microsoft.Office.Interop.Word.WdFindWrap.wdFindStop;
 oWord.Selection.Find.Execute(ref txt, ref oFalse, ref oFalse, ref oTrue, ref oFalse, ref oFalse, ref oTrue, ref wdWrap  , ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
  if (oWord.Selection.Find.Found == false)
     {
        done = true;
     }
  oWord.Selection.Text = oWord.Selection.Text.Substring(2 - 1, oWord.Selection.Text.Length - 2);
   oWord.Selection.Font.BoldBi = 1;
   oWord.Selection.Bookmarks.Add("TM2011_517_1", ref oMissing);
}

答案 2 :(得分:0)

修正了。

        oWord.Selection.HomeKey( Microsoft.Office.Interop.Word.WdUnits.wdStory );
        oWord.Selection.ClearFormatting();

        bool done = false;
        while ( !done )
        {
           object txt = "#*#";
           object oFalse = false;
           object oTrue = true;
           object wdWrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindStop;
           oWord.Selection.Find.Execute( ref txt, ref oFalse, ref oFalse, ref oTrue, ref oFalse, ref oFalse, ref oTrue, ref wdWrap, ref oFalse );
           if ( oWord.Selection.Find.Found == false )
           {
              done = true;
           }
           else
           {
              oWord.Selection.Font.Bold = 1;
              oWord.Selection.Text = oWord.Selection.Text.Substring( 1, oWord.Selection.Text.Length - 2 );
              oWord.Selection.MoveRight( Microsoft.Office.Interop.Word.WdUnits.wdCharacter );
           }
        }