Restore Find box settings

N

netloss

I am trying to save and then restore the settings for the find box.
I've tried the following code, but the .style setting is always left as
'sdPart' instead of whatever the style name was before the procedure is
run.

Thanks for any advice.
---------------------------------------

Sub MessWithFindBox2()

Dim fFind As Find
Set fFind = Selection.Find

'*********************
With Selection.Find
.ClearFormatting
.Text = ""
.Style = "sdPart"
.Execute
End With
'*********************

Selection.Find.Style = fFind.Style

End sub
 
V

Vince

"sdPart" within quotes will be treated as a string and therefore it will
look for "sdPart" as through there is a style called "sdPart".

If you need the current formatting, you will just need to remove the
".clearformatting". It works like the Find and Replace box. When you select
some specific formatting and search, the next time you open the Find and
Replace box, the same formatting is shown by default. In order to clear
them, you do a ".clearformatting"

With Selection.Find
.Text = ""
.Execute
End With
 
N

netloss

What I'm trying to do is save the settings before the procedure
'MessWithFindBox2' is run. So say for example, that the style I was
searching for before I run MessWIthFindBox2 is called 'style x'. I
declare a Find object called fFind, then set it equal to the current
settings of the find box (set fFind = Selection.find). Then the code
betw/ the lines of ********** runs. When that is done, I want to
restore the .style setting in the Find box to 'style x', so from the
user's point of view, nothing has changed.
Thanks.
 
K

Klaus Linke

Hi,

Something you might try:
-- Use Range.Find instead of Selection.Find in your macro. That shouldn't mess with the current settings in the Find dialog, and you don't need to reset anything.

Not sure why your code didn't work... Probably "Set fFind = Selection.Find" sets fFind as a reference to the object "Selection.Find", and if you mess with Selection.Find, fFind also gets changed.
Unfortunately, not all objects in VBA have a .Duplicate method to avoid this... You'd probably need to iterate over anything in .Find until you get at the elementary stuff or objects that do have a .Duplicate method, and store it ("fFindFont=Selection.Find.Font.Duplicate ..."). That might get terribly convoluted.

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