I am giveing my first shot at useing UDFs, and am haveing a problem. I am trying to replace all occurances of ( in a sendkeys with {(}. This script is not working though. Can someone give me some pointers as to where my problem is?

I tried it with the UDF below my code first. It did not work that way. Next I tried it as shown. It did nto work this way either. After that I was stumped

Code:
  
Function Replace($SourceString, $SearchString, $ReplaceString, Optional $First, Optional $CaseSensitive)
Dim $String1
Dim $String2
Dim $Finished $Finished = 0
$Counter = 0
$String1 = $SourceString
If $CaseSensitive
$PreviousState = SetOption("CaseSensitivity", "On")
EndIf
While Not $Finished
$String2 = $String1
$Location = InStr($String1, $SearchString)
If $Location > 0
$String1 = Substr($String1, 1, $Location - 1) + $ReplaceString + Substr($String1, $Location + Len($SearchString), Len($SourceString) - $Location + Len($SearchString) + 1)
Else
$Finished = 1
EndIf
If $First
$Finished = 1
EndIf
If $String1 = $String2
$Finished = 1
EndIf
Loop
$Replace = $String1
$Nul = SetOption("CaseSensitivity", $PreviousState)
EndFunction

Sleep(2)
$string = "test()this()"
$string = Replace($string,"(","{(}")
$string = Replace($string,")","{)}")
SendKeys($String)