Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2006

Tip: Looking for answers? Try searching our database.

Select text with macro based on criteria

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Josha - 07 Sep 2006 03:21 GMT
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 - 07 Sep 2006 03:57 GMT
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

> Hi,
>
[quoted text clipped - 14 lines]
>
> Josha
Josha - 07 Sep 2006 05:08 GMT
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

> 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
[quoted text clipped - 31 lines]
> >
> > Josha
Jezebel - 07 Sep 2006 05:34 GMT
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.

> Thanks, I know I am being very persistent. Basically I decided that if
> even
[quoted text clipped - 61 lines]
>> >
>> > Josha
Josha - 07 Sep 2006 06:05 GMT
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.

> 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
[quoted text clipped - 65 lines]
> >> >
> >> > Josha
Jezebel - 07 Sep 2006 06:37 GMT
'Or something' is what you'll need, yes.

> ok... so would i need to use a wend command or something to loop the
> select
[quoted text clipped - 76 lines]
>> >> >
>> >> > Josha
Josha - 07 Sep 2006 07:07 GMT
: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

> 'Or something' is what you'll need, yes.
>
[quoted text clipped - 78 lines]
> >> >> >
> >> >> > Josha
Jezebel - 07 Sep 2006 07:32 GMT
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.

> :P is this one of those "i wont tell you so you learn things" or is it a
> "not
[quoted text clipped - 92 lines]
>> >> >> >
>> >> >> > Josha
Jean-Guy Marcil - 07 Sep 2006 13:20 GMT
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
'_______________________________________

Signature

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

Doug Robbins - Word MVP - 08 Sep 2006 21:39 GMT
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

Signature

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

> Thanks, I know I am being very persistent. Basically I decided that if
> even
[quoted text clipped - 61 lines]
>> >
>> > Josha
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.