title: VBA Word - changing the heading makes it loosing its style
body: <p>There's a bunch of questions here asking how to get all headings, but apparently nobody tried to change a heading ...</p>
  
  <p>I want to change some second level headings from <em>Old Heading</em> to <em>New Heading</em>, so I did this ...</p>
  
  <pre><code>Sub changeHeading()
      Dim p As Paragraph
      For Each p In ActiveDocument.Paragraphs
          If p.Style = "Heading 2" Then _
              If p.Range.Text = "Old Heading" &amp; vbCr Then p.Range.Text = "New Heading" &amp; vbCr
      Next p
  End Sub
  </code></pre>
  
  <p>The headings get the new name, but they also get a different style - they get the style of the following paragraph - i.e. if the text after the heading had style <em>table text</em> the heading will have style <em>table text</em> afterwards (the font size will remain the one from the old heading), the numbering of the chapters is lost. If I set the style back <code>p.Style = "Heading 2"</code> this will make the first text after that heading (i.e. the next paragraph) having the <code>Heading 2</code> style, though it's still the same paragraph <code>p</code> (with changed text, but same position in the document).</p>
  
  <p>Situation before:</p>
  
  <pre><code>1.4 Old Heading
  some text
  </code></pre>
  
  <p>after changing the heading and setting back the style</p>
  
  <pre><code>New Heading
  1.4 some text    
  </code></pre>
  
  <p>Where <code>some text</code> has the style <code>Heading 2</code> and <code>New Heading</code> has font size of the heading, but no numbering.</p>
  
  <p>Second problem, it takes unbelievable long (some seconds for a small document with just few paragraphs).</p>
  
  <p>So, how to change a heading? And how to do it in a way that won't take minutes on a big document? Thanks for any help.</p>
  
  <p>EDIT:</p>
  
  <p>I found that changing the text with</p>
  
  <pre><code>Call p.Range.Find.Execute(FindText:="Old Heading", ReplaceWith:="New Heading")
  </code></pre>
  
  <p>will keep the style, so this will solve my original problem of changing the text, but I'd still like to know why setting the paragraphs text makes it loosing its style.</p>