title: VBA Word - Apply a style to a line of text
body: <p>I'm trying to apply a word style to a line of text using vba, so that it will appear in the table of contents.
  I am having trouble keeping the style contained to the line in question though, for some reason the whole document is picking up the style.</p>
  
  <pre><code>With Selection
  .TypeText Text:=headername                 ' This is defined previously,
  .HomeKey Unit:=wdLine, Extend:=wdMove   ' This is to move the cursor to the start of the line
  .Expand wdLine                           ' This is to select the whole line
  .Style = "Heading 2"                     ' this is to define the style of the selected text
  .EndKey Unit:=wdLine, Extend:=wdMove      ' This is to unhighlight the text
  .InsertBreak Type:=wdLineBreak            ' This is to create a line break    
   End With
  </code></pre>
  
  <p>For some reason though, the whole document picks up "Heading 2" as it's style.
  I have tried countless other ways of doing this, but with no luck,</p>
  
  <p>Does anyone know a better way of doing this, or see where I am going wrong?</p>
  
  <p>Thanks</p>