find and replace behavior

W

ward

Hello,

I use the command
SendKeys "^{h}"
to show the Find And Replace dialog box with some
predefined settings (see code below).

However, after some time of working (hours, ...) users
report that suddenly when they click the button that will
invoke the correct F&R action (and presents them with the
dialog box), the character preceding the cursor is
deleted. Or, if some text is selected at that moment, it
is deleted. Note that they didn't performed any action on
the F&R dialog box yet.
From then on, all documents that are open (or will be
opened), suffer from this behaviour. So Word has to be
closed and restarted.
I wouldn't believe them, if i had not seen and tried it
myself!

It might be important to note that - when this strange
behaviour starts - manually using CTRL-H to bring up te
F&R dialog works correctly (no charater is deleted, or
selected text is not replaced)

I can not reproduce this behaviour, it just seems to pop-
up after some undefined time of working.

Anybody knows what's happening? Can I use an other way
than Sendkeys to bring up the F&R dialog box?

Ward

----- CODE-----

Public Sub FRspaceComma()
'This is invoked by pressing a specific button on a toolbar
FindAndReplace " ,", ","
End Sub



Private Sub FindAndReplace(FindText As String, ReplaceText
As String, Optional DoFR As Boolean = False)
'Show F&R dialog box and if specified, execute F&R action

'Set F&R arguments
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = FindText
.Replacement.Text = ReplaceText
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

If DoFR Then
'execute F&R action
Selection.Find.Execute Replace:=wdReplaceAll
Else
'Show F&R dialog box
SendKeys "^{h}"
End If

End Sub
 
H

Helmut Weber

Hi Ward,
as easy as this, unless I have overlooked
a hidden complication:

Dialogs(wdDialogEditReplace).Show

But as I see from you code, if you want to do that
simple find & replace, there are other ways.

Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word XP, NT 4.0
 
W

ward

Thanks Helmut,

A drawback of using the dialog.show method is that the
find tab is disabled, so users can not switch to the find
tab.
But i'll give it a try for 2 days and see if any problem
pops up.

Ward
 
K

Klaus Linke

ward said:
A drawback of using the dialog.show method is that the
find tab is disabled, so users can not switch to the find
tab.


Hi Ward,

You could try

Dim myCBC As CommandBarControl
Set myCBC = CommandBars.FindControl(ID:=313)
myCBC.accDoDefaultAction

But since .accDoDefaultAction isn't documented (as far as I know), this may
be a bit risky.
I'd test it a while at least, before distributing a solution using that
trick to a lot of users.

(ID:=313 is the ID for the built-in command/control "EditReplace")

Regards,
Klaus
 
K

Klaus Linke

You can also use
CommandBars.FindControl(ID:=313).Execute

I wrongly thought this would directly execute the Find/Replace, but it, too,
displays the dialog with none of the tabs grayed out.

Klaus
 
W

ward

Hello Klaus,

Thanks for your reply. I've just implemented your
sollution and i'm awaiting any reactions from the users.

BTW: why do you use this command instead of the dialogs
().show or SendKeys solution? Beacuse of the greyed out
tabs, or because of the same problem (characters getting
deleted)?

Regards,
Ward
 
K

Klaus Linke

BTW: why do you use this command instead of the
dialogs().show or SendKeys solution? Beacuse of
the greyed out tabs, or because of the same problem
(characters getting deleted)?


Hi Ward,

To get around the greyed-out tabs. Both with showing the dialog or with
calling the built-in WordBasic-command, they always seem to be greyed out.

I don't really have a good idea why SendKeys quits working after a while in
your macro, but it's always a kludge that is better avoided if possible...
though it comes in handy in some desperate cases when everything else fails.

Greetings,
Klaus
 

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