Jump to content
Sign in to follow this  
MK_2019

Access Global Variable inside a Function

Recommended Posts

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> ..........

Share this post


Link to post
Share on other sites

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 Gude
How 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

Share this post


Link to post
Share on other sites
;~.......... <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

Share this post


Link to post
Share on other sites

image.thumb.png.515edd03a5d8c585dfd52858b887857a.png

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> ..........

 

 

Share this post


Link to post
Share on other sites
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 . 

Share this post


Link to post
Share on other sites

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....

Share this post


Link to post
Share on other sites

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 by Network_Guy

Share this post


Link to post
Share on other sites

image.png.73df18ba555096942a879e9a105b9564.png

 

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 by MK_2019

Share this post


Link to post
Share on other sites
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?


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 Gude
How 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

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...