Pregunta

am working on a word document Programmatically using C#.net, Interop.Word, I have paragraphs in this document starting and ending with a "#".

Example:

Once upon a time, there was a #little girl who lived in a village near the forest. Whenever she went out, the little girl wore a red riding cloak, so everyone in the village called her Little Red Riding Hood.#

One morning, Little Red Riding Hood #asked her mother if she could go to visit her grandmother as it had been awhile since they'd seen each other.#

"That's a good idea," her mother said. #So they packed a nice basket for Little Red Riding Hood to take to her grandmother.#

Now between all # I need the text be bold

¿Fue útil?

Solución

Fixed that.

        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 );
           }
        }

Otros consejos

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
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);
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top