Forum Discussion

Nimeshika's avatar
Nimeshika
Contributor
10 years ago
Solved

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...
  • HKosova's avatar
    10 years ago

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