1
votes

I have a word document containing many html documents as defined by html tags. I would like to create an array or collection of ranges, each range consisting of one html document. For example, here is the Word document:

<html> <head> <title> </title> </head> <body> HTML Doc 1 </body> </html>
<html> <head> <title> </title> </head> <body> HTML Doc 2 </body> </html>
<html> <head> <title> </title> </head> <body> HTML Doc 3 </body> </html>

etc. I would like to populate rngHTMLDocs() As Range with a series of ranges, each range containing the text within each opening and closing html tag.

I have created the following code in an attempt to iterate through the entire document defining these ranges, but it only keeps selecting HTML Doc 1. I think I may be approaching the whole iteration thing in the wrong way. Anyway, here is the code:

Set rngDocContent = ActiveDocument.Content
intCounter = 1
With rngDocContent.Find
    .ClearFormatting
    .Text = "<html>"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Execute
    Do While .Found = True
        Set rngTemp = rngDocContent.Duplicate
        rngTemp.Select
        Selection.Extend
         With Selection.Find
             .ClearFormatting
             .Text = "</html>"
             .Replacement.Text = ""
             .Forward = True
             .Wrap = wdFindAsk
             .Execute
        End With
        Set rngHtmlDocs(intCounter) = Selection.Range
        Selection.Start = Selection.End
        intCounter = intCounter + 1
     Loop
 End With

In setting the rngDocContent for the entire document, and using wdFindContinue, I had hoped that it would in fact continue searching the document for the next instance of an opening html tag, but that is not the case. Thanks in advance for any help you might provide.

2

2 Answers

1
votes

Would a collection of range objects work better for you in this case? Create a collection object, and each iteration of your search you create a new range object and then add it to the collection. That way each html doc could be refereced as colRanges(n)?

1
votes

Figured out that what I was missing was the .Execute statement right before the Loop statement, as that is what causes the original .Find to continue. I also added a ReDim Preserve statement, since I didn't count beforehand how many HTML docs are contained in the document. So now the end of the loop looks like this:

       Set rngHtmlDocs(intCounter) = Selection.Range
       Selection.Start = Selection.End
       intCounter = intCounter + 1
       ReDim Preserve rngHtmlDocs(intCounter)
       .Execute
    Loop
End With

Hope this helps somebody.