Forum Discussion

Nimeshika's avatar
Nimeshika
Contributor
10 years ago

How to get color of text present in MS word 2010 using jscript?

Hello,

        I have  a TextObject in MS Word 2010. I want to validate that the text is is in black. I could text using text recognition.

      The foreground property was not available in the properties list.

   Can someone help me in how i can validate that the text is in Black color?

 

I have attached the screen shot of available methods and poperties of the text object.

 

Properties.PNG

 

 

Method.PNG

 

 

 

 

  • Hi Nimeshika,

    You can use Word's COM interface to check text color. You'll need to get a Range object corresponding to the text, and then check its Font.ColorIndex property.

     

    function Test()
    {
    // The file name and text to check
    var strFileName = "C:\\Users\\helen\\Desktop\\Document1.docx"; var strText = "No PATIENT ALLERGY"; // Color constants var wdBlack = 1; var wdAuto = 0; // Automatic color; usually black // Open the file in Word var oWord = Sys.OleObject("Word.Application") oWord.Visible = true; var oDoc = oWord.Documents.Open(strFileName); // Find text in the file var oContent = oDoc.Content; if (oContent.Find.Execute(strText)) { // Check the text color var color = oContent.Font.ColorIndex; if ((color == wdBlack) || (color == wdAuto)) Log.Checkpoint("Text color is black."); else Log.Error("Text color is NOT black."); } else { Log.Error("Text not found."); } oDoc.Close(); oWord.Quit(); }
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Hi Nimeshika,

    You can use Word's COM interface to check text color. You'll need to get a Range object corresponding to the text, and then check its Font.ColorIndex property.

     

    function Test()
    {
    // The file name and text to check
    var strFileName = "C:\\Users\\helen\\Desktop\\Document1.docx"; var strText = "No PATIENT ALLERGY"; // Color constants var wdBlack = 1; var wdAuto = 0; // Automatic color; usually black // Open the file in Word var oWord = Sys.OleObject("Word.Application") oWord.Visible = true; var oDoc = oWord.Documents.Open(strFileName); // Find text in the file var oContent = oDoc.Content; if (oContent.Find.Execute(strText)) { // Check the text color var color = oContent.Font.ColorIndex; if ((color == wdBlack) || (color == wdAuto)) Log.Checkpoint("Text color is black."); else Log.Error("Text color is NOT black."); } else { Log.Error("Text not found."); } oDoc.Close(); oWord.Quit(); }
    • Nimeshika's avatar
      Nimeshika
      Contributor

      Hello Helen Kosova,

                    Thanks for the solution. But when i tried executing the above code I got a runtime error saying 'Range.Font' is null or not an object.

      I am attaching the screen shot of the error.

       

      Can that error be resolved?

      Or is there any alternative method to fetch the text color from MS Word?

       

      MS_WordError.PNG

      • HKosova's avatar
        HKosova
        SmartBear Alumni (Retired)

        I've updated the code above - could you please try again?