Listing the line number of the occurrences of highligted text in adocument

A

andreas

Dear Experts:

I got a document where lots of words/characters/paragraphs have been
highlighted yellow.

Is it possible to list the line numbers of all the occurrences of
hightlighted characters in a new document.

Thank you very much in advance. Regards, Andreas
 
G

Gordon Bentley-Mix

Andreas,

The concept of a "line" is not really supported in Word, and as such, there
is no 'Lines' collection or 'Line' object in the Word OM - at least not the
kind that your looking for. This is because a "line" is a very fluid thing
and depends on (among other things) style definitions, the printer driver,
etc. And because there is no 'Line' object/'Lines' collection, there is no
'Line Number' property to use to achieve your stated goal.

The best you might be able to do is generate a list of the paragraphs that
contain highlighted text, but I'm not really sure how you would go about
doing this even. I suspect it would involve looping through the Paragraphs
collection using a counter of some sort (e.g. For i = 1 to
ActiveDocument.Paragraphs.Count) and recording the value of the counter when
highlighting is found within the paragraph. This could be EXTREMELY slow and
inefficient, so I would approach it with caution.

BTW, although the Word OM does contain something called the "LineNumbering
Property", it only applies to the "PageSetup" object and doesn't appear to
have any relationship to the actual text in the document. It seems to be more
of a visual guide rather than a navigational aide.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
T

Tony Jollans

This is not perfect but it might meet your needs - or at least provide
something to work with:

Set Doc1 = ActiveDocument
Set Doc2 = Documents.Add
Doc1.Activate
With Selection
.HomeKey wdStory
.Find.Highlight = True
While .Find.Execute
Doc2.Range.InsertAfter _
"Page: " & .Information(wdActiveEndAdjustedPageNumber) & _
" Line: " & .Information(wdFirstCharacterLineNumber)
Doc2.Range.InsertParagraphAfter
Wend
End With
Doc2.Activate
 
A

andreas

This is not perfect but it might meet your needs - or at least provide
something to work with:

    Set Doc1 = ActiveDocument
    Set Doc2 = Documents.Add
    Doc1.Activate
    With Selection
        .HomeKey wdStory
        .Find.Highlight = True
        While .Find.Execute
            Doc2.Range.InsertAfter _
                "Page: " & .Information(wdActiveEndAdjustedPageNumber) & _
                " Line: " & .Information(wdFirstCharacterLineNumber)
            Doc2.Range.InsertParagraphAfter
        Wend
    End With
    Doc2.Activate

--
Enjoy,
Tony

 www.WordArticles.com









- Zitierten Text anzeigen -


Dear Tony,

thank you for your terrific and professional help. Although I am aware
of the problems regarding the "line matter" with VBA (as described by
your colleague Gordon), the solution you came up with is exactly what
I needed. Very good job. Thank you very much.

Your code was the ideal starting point to further expand on this macro
which I did as you can see below. I added some text and font name
information as well.

Sub FontsAnalyze()
Dim doc1 As Document
Dim doc2 As Document
Set doc1 = ActiveDocument
Set doc2 = Documents.Add
doc1.Activate
With Selection
.HomeKey wdStory
.Find.Highlight = True
While .Find.Execute
doc2.range.InsertAfter _
"Page: " & .Information(wdActiveEndAdjustedPageNumber)
& _
" / Line: " & .Information(wdFirstCharacterLineNumber)
& _
" / Text: " & .range.Text & _
" / Font Name: " & .range.Font.Name
doc2.range.InsertParagraphAfter
Wend
End With
doc2.Activate
End Sub



Again, thank you very much for your superb help. Regards, Andreas
 
A

andreas

Andreas,

The concept of a "line" is not really supported in Word, and as such, there
is no 'Lines' collection or 'Line' object in the Word OM - at least not the
kind that your looking for. This is because a "line" is a very fluid thing
and depends on (among other things) style definitions, the printer driver,
etc. And because there is no 'Line' object/'Lines' collection, there is no
'Line Number' property to use to achieve your stated goal.

The best you might be able to do is generate a list of the paragraphs that
contain highlighted text, but I'm not really sure how you would go about
doing this even. I suspect it would involve looping through the Paragraphs
collection using a counter of some sort (e.g. For i = 1 to
ActiveDocument.Paragraphs.Count) and recording the value of the counter when
highlighting is found within the paragraph. This could be EXTREMELY slow and
inefficient, so I would approach it with caution.

BTW, although the Word OM does contain something called the "LineNumbering
Property", it only applies to the "PageSetup" object and doesn't appear to
have any relationship to the actual text in the document. It seems to be more
of a visual guide rather than a navigational aide.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.








- Zitierten Text anzeigen -

Dear Gordon,

thank you so much for your in-depth explanation of the „line-
problem“ .

Your colleague – Tom – as you can see further down the thread found a
way around this limitation that ideally suits my needs. Anyhow, thank
you very much for your professional help. I really appreciate it very
much.
Regards, Andreas
 
