title: Preserve character formatting in string variables
body: <p>I have a procedure that takes the selected text and places it into an XE field for use in an index; however, if the selection includes superscript or subscript characters, they are stripped out when I place the variable in the field and the index is created incorrectly.  </p>
  
  <p>The form referenced in the code below simply allows the user to type a string, which is then added to the index via another procedure.  </p>
  
  <pre><code>Sub createAbbreviation()
  
  Dim strFieldTextAbbr As String
  Dim rngAbbreviation As Range
  
  If Selection.Type = wdSelectionIP Then
    Load frmInsertAbbreviation
  Else
    Set rngAbbreviation = Selection.FormattedText
    strAbbreviation = Selection.Text
    Load frmInsertAbbreviation
  End If
  
  frmInsertAbbreviation.Show
  If frmInsertAbbreviation.Tag = "Cancel" Then
    Unload frmInsertAbbreviation
    strAbbreviation = ""
    Exit Sub
  End If
  
  strFieldTextAbbr = """" &amp; rngAbbreviation &amp; """ \f Abbreviation \t """ &amp; frmInsertAbbreviation.strDefinition &amp; """"
  
  If Selection.Type = wdSelectionIP Then
    If Selection.Font.Subscript = True Then
      Selection.Font.Subscript = False
    End If
    ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldIndexEntry, _
      Text:=strFieldTextAbbr
  Else
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    If Selection.Font.Subscript = True Then
      Selection.Font.Subscript = False
    End If
    ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldIndexEntry, _
      Text:=strFieldTextAbbr
  End If
  
  Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
  
  Selection.Find.ClearFormatting
    With Selection.Find
      .Text = """"
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindContinue
      .format = True
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
    End With
  
    If Selection.Find.Execute = True Then
      Selection.Collapse wdCollapseRight
      Do Until InStr(1, Selection, Chr(34)) &lt;&gt; 0
        Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
      Loop
    End If
  
  Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
  Selection.Range.FormattedText = rngAbbreviation
  
  Unload frmInsertAbbreviation
  strAbbreviation = ""
  
  End Sub
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I see that the reason it won't work is because I'm placing the range into a string, effectively negating it's object status. I'm going to continue working on it to see if I can make it work. I feel like I'm on the right track, it's just taking a while to get there. Any help would be appreciated.</p>
  
  <p>It turns out that the issue is that formatted text cannot be placed in the Text argument of the Fields.Add method. What's needed is a way to add the content as formatted text or to reinstate the formatting after the text has been added to the field.</p>