0

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

例子:

从前,有一个#little 女孩住在森林附近的一个村庄里。每次出门,小女孩都会披着一件红色的马甲,所以村里的人都叫她小红帽。#

一天早上,小红帽#问妈妈是否可以去看望她的祖母,因为他们已经有一段时间没见面了。#

“这是个好主意,”她妈妈说。#所以他们为小红帽准备了一个漂亮的篮子,准备送给她的祖母。#

现在在所有之间#我需要将文本加粗

4

3 回答 3

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);
}
于 2013-09-20T06:03:34.327 回答
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 );
           }
        }
于 2013-09-20T10:21:03.470 回答
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
于 2013-09-18T14:59:54.343 回答