title: VBA Word Find and Replace with Wildcards -- Getting section contents into a variable
body: <p>I am trying to find and replace a string using wildcards and then use the contents of the section labels of the search string in another find and replace command. Here is the portion of the code that does not work; the variables RT and Part do not get assigned the contents of the sections but rather the section label itself.  I also tried setting the variables RT and Part after the Selection.Find.Execute command.  I am a novice VBA programmer so this may be obvious but forgive me.  </p>
  
  <pre><code>Dim RT, Part As String
      Selection.Find.ClearFormatting
      With Selection.Find
          .Text = "(:)([RV][0-9]{1,2})( )(*)(:)"
          .Replacement.Text = "\4"
          RT = .Replacement.Text
          Part = "\2"
          .Forward = True
          .Wrap = wdFindContinue
          .Format = False
          .MatchCase = False
          .MatchWholeWord = False
          .MatchWildcards = True
          .MatchSoundsLike = False
          .MatchAllWordForms = False
      End With
      Selection.Find.Execute
  With Selection
      If .Find.Forward = True Then
          .Collapse Direction:=wdCollapseStart
      Else
          .Collapse Direction:=wdCollapseEnd
      End If
      .Find.Execute Replace:=wdReplaceOne
      If .Find.Forward = True Then
          .Collapse Direction:=wdCollapseEnd
      Else
          .Collapse Direction:=wdCollapseStart
      End If
  End With
  </code></pre>
  
  <p>I then want to use the variables from above in another find and replace that starts out as follows (I haven't included all the code below).</p>
  
  <pre><code>With Selection.Find
      .Text = "(:)("&amp;Part&amp;")( :)"
      .Replacement.Text = RT
  </code></pre>
  
  <p>Ultimately I have an initial text definition in the document that looks like this:</p>
  
  <p>:R1 XYZ:</p>
  
  <p>:R2 ABC:</p>
  
  <p>These initial text definitions get replaced with just XYZ or ABC, then later in the document are the tokens:</p>
  
  <p>:R1 : &lt;-- which I want to replace with XYZ.<br>
  :R2 : &lt;-- which I want to replace with ABC.</p>