none
using C#.Net how to replace bold text in MS Word 2007 with <b>text</b> ?

    Question

  • Dear All

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


    Seek Knowledge From Cradle to Grave
    Wednesday, May 4, 2011 7:21 AM

Answers

  • Hi Mr. Jehan

    Probably the best way to do this would be to use the Range.Find.Execute method. Something along these lines (pseudocode!), perhaps. The following pseudo-code should find all bolded text (words) in the document (main body only!)) and put the < around > it.

    Word.Range rngFindBold = WordDocument.Content;
    rngFindBold.Find.Font.Bold = -1; //true
    rngFindBold.Execute(FindText : "(<*>)", Format : true, ReplaceWith : "< ^& >", MatchWildCards : true, Replace:Word.WdReplace.wdReplaceAll);

    You can test this in the UI by pasting the FindText in to the Find What box and the ReplaceWith string into the correspondng box. Activate "Use wildcards". With the focus in "Find what", click the "format" button, choose "Font" and select Bold. Click Find Next to see what it picks up...


    Cindy Meister, VSTO/Word MVP
    Wednesday, May 4, 2011 2:45 PM
    Moderator

All replies

  • Hello,

    Record a macro while doing that replacement in the Word UI. Then in the code of the macro, you'll find all the objects and their members used. Finally, translate the macro to the language of your choice.


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Wednesday, May 4, 2011 8:28 AM
  • Hello,

     

    To achieve this I think you have to do something like this:

          bool boldStarted=false;
          for(int i=1;i<Globals.ThisAddIn.Application.Selection.StoryLength-1;i++)
          {
            object refRangeStart = i;
            object refRangeEnd=i+1;
            var range = Globals.ThisAddIn.Application.ActiveDocument.Range(ref refRangeStart,ref refRangeEnd);
            if(range.Font.Bold>0 || !boldStarted)
            {
              boldStarted=true;
              range.InsertBefore("<");
            }
            else
            {
              if(boldStarted)
              {
                range.InsertAfter(">");
                boldStarted=false;
              }
            }
          }
    
    
    //if range.InsertBefore/After does not work try this one instead
    //
    //instead of range.InsertBefore("<")
    // Globals.ThisAddIn.Application.ActiveDocument.Range(ref refRangeStart, ref refRangeStart).Text="<";
    //
    // instead of range.InsertAfter(">")
    //Globals.ThisAddIn.Application.ActiveDocument.Range(ref refRangeEnd, ref refRangeEnd).Text=">";
    

     

    Hope this helps,

    Silviu.


    http://www.rosoftlab.net/
    Wednesday, May 4, 2011 8:33 AM
  • Because characters take their "default" formatting from the preceding character, I think the InsertBefore might not insert the "<" character as bold, so additional coding to format that character will be required.

    An alternative to using the InsertBefore and InsertAfter is to store the text defined by the range in a string. Then issue a Selection.TypeText("<") command, followed by a Selection.TypeText(storedText) command, followed by a final Selection.TypeText(">") command.

    Hope this helps


    Regards, Rich
    Wednesday, May 4, 2011 1:01 PM
  • Hi Mr. Jehan

    Probably the best way to do this would be to use the Range.Find.Execute method. Something along these lines (pseudocode!), perhaps. The following pseudo-code should find all bolded text (words) in the document (main body only!)) and put the < around > it.

    Word.Range rngFindBold = WordDocument.Content;
    rngFindBold.Find.Font.Bold = -1; //true
    rngFindBold.Execute(FindText : "(<*>)", Format : true, ReplaceWith : "< ^& >", MatchWildCards : true, Replace:Word.WdReplace.wdReplaceAll);

    You can test this in the UI by pasting the FindText in to the Find What box and the ReplaceWith string into the correspondng box. Activate "Use wildcards". With the focus in "Find what", click the "format" button, choose "Font" and select Bold. Click Find Next to see what it picks up...


    Cindy Meister, VSTO/Word MVP
    Wednesday, May 4, 2011 2:45 PM
    Moderator