peichorn
03-21-2016, 03:05 AM
Frequent lurker, first time poster.
Bottom line question: why do I get 3 different numbers returned when I use Len(ThisDocument.Range.Text), ThisDocument.Characters.Count, and ThisDocument.Range.End ? Does it have something to do with wordstories (a concept I don't understand), or non-printing characters, or something?
Background to this question:
I've got a way to loop through a document and identify the part of the document I want (range) by looping through each paragraph and testing whether I've found the headings I know are located at the beginning and the end of the range. The document follows a standard structure. It works, but it takes longer than I'd like (>1 minute for a smallish doc, several min for a big one). Here's the code:
Dim PP As Paragraph
sel As Range 'sel = the part of the document I want to select as my range.
Set sel = ActiveDocument.Range(Start:=1, End:=1)
For Each PP In ActiveDocument.Paragraphs
sel.End = PP.Range.End
If InStr(PP.Style, "Heading 2") > 0 And InStr(PP.Range, "[TARGET-1 TEXT HERE]") > 0 Then
'I know the part of the doc I want starts with Heading 2 and that this heading contains specific text
sel.Start = PP.Range.Start
ElseIf InStr(PP.Style, "Heading 1") > 0 And InStr(PP.Range, "[TARGET-2 TEXT HERE") > 0 Then
'Likewise I know the portion of the doc that I want ends this way
sel.End = PP.Range.Start - 1
Exit For
End If
Next PP
sel.select
I'm sure this is an inefficient approach so I tried a couple other approaches, which both ran into the problem that prompted by initial question.
Approach 1. Use instr to find the TARGET-1 TEXT, then check if the corresponding range in the doc is the correct heading style. If so, I found the beginning of my desired range. A similar approach would find TARGET-2.
Thus:
Dim x As Long
x = InStr(ThisDocument.Range.Text, "TARGET-1")
If ThisDocument.Range(x, x + 10).Style = "Heading 2" Then
And here I expected that range.start = x would bring me to the same place in the doc as instr. For simple little test docs, that could happen, but for the real doc I find that range.start is not the same as x.
Approach 2. Similar concept
Dim arr() As String, j As Long
arr = Split(ThisDocument.Range.Text, vbCr) 'I also tried chr(13) and vbcrlf
For j = LBound(arr) To UBound(arr) 'lbound will = 0
If arr(j) = "TARGET-1" Then
If ThisDocument.Paragraph(j + 1).Range.Style = "Heading 2" Then 'j+1 because paragraphs start count at 1 not 0
I expected that each arr(j) would give me a paragraph I could easily associate with paragraph(item), but again while that's true at the beginning of the doc, eventually they go out of sync...making me think there's hidden characters or something...
Any alternative approaches also appreciated. Again, my initial solution works but I'm sure there's a quicker way.
I did search the forum and the web for solutions, but didn't find any.
Thanks!
Bottom line question: why do I get 3 different numbers returned when I use Len(ThisDocument.Range.Text), ThisDocument.Characters.Count, and ThisDocument.Range.End ? Does it have something to do with wordstories (a concept I don't understand), or non-printing characters, or something?
Background to this question:
I've got a way to loop through a document and identify the part of the document I want (range) by looping through each paragraph and testing whether I've found the headings I know are located at the beginning and the end of the range. The document follows a standard structure. It works, but it takes longer than I'd like (>1 minute for a smallish doc, several min for a big one). Here's the code:
Dim PP As Paragraph
sel As Range 'sel = the part of the document I want to select as my range.
Set sel = ActiveDocument.Range(Start:=1, End:=1)
For Each PP In ActiveDocument.Paragraphs
sel.End = PP.Range.End
If InStr(PP.Style, "Heading 2") > 0 And InStr(PP.Range, "[TARGET-1 TEXT HERE]") > 0 Then
'I know the part of the doc I want starts with Heading 2 and that this heading contains specific text
sel.Start = PP.Range.Start
ElseIf InStr(PP.Style, "Heading 1") > 0 And InStr(PP.Range, "[TARGET-2 TEXT HERE") > 0 Then
'Likewise I know the portion of the doc that I want ends this way
sel.End = PP.Range.Start - 1
Exit For
End If
Next PP
sel.select
I'm sure this is an inefficient approach so I tried a couple other approaches, which both ran into the problem that prompted by initial question.
Approach 1. Use instr to find the TARGET-1 TEXT, then check if the corresponding range in the doc is the correct heading style. If so, I found the beginning of my desired range. A similar approach would find TARGET-2.
Thus:
Dim x As Long
x = InStr(ThisDocument.Range.Text, "TARGET-1")
If ThisDocument.Range(x, x + 10).Style = "Heading 2" Then
And here I expected that range.start = x would bring me to the same place in the doc as instr. For simple little test docs, that could happen, but for the real doc I find that range.start is not the same as x.
Approach 2. Similar concept
Dim arr() As String, j As Long
arr = Split(ThisDocument.Range.Text, vbCr) 'I also tried chr(13) and vbcrlf
For j = LBound(arr) To UBound(arr) 'lbound will = 0
If arr(j) = "TARGET-1" Then
If ThisDocument.Paragraph(j + 1).Range.Style = "Heading 2" Then 'j+1 because paragraphs start count at 1 not 0
I expected that each arr(j) would give me a paragraph I could easily associate with paragraph(item), but again while that's true at the beginning of the doc, eventually they go out of sync...making me think there's hidden characters or something...
Any alternative approaches also appreciated. Again, my initial solution works but I'm sure there's a quicker way.
I did search the forum and the web for solutions, but didn't find any.
Thanks!