A family of Microsoft word processing software products for creating web, email, and print documents.
Although it's quite possible to do nested searches in Word, it's also unnecessary in this case. Instead, one can use a single Find expression then test what's found. Try:
Sub ExportTaggedRanges()
Dim StrPath As String, StrName As String
Dim NewDoc As Document, Rng As Range
StrPath = GetFolder & ""
With ActiveDocument
Set Rng = .Range(0, 0)
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "{[C/]{1,2}ontentI[!\}]{1,}}^13"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
Rng.End = .End
Rng.MoveEnd wdParagraph, -1
If Rng.Paragraphs.First.Range.Text Like "{ContentID #####*}" & vbCr Then
StrName = Replace(Replace(Rng.Paragraphs.First.Range.Text, "{ContentID ", ""), "}" & vbCr, "")
Rng.Copy
Set NewDoc = Documents.Add(Template:="Normal", Visible:=False)
With NewDoc
.Range.Paste
.Range.Paragraphs.First.Range.Delete
With .Range.Characters.Last
While .Previous Like "[" & Chr(9) & "-" & Chr(14) & Chr(32) & Chr(160) & "]"
.Previous.Text = vbNullString
Wend
End With
.SaveAs2 FileName:=StrPath & StrName, Fileformat:=wdFormatXMLDocument, AddToRecentFiles:=False
.Close
End With
End If
Rng.Collapse wdCollapseEnd
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End With
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
Note: With the above code, the output document is saved with just the tag # for its name, and the tag is deleted from the body of the document. The former is managed by 'StrName = Replace(Replace(Rng.Paragraphs.First.Range.Text, "{ContentID ", ""), "}" & vbCr, "")'. The latter is managed by '.Range.Paragraphs.First.Range.Delete'