A

andreas

This is not perfect but it might meet your needs - or at least provide
something to work with:

    Set Doc1 = ActiveDocument
    Set Doc2 = Documents.Add
    Doc1.Activate
    With Selection
        .HomeKey wdStory
        .Find.Highlight = True
        While .Find.Execute
            Doc2.Range.InsertAfter _
                "Page: " & .Information(wdActiveEndAdjustedPageNumber) & _
                " Line: " & .Information(wdFirstCharacterLineNumber)
            Doc2.Range.InsertParagraphAfter
        Wend
    End With
    Doc2.Activate

--
Enjoy,
Tony

 www.WordArticles.com









- Show quoted text -


Tony,
There is one thing I would like to ask you concerning your macro. Is
it possible to copy the formatted highlighted text of the source
document into the target document?

Help is much appreciated. Thank you very much in advance. Regards,
Andreas
 
T

Tony Jollans

Hi Andreas,

Untested, but I think you should be able to do it just by adding a couple of
lines (or changing the code in some equivalent way):

After:
Doc2.Range.InsertParagraphAfter
And before:
Wend

Add:
Doc2.Range.InsertAfter .Text
Doc2.Range.InsertParagraphAfter

--
Enjoy,
Tony

www.WordArticles.com

This is not perfect but it might meet your needs - or at least provide
something to work with:

Set Doc1 = ActiveDocument
Set Doc2 = Documents.Add
Doc1.Activate
With Selection
.HomeKey wdStory
.Find.Highlight = True
While .Find.Execute
Doc2.Range.InsertAfter _
"Page: " & .Information(wdActiveEndAdjustedPageNumber) & _
" Line: " & .Information(wdFirstCharacterLineNumber)
Doc2.Range.InsertParagraphAfter
Wend
End With
Doc2.Activate

--
Enjoy,
Tony

www.WordArticles.com









- Show quoted text -


Tony,
There is one thing I would like to ask you concerning your macro. Is
it possible to copy the formatted highlighted text of the source
document into the target document?

Help is much appreciated. Thank you very much in advance. Regards,
Andreas
 
A

andreas

Hi Andreas,

Untested, but I think you should be able to do it just by adding a coupleof
lines (or changing the code in some equivalent way):

After:
    Doc2.Range.InsertParagraphAfter
And before:
    Wend

Add:
    Doc2.Range.InsertAfter .Text
    Doc2.Range.InsertParagraphAfter

--
Enjoy,
Tony

 www.WordArticles.com






Tony,
There is one thing I would like to ask you concerning your macro. Is
it possible to copy the formatted highlighted text of the source
document into the target document?

Help is much appreciated. Thank you very much in advance. Regards,
Andreas- Hide quoted text -

- Show quoted text -

Hey Tony,

thank you very much for your quick help. You may have gotten my
question wrong. I would like the highlighted text to be copied into
the target document as formatted text. That is, this highlighted text
may be underlined or italic or have some specific font. I hope you get
what I mean with englisch being not my mother tongue.
Regards, Andreas
 
D

Doug Robbins - Word MVP on news.microsoft.com

In your code, use

" / Text: " & .range.FormattedText &

in place of

" / Text: " & .range.Text &
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

Hi Andreas,

Untested, but I think you should be able to do it just by adding a couple
of
lines (or changing the code in some equivalent way):

After:
Doc2.Range.InsertParagraphAfter
And before:
Wend

Add:
Doc2.Range.InsertAfter .Text
Doc2.Range.InsertParagraphAfter

--
Enjoy,
Tony

www.WordArticles.com






Tony,
There is one thing I would like to ask you concerning your macro. Is
it possible to copy the formatted highlighted text of the source
document into the target document?

Help is much appreciated. Thank you very much in advance. Regards,
Andreas- Hide quoted text -

- Show quoted text -

Hey Tony,

thank you very much for your quick help. You may have gotten my
question wrong. I would like the highlighted text to be copied into
the target document as formatted text. That is, this highlighted text
may be underlined or italic or have some specific font. I hope you get
what I mean with englisch being not my mother tongue.
Regards, Andreas
 
A

andreas

In your code, use

                " / Text: " & .range.FormattedText &

in place of

                " / Text: " & .range.Text &
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com











Hey Tony,

thank you very much for your quick help. You may have gotten my
question wrong. I would like the highlighted text to be copied into
the target document as formatted text. That is, this highlighted text
may be underlined or italic or have some specific font. I hope you get
what I mean with englisch being not my mother tongue.
Regards, Andreas- Hide quoted text -

- Show quoted text -

Hi Dough,
thank you for your swift reply. I am afraid to say that no formatted
text is copied into the target document with this adjustment of the
code.
Regards, Andreas
 
D

Doug Robbins - Word MVP on news.microsoft.com

Hi Andrea,

Use:

