title: How to update an add-in field code without losing field data (MS Word)?
body: <p>I have an add-in field in MS Word which also has some hidden data. I want to update the field's code text. However, when I do so and read my hidden data again, they are gone. See this minimal example:</p>
  <pre><code>Sub testUpdatingFieldCode()
    Dim newField As Field
    Set newField = ActiveDocument.Fields.Add(Range:=Selection.Range, Type:=wdFieldAddin)
    newField.Code.Text = &quot; ADDIN mycode \* MERGEFORMAT &quot;
    newField.Data = &quot;mydata&quot;
  
    Debug.Print &quot;Code: &quot; &amp; newField.Code.Text
    Debug.Print &quot;Data: &quot; &amp; newField.Data ' works - prints &quot;Data: mydata&quot;
  
    newField.Code.Text = &quot; ADDIN mycodechanged \* MERGEFORMAT &quot;
    Debug.Print &quot;Code: &quot; &amp; newField.Code.Text
    Debug.Print &quot;Data: &quot; &amp; newField.Data ' doesn't work - prints &quot;Data: &quot;
  End Sub
  </code></pre>
  <p>Is this normal behaviour or a bug? Is there a suggested way to update field codes without losing the data?</p>
  <p>I could save the <code>Data</code> to a variable like this but I'm worried about performance (I ultimately need to do this for tens or hundreds fields at a time):</p>
  <pre><code>Dim previousData As String: previousData = newField.Data
  newField.Code.Text = &quot; ADDIN my_code_changed  \* MERGEFORMAT &quot;
  newField.Data = previousData
  </code></pre>