View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Dan Williams Dan Williams is offline
external usenet poster
 
Posts: 23
Default Determine Find/Replace settings and then restore them?

Below is the code I use in Microsoft Word to preserve my Search/Replace
settings when I run VBA that uses Search/Replace and would normally
leave the settings changed.

It doesn't look as easy in Excel. Is there a way to obtain the
settings, or at least the search string? If so, I guess a clumsy way
to restore them would be to open a temporary window and do an actual
Find using the original string and other settings, then close the
window. But I expect there's a more elegant way.

Dan Williams
danwPlanet

________________________________________________

Call RememberFindReplaceSettings("Memorize")
' Find/Replace code goes here.
Call RememberFindReplaceSettings("Restore")

Sub RememberFindReplaceSettings(myMode As String)

Static myOldFindText As String
Static myOldReplText As String
Static myOldDirectio As Boolean
Static myOldWrapping As Integer
Static myOldFormatNo As Boolean
Static myOldMatchCas As Boolean
Static myOldWholWord As Boolean
Static myOldWildcard As Boolean
Static myOldSounLike As Boolean
Static myOldAllWdFms As Boolean
Static myOldFindStyl As String
Static myOldReplStyl As String

With Selection.Find
If myMode = "Memorize" Then
myOldFindStyl = .Style
myOldReplStyl = .Replacement.Style
myOldFindText = .Text
myOldReplText = .Replacement.Text
myOldDirectio = .forward
myOldWrapping = .Wrap
myOldFormatNo = .Format
myOldMatchCas = .MatchCase
myOldWholWord = .MatchWholeWord
myOldWildcard = .MatchWildcards
myOldSounLike = .MatchSoundsLike
myOldAllWdFms = .MatchAllWordForms
ElseIf myMode = "Restore" Then
If myOldFindStyl < "" Then .Style = myOldFindStyl
If myOldReplStyl < "" Then .Replacement.Style =
myOldReplStyl
.Text = myOldFindText
.Replacement.Text = myOldReplText
.forward = myOldDirectio
.Wrap = myOldWrapping
.Format = myOldFormatNo
.MatchCase = myOldMatchCas
.MatchWholeWord = myOldWholWord
.MatchWildcards = myOldWildcard
.MatchSoundsLike = myOldSounLike
.MatchAllWordForms = myOldAllWdFms
End If
End With

End Sub