J
Jean-Guy Marcil
Hi there!
I have a routine that looks for highlighted text and then copies each
instance of such found text in a new document.
The main part of the main sub goes like this:
Dim rgeDoc As Range
Dim lngWordCount As Long
Dim i As Long
Dim docNew As Document
Dim docCurrent As Document
Set rgeDoc = ActiveDocument.Range
lngWordCount = 0
Set docCurrent = ActiveDocument
Set docNew = Documents.Add
'wdMainTextStory = 1 and wdTextFrameStory = 5
CheckRange rgeDoc, docNew, docCurrent
i = 1
With docCurrent.Shapes
If .Count > 0 Then
For i = 1 To .Count
If .Item(i).Type = msoTextBox Or .Item(i).Type = msoAutoShape Then
If .Item(i).TextFrame.HasText Then
Set rgeDoc = .Item(i).TextFrame.TextRange
CheckRange rgeDoc, docNew, docCurrent
End If
End If
Next
End If
End With
I have to search each shape separately because the built-in Find ignores them.
This sub calls the following function:
Function CheckRange(ByVal rgeFind As Range, _
ByVal docCopy As Document, ByRef docFrom As Document)
Dim rgeKeep As Range
Dim rgeCopy As Range
Set rgeKeep = rgeFind.Duplicate
If rgeFind.StoryType = 1 Then
With rgeFind.Find
.Highlight = True
.Format = True
.Wrap = wdFindStop
Do While .Execute
With .Parent
Set rgeCopy = docCopy.Range
rgeCopy.Collapse wdCollapseEnd
rgeCopy.FormattedText = .FormattedText
rgeCopy.Collapse wdCollapseEnd
rgeCopy.InsertParagraphAfter
.Collapse wdCollapseEnd
If .End = docFrom.Range.End Then Exit Do
If Asc(.Characters(1)) = 13 Then
.MoveStart wdCharacter, 1
End If
Set rgeFind = docFrom.Range(.Start, docFrom.Range.End)
End With
Loop
End With
Else
With rgeFind.Find
.Highlight = True
.Format = True
.Wrap = wdFindStop
Do While .Execute
With .Parent
Set rgeCopy = docCopy.Range
rgeCopy.Collapse wdCollapseEnd
rgeCopy.FormattedText = .FormattedText
rgeCopy.Collapse wdCollapseEnd
rgeCopy.InsertParagraphAfter
.Collapse wdCollapseEnd
If .End = rgeKeep.End Then Exit Do
If Asc(.Characters(1)) = 13 Then
.MoveStart wdCharacter, 1
End If
rgeFind.SetRange .Start, rgeKeep.End
End With
Loop
End With
End If
End Function
I needed to have two "With rgeFind.Find" because the ranges are handled
differently if they are in the main story or part of a text frame.
Here is the context:
I have a document that has Figures made up of many textboxes connected by
lines. They were created by converting embeded Visio drawings into enhanced
meta files, and then edited by right clicking on them. The Figures were
cleaned up so that they consit only of lines and textboxes containing
textframes. All useless shapes that the Edit command created have been
removed. (This complicated process was used because it would have been too
long to recreate the complex Figures from scratch within Word. It was overall
faster to convert these complex English Figures into editable ones so that
people who do not have Visio installed can easily translate them.) Those
figures are held within a 1x1 table.
Here is the bug:
All highlighted text in the main story is picked up.
Many of the shapes within the figures have highlighted text in them. Only
about 5% of the highlighted text is being picked up by the function. I
debugged the code and I know that the second ""With rgeFind.Find" above is
being executed, but the corresponding "Do While .Execute" fails, as if the
range from the textframes did not contained highlighted text when in fact it
does...
¿Qué este pasa?
I have a routine that looks for highlighted text and then copies each
instance of such found text in a new document.
The main part of the main sub goes like this:
Dim rgeDoc As Range
Dim lngWordCount As Long
Dim i As Long
Dim docNew As Document
Dim docCurrent As Document
Set rgeDoc = ActiveDocument.Range
lngWordCount = 0
Set docCurrent = ActiveDocument
Set docNew = Documents.Add
'wdMainTextStory = 1 and wdTextFrameStory = 5
CheckRange rgeDoc, docNew, docCurrent
i = 1
With docCurrent.Shapes
If .Count > 0 Then
For i = 1 To .Count
If .Item(i).Type = msoTextBox Or .Item(i).Type = msoAutoShape Then
If .Item(i).TextFrame.HasText Then
Set rgeDoc = .Item(i).TextFrame.TextRange
CheckRange rgeDoc, docNew, docCurrent
End If
End If
Next
End If
End With
I have to search each shape separately because the built-in Find ignores them.
This sub calls the following function:
Function CheckRange(ByVal rgeFind As Range, _
ByVal docCopy As Document, ByRef docFrom As Document)
Dim rgeKeep As Range
Dim rgeCopy As Range
Set rgeKeep = rgeFind.Duplicate
If rgeFind.StoryType = 1 Then
With rgeFind.Find
.Highlight = True
.Format = True
.Wrap = wdFindStop
Do While .Execute
With .Parent
Set rgeCopy = docCopy.Range
rgeCopy.Collapse wdCollapseEnd
rgeCopy.FormattedText = .FormattedText
rgeCopy.Collapse wdCollapseEnd
rgeCopy.InsertParagraphAfter
.Collapse wdCollapseEnd
If .End = docFrom.Range.End Then Exit Do
If Asc(.Characters(1)) = 13 Then
.MoveStart wdCharacter, 1
End If
Set rgeFind = docFrom.Range(.Start, docFrom.Range.End)
End With
Loop
End With
Else
With rgeFind.Find
.Highlight = True
.Format = True
.Wrap = wdFindStop
Do While .Execute
With .Parent
Set rgeCopy = docCopy.Range
rgeCopy.Collapse wdCollapseEnd
rgeCopy.FormattedText = .FormattedText
rgeCopy.Collapse wdCollapseEnd
rgeCopy.InsertParagraphAfter
.Collapse wdCollapseEnd
If .End = rgeKeep.End Then Exit Do
If Asc(.Characters(1)) = 13 Then
.MoveStart wdCharacter, 1
End If
rgeFind.SetRange .Start, rgeKeep.End
End With
Loop
End With
End If
End Function
I needed to have two "With rgeFind.Find" because the ranges are handled
differently if they are in the main story or part of a text frame.
Here is the context:
I have a document that has Figures made up of many textboxes connected by
lines. They were created by converting embeded Visio drawings into enhanced
meta files, and then edited by right clicking on them. The Figures were
cleaned up so that they consit only of lines and textboxes containing
textframes. All useless shapes that the Edit command created have been
removed. (This complicated process was used because it would have been too
long to recreate the complex Figures from scratch within Word. It was overall
faster to convert these complex English Figures into editable ones so that
people who do not have Visio installed can easily translate them.) Those
figures are held within a 1x1 table.
Here is the bug:
All highlighted text in the main story is picked up.
Many of the shapes within the figures have highlighted text in them. Only
about 5% of the highlighted text is being picked up by the function. I
debugged the code and I know that the second ""With rgeFind.Find" above is
being executed, but the corresponding "Do While .Execute" fails, as if the
range from the textframes did not contained highlighted text when in fact it
does...
¿Qué este pasa?