Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Select text with macro based on criteria

1 view
Skip to first unread message

Josha

unread,
Sep 7, 2006, 2:21:02 AM9/7/06
to
Hi,

I am a newbie trying to self teach VB. I am pretty useless so please be
gentle.

I am trying to write a macro to allow me to select text in MS Word based
upon its case. I want to select all the text in the document that is in all
caps. Is this possible?

Also, is it possible to exclude 34 specific two letter strings of text? The
reason for this that specific "class codes" need to appear in in caps.

Can anyone help? As i said, i am pretty useless at this stage, so sample
code would be awsome.

Josha

Jezebel

unread,
Sep 7, 2006, 2:57:22 AM9/7/06
to
Presumably this relates to your previous post about correcting upper-case
names. The problem here is not so much the VBA code as specifying what the
code has to do. Which is the first lesson of programming: each hour of
design effort saves you ten hours of coding.

Do you really want to *select* the uppercase text? -- or do you want to find
it and do something with it?

At its simplest, you could use code like --

Dim pChar As Variant
For Each pChar In ActiveDocument.Characters
If pChar Like "[A-Z]" Then
...
End if
Next


"Josha" <Jo...@discussions.microsoft.com> wrote in message
news:B631DF8E-4CFA-4F42...@microsoft.com...

Josha

unread,
Sep 7, 2006, 4:08:02 AM9/7/06
to
Thanks, I know I am being very persistent. Basically I decided that if even
if i could select the text with a macro, then I could use the Format >>
Change Case command to change the text as I needed.

I used you code which worked when i inserted:

Sub Test2()

Dim pChar As Variant
For Each pChar In ActiveDocument.Characters
If pChar Like "[A-Z]" Then

pChar.Select
End If
Next


End Sub


But it would only select the last letter in the document that was caps. Any
ideas?

Thbaks

Josha

Jezebel

unread,
Sep 7, 2006, 4:34:18 AM9/7/06
to
What your code does is select each character in turn, until it reaches the
end of the document, leaving the last one selected. Run it by pressing F8
repeatedly to see what's going on.


"Josha" <Jo...@discussions.microsoft.com> wrote in message

news:7375E901-DAAC-4CB8...@microsoft.com...

Josha

unread,
Sep 7, 2006, 5:05:01 AM9/7/06
to
ok... so would i need to use a wend command or something to loop the select
and add the selections togeather. As i said... i have no idea.

Jezebel

unread,
Sep 7, 2006, 5:37:27 AM9/7/06
to
'Or something' is what you'll need, yes.


"Josha" <Jo...@discussions.microsoft.com> wrote in message

news:14B34489-26C6-46E3...@microsoft.com...

Josha

unread,
Sep 7, 2006, 6:07:01 AM9/7/06
to
:P is this one of those "i wont tell you so you learn things" or is it a "not
possible" or "i dont know" things

Jezebel

unread,
Sep 7, 2006, 6:32:15 AM9/7/06
to
This is a "you're starting in absolutely the wrong place to solve this
problem" kind of thing. I could write you whole essays on ways you could
approach this task, but you've resolutely ignored all the previous
suggestions I and others have offered.

Go buy a book.

"Josha" <Jo...@discussions.microsoft.com> wrote in message

news:35C1D89E-4612-433C...@microsoft.com...

Jean-Guy Marcil

unread,
Sep 7, 2006, 12:20:37 PM9/7/06
to
Josha was telling us:
Josha nous racontait que :

> ok... so would i need to use a wend command or something to loop the
> select and add the selections togeather. As i said... i have no idea.
>

AS others have said, this is easier said than done. Since you seem you
really want to do this, here is some code that will get you started. Be
warned that there are many problems ahead: Just to state one: How to deal
with compound words that needs to be capitalized (Dominican Republic)?

Also, dealing with characters in a document makes for code that is really
slow... you may want to change the approach for words, but it will still be
slow...

It would be better to find a way to do this with Find/Replace.

'_______________________________________
Dim pChar As Range
Dim rgeTemp As Range
Dim lngRgeStart As Long
Dim lngRgeEnd As Long
Dim boolFoundCaps As Boolean

lngCharCount = ActiveDocument.Characters.Count
boolFoundCaps = False

For Each pChar In ActiveDocument.Characters
If pChar Like "[A-Z]" Then

If Not boolFoundCaps Then
lngRgeStart = pChar.Start
boolFoundCaps = True
End If
Else
If boolFoundCaps Then
lngRgeEnd = pChar.Start
boolFoundCaps = False
Set rgeTemp = ActiveDocument.Range(lngRgeStart, lngRgeEnd)
If rgeTemp.Characters.Count <> 1 Then
If Not rgeTemp.Text Like "BP" And _
Not rgeTemp.Text Like "MT" And _
Not rgeTemp.Text Like "RS" Then
rgeTemp.Case = wdLowerCase
End If
End If
End If
End If
Next
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarci...@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org


Doug Robbins - Word MVP

unread,
Sep 8, 2006, 8:39:45 PM9/8/06
to
Use the following:

Dim drange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="[A-Z]{2,}", MatchWildcards:=True,
Wrap:=wdFindContinue, Forward:=True) = True
Set drange = Selection.Range
drange.Case = wdTitleWord
Loop
End With


--
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

"Josha" <Jo...@discussions.microsoft.com> wrote in message

news:7375E901-DAAC-4CB8...@microsoft.com...

0 new messages