title: VBA - Find string in email body or subject
body: <p>I am trying to create a simple macro, which reads the <em>active</em> email and checks whether or not a certain string is present. Now, the string can have two possible formats, and will only contains digits. </p>
  
  <p><strong>The two formats:</strong></p>
  
  <p><code>xxx-xxxxxxxx</code> or <code>xxxxxxxxxxx</code>
  (x will always be a digit)</p>
  
  <p>I am unsure on how to do this. Below I have a macro, which reads the mail - but it can only find a <strong><em>specific string</em></strong>:</p>
  
  <pre><code>Sub AutomateReplyWithSearchString()
  
      Dim myInspector As Outlook.Inspector
      Dim myObject As Object
      Dim myItem As Outlook.MailItem
      Dim myDoc As Word.Document
      Dim mySelection As Word.Selection
      Dim strItem As String
      Dim strGreeting As String
  
      Set myInspector = Application.ActiveInspector
      Set myObject = myInspector.CurrentItem
  
      'The active inspector is displaying a mail item.
      If myObject.MessageClass = "IPM.Note" And myInspector.IsWordMail = True Then
          Set myItem = myInspector.CurrentItem
  
          'Grab the body of the message using a Word Document object.
          Set myDoc = myInspector.WordEditor
          myDoc.Range.Find.ClearFormatting
          Set mySelection = myDoc.Application.Selection
          With mySelection.Find
  
              .Text = "xxx-xxxxxxxx"
              .Replacement.Text = ""
              .Forward = True
              .Wrap = wdFindContinue
              .Format = False
              .MatchCase = False
              .MatchWholeWord = False
              .MatchAllWordForms = False
              .MatchSoundsLike = False
              .MatchWildcards = True
  
          End With
  
          If mySelection.Find.Execute = True Then
              strItem = mySelection.Text
  
              'Mail item is in compose mode in the inspector
              If myItem.Sent = False Then
                  strGreeting = "With reference to " + strItem
                  myDoc.Range.InsertBefore (strGreeting)
              End If
          Else
              MsgBox "There is no item number in this message."
  
          End If
      End If
      End Sub
  </code></pre>