1

我有一个包含许多由 html 标签定义的 html 文档的 word 文档。我想创建一个数组或范围集合,每个范围由一个 html 文档组成。例如,这里是 Word 文档:

<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>

等我想用一系列范围填充 rngHTMLDocs() As Range,每个范围都包含每个开始和结束 html 标记中的文本。

我创建了以下代码,试图遍历定义这些范围的整个文档,但它只选择 HTML Doc 1。我想我可能以错误的方式处理整个迭代。无论如何,这是代码:

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

在为整个文档设置 rngDocContent 并使用 wdFindContinue 时,我曾希望它实际上会继续搜索文档以查找打开的 html 标记的下一个实例,但事实并非如此。提前感谢您提供的任何帮助。

4

2 回答 2

1

在这种情况下,范围对象的集合对您来说会更好吗?创建一个集合对象,每次搜索迭代都会创建一个新的范围对象,然后将其添加到集合中。这样每个 html 文档都可以被称为 colRanges(n)?

于 2012-06-22T20:07:33.070 回答
1

发现我缺少的是 Loop 语句之前的 .Execute 语句,因为这就是导致原始 .Find 继续的原因。我还添加了一个 ReDim Preserve 语句,因为我没有事先计算文档中包含多少 HTML 文档。所以现在循环的结尾看起来像这样:

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

希望这可以帮助某人。

于 2012-06-22T19:57:27.223 回答