none
Adding color information to numeric picture switch RRS feed

  • Question

  • I am creating a new field in my Word document and want to apply a numeric picture switch to it.  The value I have is pretty standard:

    "$#,0.00;($#,0.00);$0.00;"

    I would like, if possible, to add color information to the switch. For example, if it's a negative number I would like to display the field in red.  If it's zero, I'd like to display it in green.

    I have read numerous articles that imply that color information can be added, but I haven't been able to find an example. 

    Can color info be added, and if so, can someone provide an example? 

    Thanks. 

    Don

    Tuesday, January 28, 2014 1:23 PM

Answers

  • No, I really mean apply the colour red to the text. It's not like the Excel switches, where you put the word [red] in there.

    { =12345.67 \#"$#,0.000;($#,0.00);$0.00;" }



    Peter Jamieson

    Tuesday, January 28, 2014 7:31 PM
  • With Word automation (VSTO or otherwise) you should be able to apply the colours you want, but it will be simpler if you are inserting the numeric switch rather than having to detect and format existing ones.

    A bit of VBA to give you the general idea:

    Sub insertColouredSwitches()
    Dim f As Word.Field
    Dim r As Word.Range
    Dim placeholder As String
    ' think of a suitable placeholder character
    placeholder = Chr(127)
    
    Set f = Selection.Fields.Add(Selection.Range, wdFieldEmpty, "=12345.67 " & placeholder, False)
    Set r = f.Code
    Debug.Print r.Start
    r.MoveStartUntil placeholder
    r.End = r.Start + 1
    r.Text = "\#""$#,0.000;"
    r.Collapse wdCollapseEnd
    r.Text = "($#,0.00);"
    r.Font.ColorIndex = wdRed
    r.Collapse wdCollapseEnd
    r.Text = "$0.00;"
    r.Font.ColorIndex = wdGreen
    r.Collapse wdCollapseEnd
    r.Text = """"
    Set r = Nothing
    Set f = Nothing
    End Sub
    



    Peter Jamieson

    Tuesday, January 28, 2014 8:18 PM

All replies

  • For a foreground colour, select the relevant parts of the switch and apply the appropriate colour, i.e. 

    colour ($#,0.00) red

    colour $0.00 green

    You can apply highlight colours if you want to specify a background colour.

    Also, make sure there is no \*Mergeformat switch in the field.


    Peter Jamieson


    • Edited by Peter Jamieson Tuesday, January 28, 2014 6:38 PM remove mergeformat
    Tuesday, January 28, 2014 6:27 PM
  • Hi Peter,

    Thanks for the reply. Can you provide a complete numeric picture switch? I tried this based on your response and didn't see the desired result.  I took the suggestions very literally... :-)

    \#"$#,0.00;($#,0.00) red;$0.00;"

    \#"$#,0.00;color ($#,0.00) red;$0.00;"

    \#"$#,0.00;colour ($#,0.00) red;$0.00;"

    Based on a cryptic answer to another question on the web, I also tried this:

    \#"$#,0.00;($#,0.00)[red];$0.00;"

    but that didn't work either. 

    Thanks! 

    Don

    Tuesday, January 28, 2014 7:09 PM
  • No, I really mean apply the colour red to the text. It's not like the Excel switches, where you put the word [red] in there.

    { =12345.67 \#"$#,0.000;($#,0.00);$0.00;" }



    Peter Jamieson

    Tuesday, January 28, 2014 7:31 PM
  • I get it now.  

    It sounds, then, like there is no way to do it programmatically - I have to physically edit the field, change the color to red, then update the field. I'd really like to be able to create the field that way from the start, programmatically through VSTO, which is how we're creating and inserting fields in our Word docs. 

    Any ideas on how to do that, or is that outside of the abilities of an add-in? 

    Thanks for your responses. 

    Don

    Tuesday, January 28, 2014 7:43 PM
  • With Word automation (VSTO or otherwise) you should be able to apply the colours you want, but it will be simpler if you are inserting the numeric switch rather than having to detect and format existing ones.

    A bit of VBA to give you the general idea:

    Sub insertColouredSwitches()
    Dim f As Word.Field
    Dim r As Word.Range
    Dim placeholder As String
    ' think of a suitable placeholder character
    placeholder = Chr(127)
    
    Set f = Selection.Fields.Add(Selection.Range, wdFieldEmpty, "=12345.67 " & placeholder, False)
    Set r = f.Code
    Debug.Print r.Start
    r.MoveStartUntil placeholder
    r.End = r.Start + 1
    r.Text = "\#""$#,0.000;"
    r.Collapse wdCollapseEnd
    r.Text = "($#,0.00);"
    r.Font.ColorIndex = wdRed
    r.Collapse wdCollapseEnd
    r.Text = "$0.00;"
    r.Font.ColorIndex = wdGreen
    r.Collapse wdCollapseEnd
    r.Text = """"
    Set r = Nothing
    Set f = Nothing
    End Sub
    



    Peter Jamieson

    Tuesday, January 28, 2014 8:18 PM
  • Hi

    Code shared below almost seems the .net code similar to vbscript code mentioned in above thread .I have tried out with the snippet options below to set the font color for word add-in variable field, problem I am facing is it considering default color selected in Word Palette and the value setting automatically to Font is  "wdAutomatic" . Could any one have any clue on this behavior for VSTO word add-in?

    Different snippet options tries out for setting word variable font value : 

    option1 : currentSelection.Range.Font.Color = WdColor.wdColorRed;

    option2 : currentSelection.Range.Font.ColorIndex = WdColorIndex.wdRed;

    and other options using some ColorTranslator  like.

    option3: currentSelection.Range.Font.Shading.BackgroundPatternColor = (WdColor)ColorTranslator.ToOle(Color.Red);

    option4 :currentSelection.Range.Font.Color = (WdColor)ColorTranslator.ToOle(Color.Red);

    Thursday, January 30, 2014 2:54 PM
  • For an implementation, with the use of a single switch to provide different formats for +ve, -ve and 0 results, see 'Formatting Numeric Field Results' in my Microsoft Word Field Maths Tutorial, at:
    or

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, January 31, 2014 11:41 AM
  • All I had to do here was qualify the WdColorIndex.wdRed correctly, e.g.

    using Word = Microsoft.Office.Interop.Word;

    //

    //

    <whatever>.Range.Font.ColorIndex = Word.WdColorIndex.wdRed;


    Peter Jamieson

    Friday, January 31, 2014 1:46 PM