Uemlue Posted November 15, 2022 Share Posted November 15, 2022 Hello Guys, sorry i am new in Autoit and i am trying to use comparing two string. The frist sring i am copying a string from excel ("Test"), saving into $var with ClipGet() function. So, now i want to compare ClipGet value $var with "Test". It should be "Matched! ", but it doesn`t work. Why? I checked the variable with MsgBox and it is "Test". Global $var sleep(2000) send("^c") sleep(2000) $var = ClipGet() Switch $var Case "Test" MsgBox(64,"","Matched !") Case Else MsgBox(64,"","Didn't match..") EndSwitch Thanks for your help... Link to comment Share on other sites More sharing options...
OJBakker Posted November 15, 2022 Share Posted November 15, 2022 Global $var sleep(2000) ; send("^c") ClipPut("Test") sleep(2000) $var = ClipGet() Switch $var Case "Test" MsgBox(64,"","Matched !") Case Else MsgBox(64,"","Didn't match..") EndSwitch gives "Matched !" so it seems you are not copying the exact string Test from Excel. Check if you are selecting/copying a space or (double) quot's with the string Test. Uemlue 1 Link to comment Share on other sites More sharing options...
Jfish Posted November 15, 2022 Share Posted November 15, 2022 I recommend the excel UDF functions to read the value from excel for your compare instead of sending ctrl+c for a generic copy. Uemlue 1 Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
Solution Subz Posted November 15, 2022 Solution Share Posted November 15, 2022 If you're using copy to clipboard from Excel then you should use StringStripWS($var, 3) Normally when you copy from a cell in Excel it appends line feed at the end of the string. StringStripWS with flag of 3, will remove any leading white space and any trailing white space. But as @Jfish mentioned, it would be better to use Excel UDF for example: Note the example below is using the default AutoIt Excel1.xls example file #include <Excel.au3> ;~ Open Excel Local $oExcel = _Excel_Open() If @error Then Exit MsgBox(4096, "Excel UDF: _Excel_RangeRead Example", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ;~ Open Workbook Local $oWorkbook = _Excel_BookOpen($oExcel, "C:\Program Files (x86)\AutoIt3\Examples\Helpfile\Extras\_Excel1.xls") If @error Then MsgBox(4096, "Excel UDF: _Excel_RangeRead Example", "Error opening workbook '" & @ScriptDir & "\Extras\_Excel1.xls'." & @CRLF & "@error = " & @error & ", @extended = " & @extended) _Excel_Close($oExcel) Exit EndIf ; Read data from a single cell on the active sheet of the specified workbook Local $sResult = _Excel_RangeRead($oWorkbook, Default, "G1") If @error Then Exit MsgBox(4096, "Excel UDF: _Excel_RangeRead Example 1", "Error reading from workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ;~ By default the _Excel1.xls G1 equals "This is a long Story" ;~ Case insensitive comparison Local $sCompare = "This is a long story" If $sResult = $sCompare Then MsgBox(4096, "Results", "Case Insensitive Match = True") Else MsgBox(4096, "Results", "Strings do not match") EndIf ;~ Case sensitive comparison ;~ Note the == operator Local $sCompare = "This is a long Story" If $sResult == $sCompare Then MsgBox(4096, "Results", "Case sensitive match = True") Else MsgBox(4096, "Results", "Case-sensitive strings do not match") EndIf Uemlue 1 Link to comment Share on other sites More sharing options...
Uemlue Posted November 15, 2022 Author Share Posted November 15, 2022 1 hour ago, Subz said: If you're using copy to clipboard from Excel then you should use StringStripWS($var, 3) Normally when you copy from a cell in Excel it appends line feed at the end of the string. StringStripWS with flag of 3, will remove any leading white space and any trailing white space. But as @Jfish mentioned, it would be better to use Excel UDF for example: Note the example below is using the default AutoIt Excel1.xls example file #include <Excel.au3> ;~ Open Excel Local $oExcel = _Excel_Open() If @error Then Exit MsgBox(4096, "Excel UDF: _Excel_RangeRead Example", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ;~ Open Workbook Local $oWorkbook = _Excel_BookOpen($oExcel, "C:\Program Files (x86)\AutoIt3\Examples\Helpfile\Extras\_Excel1.xls") If @error Then MsgBox(4096, "Excel UDF: _Excel_RangeRead Example", "Error opening workbook '" & @ScriptDir & "\Extras\_Excel1.xls'." & @CRLF & "@error = " & @error & ", @extended = " & @extended) _Excel_Close($oExcel) Exit EndIf ; Read data from a single cell on the active sheet of the specified workbook Local $sResult = _Excel_RangeRead($oWorkbook, Default, "G1") If @error Then Exit MsgBox(4096, "Excel UDF: _Excel_RangeRead Example 1", "Error reading from workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ;~ By default the _Excel1.xls G1 equals "This is a long Story" ;~ Case insensitive comparison Local $sCompare = "This is a long story" If $sResult = $sCompare Then MsgBox(4096, "Results", "Case Insensitive Match = True") Else MsgBox(4096, "Results", "Strings do not match") EndIf ;~ Case sensitive comparison ;~ Note the == operator Local $sCompare = "This is a long Story" If $sResult == $sCompare Then MsgBox(4096, "Results", "Case sensitive match = True") Else MsgBox(4096, "Results", "Case-sensitive strings do not match") EndIf First of all, thx guys. It works now. I used StringStripWS($var, 3) and now i have match. I am looking now the excel udf and trying to understand it. You awsome Big thx from a newbie Link to comment Share on other sites More sharing options...
Jfish Posted November 18, 2022 Share Posted November 18, 2022 @Uemlue my book has a chapter on automating other applications and covers some of the basics for the Excel UDF (link below). There is also excellent wiki coverage here: https://www.autoitscript.com/wiki/Excel_UDF Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now