Dim doc1 As Document
Dim doc2 As Document
Dim drange As Range, drange2 As Range
Set doc1 = ActiveDocument
Set doc2 = Documents.Add
doc1.Activate
With Selection
.HomeKey wdStory
.Find.Highlight = True
While .Find.Execute
Set drange = doc2.Range
drange.Collapse wdCollapseEnd
drange.FormattedText = .Range.FormattedText
drange.InsertBefore "Page: " &
..Information(wdActiveEndAdjustedPageNumber) & _
" / Line: " & .Information(wdFirstCharacterLineNumber) & _
" / Text: "
drange.InsertAfter " / Font Name: " & .Range.Font.Name & vbCr
Set drange2 = drange.Duplicate
drange2.End = drange2.Start + InStr(drange2, " / Text: ") + 8
drange2.Font.Reset
drange2.HighlightColorIndex = wdAuto
Set drange2 = drange.Duplicate
drange2.Start = drange2.Start + InStrRev(drange, "/") - 2
drange2.Font.Reset
drange2.HighlightColorIndex = wdAuto
Wend
End With
doc2.Activate


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

In your code, use

" / Text: " & .range.FormattedText &

in place of

" / Text: " & .range.Text &
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com











Hey Tony,

thank you very much for your quick help. You may have gotten my
question wrong. I would like the highlighted text to be copied into
the target document as formatted text. That is, this highlighted text
may be underlined or italic or have some specific font. I hope you get
what I mean with englisch being not my mother tongue.
Regards, Andreas- Hide quoted text -

- Show quoted text -

Hi Dough,
thank you for your swift reply. I am afraid to say that no formatted
text is copied into the target document with this adjustment of the
code.
Regards, Andreas
 
T

Tony Jollans

Instead of adding

Doc2.Range.InsertAfter .Text

Try adding

Doc2.Range(Doc2.Range.End - 1).FormattedText = .FormattedText

--
Enjoy,
Tony

www.WordArticles.com

Hi Andreas,

Untested, but I think you should be able to do it just by adding a couple
of
lines (or changing the code in some equivalent way):

After:
Doc2.Range.InsertParagraphAfter
And before:
Wend

Add:
Doc2.Range.InsertAfter .Text
Doc2.Range.InsertParagraphAfter

--
Enjoy,
Tony

www.WordArticles.com






Tony,
There is one thing I would like to ask you concerning your macro. Is
it possible to copy the formatted highlighted text of the source
document into the target document?

Help is much appreciated. Thank you very much in advance. Regards,
Andreas- Hide quoted text -

- Show quoted text -

Hey Tony,

thank you very much for your quick help. You may have gotten my
question wrong. I would like the highlighted text to be copied into
the target document as formatted text. That is, this highlighted text
may be underlined or italic or have some specific font. I hope you get
what I mean with englisch being not my mother tongue.
Regards, Andreas
 
A

andreas

Instead of adding

    Doc2.Range.InsertAfter .Text

Try adding

    Doc2.Range(Doc2.Range.End - 1).FormattedText = .FormattedText

--
Enjoy,
Tony

 www.WordArticles.com











Hey Tony,

thank you very much for your quick help. You may have gotten my
question wrong. I would like the highlighted text to be copied into
the target document as formatted text. That is, this highlighted text
may be underlined or italic or have some specific font. I hope you get
what I mean with englisch being not my mother tongue.
Regards, Andreas- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Hi Tony,

ok, Thank you very much. This did the trick. Superb help. Regards,
Andreas
 
A

andreas

Hi Andrea,

Use:

    Dim doc1 As Document
    Dim doc2 As Document
    Dim drange As Range, drange2 As Range
    Set doc1 = ActiveDocument
    Set doc2 = Documents.Add
    doc1.Activate
    With Selection
        .HomeKey wdStory
        .Find.Highlight = True
        While .Find.Execute
            Set drange = doc2.Range
            drange.Collapse wdCollapseEnd
            drange.FormattedText = .Range.FormattedText
            drange.InsertBefore "Page: " &
.Information(wdActiveEndAdjustedPageNumber) & _
                " / Line: " & .Information(wdFirstCharacterLineNumber) & _
                " / Text: "
            drange.InsertAfter " / Font Name: " & .Range.Font..Name & vbCr
            Set drange2 = drange.Duplicate
            drange2.End = drange2.Start + InStr(drange2, " / Text: ") + 8
            drange2.Font.Reset
            drange2.HighlightColorIndex = wdAuto
            Set drange2 = drange.Duplicate
            drange2.Start = drange2.Start + InStrRev(drange, "/") - 2
            drange2.Font.Reset
            drange2.HighlightColorIndex = wdAuto
        Wend
    End With
    doc2.Activate

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com














Hi Dough,
thank you for your swift reply. I am afraid to say that no formatted
text is copied into the target document with this adjustment of the
code.
Regards, Andreas- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Hey Doug,

superb code. Exactly what I wanted. Great job. Thank you very much for
your professional help. Regards, Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top