Jump to content

Recommended Posts

Posted

Long time watcher, first time poster. I'm a HUGE fan of AutoIt and love what it can do!

I'm working on a script that will escape certain characters in an Excel with a '\'.  Everything works perfectly except for a few lines of code. Please see below:

Func _escapeWildcard($varExcel)
    Local $oExcel =_Excel_Open()
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeRead Example", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

    Local $oWorkbook = _Excel_BookOpen($oExcel, $varExcel)  ;(False, False, False, False, False)
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_BookOpen Example", "Error opening workbook: " & $UIFile & @CRLF & "@error = " & @error & ", @extended = " & @extended)
        _Excel_Close($oExcel)
        Exit
    EndIf

    _Excel_RangeReplace($oWorkbook, Default, Default, "?", "\?")
    _Excel_RangeReplace($oWorkbook, Default, Default, "*", "\*")
    _Excel_RangeReplace($oWorkbook, Default, Default, ".", "\.")

    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeReplace Example 2", "Error replacing data in the range." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

    _Excel_Close($oExcel, False)
EndFunc

The issue I'm having is with the lines to escape '?' and '*', as RangeReplace seems to be treating these as wildcards. My question is, how do I bypass the wildcard value of '?' and '*' and treat these as literal characters?

The problem lines are here:

_Excel_RangeReplace($oWorkbook, Default, Default, "?", "\?")
_Excel_RangeReplace($oWorkbook, Default, Default, "*", "\*")

Example pre-script string:

'Did you go to the store?'

Desired output:

'Did you go to the store\?'

Actual output

'\?\?\?\?\?\?\?\?\?\?\?\?\?\?'

 

With * it yields the following:

Example pre-script string:

'Timmy went to the store *'

Desired output:

'Timmy went to the store \*'

Actual output

'\*'

 

As a note, the following line to escape '.' works perfectly:

_Excel_RangeReplace($oWorkbook, Default, Default, ".", "\.")

Example pre-script string:

'I went to the store.'

Desired and actual output:

'I went to the store\.'

Posted

No sooner did I submit this character that I realized that the Excel escaping rules are being used (of course). You must escape these wildcards with a '~' character during the find. Lines are changed to the following and worked!

_Excel_RangeReplace($oWorkbook, Default, Default, "~?", "\?")
_Excel_RangeReplace($oWorkbook, Default, Default, "~*", "\*")

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...