none
Character style subscript for the last digit in a sequence RRS feed

  • Question

  • Hi all,

    Pardon me if this question sounds silly. But if an answer or a work-around exists, it will greatly help me. I was wondering, if I can format a string of digits in a Word document with a Word style such as the example shown below.

    For the string "11000B" - I want to create a style called "Binary" that when applied to this string, formats only the last character "B" as a subscript. So the desired output (HTML format shown below for clarity) is:

    11000<sub>B</sub>
    

    That is, only the character "B" is subscripted and the rest of the digits left as it is. Is this possible?

    Greets,

     

    Tuesday, October 11, 2011 2:42 PM

Answers

  • Hi Sam

    Not with a style, no, I'm afraid it's not possible :-) Interesting idea, though!

    The only alternative to direct formatting (as you show) that occurs to me is to put the entire string in a EQ field code. The switches available for the EQ field will let you raise/lower and do other things to individual parts of text, while essentially keeping it together as a single entity. (EQ = Equation and comes from times before MS licensed MathType or introduced OMath).


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by samwillie Wednesday, October 12, 2011 7:05 AM
    Tuesday, October 11, 2011 3:42 PM
    Moderator
  • Hi Sam,

    Try something based on:

    Sub Demo()
    Dim FldRng As Range
    With ActiveDocument
      Set FldRng = Selection.Range
      .Fields.Add Range:=FldRng, Type:=wdFieldEmpty, Text:="", PreserveFormatting:=False
      With FldRng
        .MoveEnd Unit:=wdWord, Count:=1
        .Fields(1).Code.Text = "EQ 01010\s\do8(B)"
        .Fields.Update
      End With
    End With
    End Sub

    The reason for the roundabout way of adding the field code is that, unless you do so, the field will end up with a space at each end and those spaces will then show up in the document.

    Do note that the field code isn't actually subscripting the 'B' - it's simply setting it lower than the rest of the line at full size. A side effect is that the line height for the line concerned will increase. Thus, if you're inserting only a single such string into the document, you'd probably be better off applying a subscript character style to just the last character of the string via your code (not particularly difficult) or, if you're working with many such strings in a document, you might prefer to insert the strings, then use Find/Replace to manage the subscripting. Either way, I don't think field coding adds anything of value to the process if you want true subscripting.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    • Marked as answer by samwillie Thursday, October 13, 2011 7:37 AM
    Wednesday, October 12, 2011 9:37 AM

All replies

  • Hi Sam

    Not with a style, no, I'm afraid it's not possible :-) Interesting idea, though!

    The only alternative to direct formatting (as you show) that occurs to me is to put the entire string in a EQ field code. The switches available for the EQ field will let you raise/lower and do other things to individual parts of text, while essentially keeping it together as a single entity. (EQ = Equation and comes from times before MS licensed MathType or introduced OMath).


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by samwillie Wednesday, October 12, 2011 7:05 AM
    Tuesday, October 11, 2011 3:42 PM
    Moderator
  • Hi Cindy

    Thanks very much! This should resolve my issue. But I am having trouble inserting the EQ field code programmatically in the Word document using C#. This is the macro recording I got:

        Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
            "EQ 01010\s\do8(B)", PreserveFormatting:=False
    

    Can you please point me in the right direction? I eventually want this field code to appear within my plain text CC whose tag name is "Binary", so I get a formatted code inside my plain text CC.

    One other thing I observed was  - when I try to do "Insert"->"Quick Parts"->"Field" inside a content control, the "Field" button is greyed out - that means I cannot insert a field code within a content control. Is this behavior right?

    Greets,

    Wednesday, October 12, 2011 7:15 AM
  • Hi Sam,

    Try something based on:

    Sub Demo()
    Dim FldRng As Range
    With ActiveDocument
      Set FldRng = Selection.Range
      .Fields.Add Range:=FldRng, Type:=wdFieldEmpty, Text:="", PreserveFormatting:=False
      With FldRng
        .MoveEnd Unit:=wdWord, Count:=1
        .Fields(1).Code.Text = "EQ 01010\s\do8(B)"
        .Fields.Update
      End With
    End With
    End Sub

    The reason for the roundabout way of adding the field code is that, unless you do so, the field will end up with a space at each end and those spaces will then show up in the document.

    Do note that the field code isn't actually subscripting the 'B' - it's simply setting it lower than the rest of the line at full size. A side effect is that the line height for the line concerned will increase. Thus, if you're inserting only a single such string into the document, you'd probably be better off applying a subscript character style to just the last character of the string via your code (not particularly difficult) or, if you're working with many such strings in a document, you might prefer to insert the strings, then use Find/Replace to manage the subscripting. Either way, I don't think field coding adds anything of value to the process if you want true subscripting.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    • Marked as answer by samwillie Thursday, October 13, 2011 7:37 AM
    Wednesday, October 12, 2011 9:37 AM
  • Hi Paul,

    Thanks! Oh, I didn't think about the line-height side effect - this is an issue. I will try applying a character style just to the last character in the string. Thanks for the detailed explanation again!

    Greets

    Thursday, October 13, 2011 7:37 AM