View Single Post
 
Old 12-02-2021, 04:30 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

This is not particularly clean code but it appears that there are far better ways to achieve what you are trying to do. Using selection while jumping across multiple documents is a very bad idea if you want to know where you are.

Your code implies that within each <html> element is only one number string and one <title> element. If that is the case then there is no reason to bother with the html elements at all since the pairs of title/number probably match.

Perhaps if you post a sample document we can provide better code to achieve your aims. For instance, the initial loop to find the <html> is better done with ranges along these lines
Code:
  Dim aRng As Range, aDoc As Document, aDoc2 As Document
  
  Set aDoc = ActiveDocument
  Set aRng = aDoc.Range
  With aRng.Find
    .ClearFormatting
    .Text = "\<\!doctype html\>*\</html\>"
    .Forward = True
    .Wrap = wdFindStop
    .MatchCase = False
    .MatchWildcards = True
    Do While .Execute = True
      Set aDoc2 = Documents.Add
      aDoc2.Range.FormattedText = aRng.FormattedText
      'do your inner processing of the <html> element here
    
      aRng.Collapse Direction:=wdCollapseEnd
    Loop
  End With
But I'm not really convinced that there needs to be three documents at all. You should only need the initial document and let the code create an output document for the data you want to gather.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote