MK_2019 Posted August 9, 2019 Share Posted August 9, 2019 Hello, Am New to AutoIT, I Tried to search for Solution in the Forum, But could Not find. Hence, Posting this Query. I Have Different .au3 Files (Main.au3, File2.au3) Am calling File2.au3 from Main.au3. Am trying to increment the "$cell" value in Main.au3, so that, the value is captured in next cell of spreadsheet. The Problem is, Even-though i have Declared Global Variable value (for $cell) in Main.au3,.. Local variable value (for $cell) From File2.au3 is Displayed in Spreadsheet... And $cell value is NOT getting Incremented. Am Expecting Global Variable Value (for $cell) and increment in Main.au3. Any Help is appreciated. ;~ <File2.au3> ;~.......... <File2.au3> .......... Local $cell = 1 Local $Description ="TTT3" Local $var = "C:\AutoIT_Excel_Report\Book3.xlsx" Local $oExcel = _Excel_Open (True) Local $oWorkbook = _Excel_BookOpen ($oExcel, $var) $range = _Excel_RangeWrite ($oWorkbook, Default, $Description, "A" &$cell) _Excel_BookSave ($oWorkbook) Sleep (2000) _Excel_BookClose ($oWorkbook) Sleep(2000) _Excel_Close ($oExcel) $cell +=1 ;~.......... <File2.au3> .......... ;~ <Main.au3> ;~.......... <Main.au3> .......... #include <File2.au3> Global $cell = 4 Global $Description = "Desc" $sFilePath = "C:\xxx\xxx\File2.au3" Func _Report($sFilePath, $sWorkingDir = "", $iShowFlag = @SW_SHOW, $iOptFlag = 0) Return Run('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & $sFilePath & '"', $sWorkingDir, $iShowFlag, $iOptFlag) EndFunc If <Condition1> Then <Action1....> _Report("C:\xxx\xxx\File2.au3") EndIf $cell +=1 If <Condition2> Then <Action2....> _Report("C:\xxx\xxx\File2.au3") EndIf ;~ .......... <Main.au3> .......... Link to comment Share on other sites More sharing options...
BrewManNH Posted August 9, 2019 Share Posted August 9, 2019 Is the variable $cell declared local inside a function in script File2? Also, when posting code, use the code block in the editor "<>" button, it makes things much easier to read. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
Danp2 Posted August 9, 2019 Share Posted August 9, 2019 8 minutes ago, MK_2019 said: Am calling File2.au3 from Main.au3. Technically, you are including the one file into the other. Therefore, I believe the two attempts to declare the variable will occur at the same level and only one instance actually exists. Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
MK_2019 Posted August 9, 2019 Author Share Posted August 9, 2019 Yes.. $cell is declared as Local in Script File2.au3 Link to comment Share on other sites More sharing options...
Bowmore Posted August 9, 2019 Share Posted August 9, 2019 ;~.......... <File2.au3> .......... Local $cell = 1 Local $Description ="TTT3" Local $var = "C:\AutoIT_Excel_Report\Book3.xlsx" Local $oExcel = _Excel_Open (True) Local $oWorkbook = _Excel_BookOpen ($oExcel, $var) These variables you have declared in File2.au3 as Local are in fact Global. AutoIt treats all variable declared outside a function as Global. There is no file scoping in AutoIt like some other languages. "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook Link to comment Share on other sites More sharing options...
MK_2019 Posted August 11, 2019 Author Share Posted August 11, 2019 Thank You for Reply... Have Moved the scripts into a Function ( _File() in File2.au3 ).. And have declared variables as Local inside Function ( _File() )... But still issue Remain.... $cell value is still considered from local... Not getting incremented.... Is below Script correct?? Or am i missing something?? ;~ <File2.au3> ;~.......... <File2.au3> .......... Func _File() Local $cell = 1 Local $Description ="TTT3" Local $var = "C:\AutoIT_Excel_Report\Book3.xlsx" Local $oExcel = _Excel_Open (True) Local $oWorkbook = _Excel_BookOpen ($oExcel, $var) $range = _Excel_RangeWrite ($oWorkbook, Default, $Description, "A" &$cell) _Excel_BookSave ($oWorkbook) Sleep (2000) _Excel_BookClose ($oWorkbook) Sleep(2000) _Excel_Close ($oExcel) EndFunc _File () ;~.......... <File2.au3> .......... Link to comment Share on other sites More sharing options...
Network_Guy Posted August 11, 2019 Share Posted August 11, 2019 On 8/9/2019 at 12:38 PM, MK_2019 said: Func _Report($sFilePath, $sWorkingDir = "", $iShowFlag = @SW_SHOW, $iOptFlag = 0) Return Run('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & $sFilePath & '"', $sWorkingDir, $iShowFlag, $iOptFlag) EndFunc you run file2.au3 in a new process which have a totally separated memory . Link to comment Share on other sites More sharing options...
MK_2019 Posted August 11, 2019 Author Share Posted August 11, 2019 Hello Network_Guy...., It would be appreciable, if you can share a script (for Reference) from which i could Run new process in totally seperate memory.... Link to comment Share on other sites More sharing options...
Network_Guy Posted August 11, 2019 Share Posted August 11, 2019 (edited) using Run() will run new process in totally separated memory and thats the reason for your problem with $cell . i cant rly get what are you trying to do with this script , but here is mini example for calling function from included au3 with shared global variable. Main #include<test.au3> Global $var=0 for $i =0 to 2 _Function() $var+=1 next Include(test.au3) #include-once func _function() MsgBox(0,"",$var) EndFunc Edited August 11, 2019 by Network_Guy MK_2019 1 Link to comment Share on other sites More sharing options...
MK_2019 Posted August 11, 2019 Author Share Posted August 11, 2019 (edited) Hello Network_Guy.... You were Correct.., The Problem was "Return Run()"... it was creating a Separate memory... Also, I changed Variable (in Function) to "Dim" from "Local"... Below are the Modified Script i was able to Run the Script and get desired Expected Result... Thank You and All in the Thread for your suggestions... ******.......... <Main.au3> ..........****** #RequireAdmin Global $cell = 6 Global $Description ="Global" $sFilePath = "C:\xxx\xxx\file2.au3" #include <file2.au3> ;Func _Report($sFilePath, $sWorkingDir = "", $iShowFlag = @SW_SHOW, $iOptFlag = 0) ; Return Run('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & $sFilePath & '"', $sWorkingDir, $iShowFlag, $iOptFlag) ;EndFunc $cell +=1 For $i =0 to 2 $Description = "i"&$cell _File() $i +=1 $cell +=1 Next For $r =0 to 2 $Description = "r"&$cell _File() $r +=1 $cell +=1 Next For $k =0 To 2 $Description = "k"&$cell _File() $k +=1 $cell +=1 Next ******.......... <Main.au3> ..........****** ******......... <file2.au3> ..........****** #RequireAdmin ;#include-once #include <excel.au3> _File() Func _File() Sleep (2000) Dim $cell Dim $Description Local $var = "C:\xxx\Book3.xlsx" Local $oExcel = _Excel_Open (True) Local $oWorkbook = _Excel_BookOpen ($oExcel, $var) $range = _Excel_RangeWrite ($oWorkbook, Default, $Description, "A" &$cell) _Excel_BookSave ($oWorkbook) Sleep (2000) _Excel_BookClose ($oWorkbook) Sleep(2000) _Excel_Close ($oExcel) Sleep (2000) EndFunc ******.......... <file2.au3> ..........****** Edited August 11, 2019 by MK_2019 Link to comment Share on other sites More sharing options...
BrewManNH Posted August 12, 2019 Share Posted August 12, 2019 On 8/10/2019 at 11:34 PM, MK_2019 said: And have declared variables as Local inside Function ( _File() )... But still issue Remain.... $cell value is still considered from local... Not getting incremented.... A local variable with the same name as the Global variable will cause the local version to be used inside the function. Why are you redeclaring the variable $cell inside the function if you want to use the global version of it? Xandy 1 If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator 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