Need to search for criteria that doesn't exist

D

dedawson

A simple problem. I can easily define Search criteria to find the
first occurence of text formatted as wdEnglishUS (or any other
language, for that matter). What I need to be able to do is define a
Search that finds the first text that is NOT wdEnglishUS.

I can brute force it by Replacing all wdEnglishUS text with Highlight,
conducting a second Search for Not Highlight (thereby identifying the
text that is NOT wdEnglishUS), followed by a third Replace to remove
the Highlighted text. The problem with this approach, of course, is
that if there was anything already Highlighted before I began, it will
be lost when I'm done.

Any suggestions will be gratefully accepted.

david
 
C

Carolin

I presume you are after a vba solution. How about the following:

Dim b As Language
Selection.HomeKey Unit:=wdStory

For Each b In Languages
If b.ID = wdEnglishUS Then GoTo Cont
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.LanguageID = b.ID
.Execute Wrap:=wdFindContinue, Forward:=True
End With
While Selection.Find.Found
Selection.LanguageID = wdEnglishUS
Selection.Find.Execute Wrap:=wdFindContinue
Wend
Cont:
Next
 
T

Tony Jollans

It rather depends what you want to do when you have found the non-US English
text, but this will find the first run

Dim USEnglish As Range
Dim FStart As Long, FEnd As Long
Set USEnglish = ActiveDocument.Range
With USEnglish.Find
.Format = True
.LanguageID = wdEnglishUS
.Execute
FStart = USEnglish.End
.Execute
FEnd = USEnglish.Start
End With
ActiveDocument.Range(FStart, FEnd).Select

It will need a bit of tweaking to cope with there not being any or it being
at the start or end of the document, or wanting to find multiple runs, etc.,
but I don't want to do that before knowing more of what you want.
 
D

dedawson

Thanks Carolin,

Your code showed me the path for identifying the text I wanted to
process. An added benefit is that it not only allows me to find text
that is NOT wdEnglishUS, but also allows me to know exactly what
language been found, including wdNoProofing, which I would not want to
change.

This added benefit also revealed a gotcha that anyone else needing to
do this sort of thing will need to allow for. If the LanguageID that
is being processed is wdNoProofing (1024), the While
Selection.Find.Found loop will hang, because setting the LanguageID =
wdEnglishUS will not clear the wdNoProofing from the text.

david
 
D

dedawson

Thanks Tony,

Your code gave me a better understanding of ways to identify the text
I wanted to process, and is a cool approach to boot; find where one
wdEnglishUS ends and where the next one starts, and what's in between
is obviously non-wdEnglishUS. The code can easily be incorporated
into a larger chunk to process an entire document.

In playing around, I did discover one interesting anomaly, that is
apparently a function of how Word does its Find. If the non-
wdEnglishUS text appears in the first paragraph, it is correctly
selected. Similarly, if if begins in the first paragaph, and
continues into the following paragraph(s), it is also correctly
selected. If however, the first occurrence does not appear until
later paragraphs, the code moves the Selection to the beginning of the
second paragraph. This is because when Word finds the first occurence
of wdEnglishUS, it only selects through the paragraph mark, even
though subsequent paragraphs are also wdEnglishUS. Is this just the
way Word works, or is there some means of directing Word to not stop
at the paragraph mark?

david
 
C

Carolin

Just another thought....are you purely wanting to make everything EnglishUS?
If so, you could always just select the whole doc and set the language. But
I guess this is just too obvious.
 
D

dedawson

Hi Carolin,

Actually, this was more of a learning exercise triggered by my
annoyance at receiving some Msgboxs telling me something along the
lines of 'text marked as French... ' Until this occurred I didn't
even know there was any such text in my docs. I had no idea how it
got there, but now, of course, I needed to find a way to find all this
ahead of time instead of waiting for it to happen again; part of my
System Test background, you know.

Thanks again for your help.

david
 

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