Changes to lines starting with an apostrophe

W

wleonard47

Good day,

I am trying to change the font color for any line in a document that begins
with an apostrophe. I do this to highlight the comment lines when I have
pasted VBA code into a Word doc after copying it from the VB editor. I have
succeded in being able to do this when the first character of a line is A or
B or C, etc., but cannot get the macro to detect the apostrophe symbol. I
tried " ' " and Chr(39), but to no avail. Is there some magic regarding the
apostrophe? Code follows:

Sub wflFindandReplaceTest1()

Dim X As Integer
Dim rngParagraph As Range
Dim intParaCount As Integer

intParaCount = ActiveDocument.Paragraphs.Count
For X = 1 To intParaCount
Set rngParagraph = ActiveDocument.Paragraphs(X).Range
If rngParagraph.Characters.First = Chr(84) Then
rngParagraph.Font.Color = wdColorGreen
End If

Next X

End Sub

Thank you

Bill Leonard
Hingham, MA
 
H

Helmut Weber

Hi,

this one works for me, as well as your code.

Sub Test4()
Dim rPrg As Paragraph
For Each rPrg In ActiveDocument.Range.Paragraphs
If rPrg.Range.Characters.First = Chr(39) Then
rPrg.Range.Font.Color = wdColorGreen
End If
Next
End Sub

Are you sure there are no leading spaces?

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
T

Tony Jollans

Do you really have a straight quote? Or has Word changed it to a curly one?
Try looking for ChrW(8216).
 
W

wleonard47

Helmut,

Greetings back to you from Hingham, Massachusetts (10 miles south of
Boston).

Thank you for the response. Suddenly it works for me as well. I had tried
both 84 and 39 to no avail. When I subsequently returned to this effort, the
macro was working as originally written. I think I might not have been
properly compiling the code between attempts--does this make any sense?

Thank you once again.

Bill Leonard
 
W

wleonard47

Hi Tony,

I have seen both and worried that the character might have been something
different. All is well now--as I detailed in the response to Helmut.

Thanks very much.

Regards,

Bill Leonard
 
H

Helmut Weber

Hi,
I think I might not have been
properly compiling the code between attempts
--does this make any sense?

no.
In VBA you can't influence compiling at all,
apart from some compiler directives.

Just must have been one of those days...

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
R

Russ

Bill,
You might find this routine speedier and more versatile.
It uses wdReplaceAll which is speedier and it colors from first found
apostrophe in paragraph to end of paragraph.
If selection is an insertion point, then whole document is acted on.
If selection is one or more characters then the only selection's
paragraph(s) are acted on.
Apostrophe search can be complicated by the possible usage of 'Smart Quotes'
characters. (Enabled as option in the AutoCorrect/AutoFormat dialog) I tried
to include the character codes I knew might be single smart quote
characters.

Sub ColorCommentsGreen()
Dim aRange As Word.Range

If Selection.Type = wdSelectionIP Then 'No text selected?
Set aRange = ActiveDocument.Content 'then work one whole main body
Else
Set aRange = Selection.Range 'Otherwise work on selected text.
aRange.Expand unit:=wdParagraph 'Expand to full paragraph
End If
With aRange.Find
.Text = "[^39^145^146^213^212]*^13" 'smart quote apostrophes
'use \n in place of ^13 in MacWord
.Replacement.Font.Color = wdColorGreen
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub
 
W

wleonard47

Hi Russ,
Sorry to be so long in getting back to you on this very helpful suggestion.
This is exactly what I was looking to do. Thanks very much.
Bill

Russ said:
Bill,
You might find this routine speedier and more versatile.
It uses wdReplaceAll which is speedier and it colors from first found
apostrophe in paragraph to end of paragraph.
If selection is an insertion point, then whole document is acted on.
If selection is one or more characters then the only selection's
paragraph(s) are acted on.
Apostrophe search can be complicated by the possible usage of 'Smart Quotes'
characters. (Enabled as option in the AutoCorrect/AutoFormat dialog) I tried
to include the character codes I knew might be single smart quote
characters.

Sub ColorCommentsGreen()
Dim aRange As Word.Range

If Selection.Type = wdSelectionIP Then 'No text selected?
Set aRange = ActiveDocument.Content 'then work one whole main body
Else
Set aRange = Selection.Range 'Otherwise work on selected text.
aRange.Expand unit:=wdParagraph 'Expand to full paragraph
End If
With aRange.Find
.Text = "[^39^145^146^213^212]*^13" 'smart quote apostrophes
'use \n in place of ^13 in MacWord
.Replacement.Font.Color = wdColorGreen
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub
Hi Tony,

I have seen both and worried that the character might have been something
different. All is well now--as I detailed in the response to Helmut.

Thanks very much.

Regards,

Bill Leonard
 
W

wleonard47

Hi Shauna,
Thank you. I was not surprized to see that others have considered this.
Well done. Thanks again.

Bill
 

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