Jump to content

Variables question


Recommended Posts

I need to read a registry key during an AutoItStart function and possibly change that key before a GUI form opens. If the registry key was changed it will need to be changed back when the GUI form is closed so a variable will need to be created during the AutoItStart function and used during the shutdown function. I am guessing some variable creation will have to happen outside the AutoItStart function but I cant seem to get it right. Below is a synopsis of what I have going on. I am pretty sure this is a simple problem that is caused by my inexperience with the world of variables.

Any and all help is appreciated

CODE

Func OnAutoItStart()

; Reads registry to see if "SuggestAutoComplete" is on

$reg = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Outlook\Preferences", "ShowAutoSug")

; If "SuggestAutoComplete" is active the user is given the opportunity to

; turn it off and restart outlook or bypass the process

If $reg = 1 Then

$response = MsgBox(49, "EPOPS Data Gatherer", "The 'Suggest Auto Complete' function in Outlook is currently active." & @LF & "This needs to be deactivated during the email creation process." & @LF & "If Outlook is open it will be restarted. Please save any email that you may have open.")

$regvar1 = 1 ;Setting the variable so that the close function will know what the state of the reg key was

If $response = 1 Then ;closing outlook in preperation for the reg key change

If ProcessExists("outlook.exe") Then

Opt("WinTitleMatchMode", 2)

WinClose("Microsoft Outlook", "")

Sleep(500)

EndIf

RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Outlook\Preferences", "ShowAutoSug", "REG_DWORD", 0) ; changing the reg key

Sleep(500)

If FileExists(@ProgramFilesDir & "\Microsoft Office\OFFICE11\outlook.exe") Then ; Restarting outlook so the reg entry can take effect

Run(@ProgramFilesDir & "\Microsoft Office\OFFICE11\outlook.exe", "", @SW_MAXIMIZE)

Sleep(1000)

Opt("WinTitleMatchMode", 2)

WinActivate("Microsoft Outlook", "")

Else

$nomail = MsgBox(0, "Opening Outlook", "Outlook could not be found in the default location." & @LF & "This program will now terminate") ;just in case outlook has disapeared cause you can never assume anything

Exit

EndIf

Else

If $response = 2 Then ;This is in case they choose not to change the reg key

$regvar1 = 2 ; Setting the variable so that the close function will know what the state of the reg key was

$response2 = MsgBox(52, "EPOPS Data Gatherer", "You have chosen not to deactivate the 'Suggest Auto Complete' function. " & @LF & "Would you still like to run the EPOPS Data Gatherer application?" & @LF & "The email creation portion of the process amy not work properly")

If $response2 = 7 Then

Exit

EndIf

EndIf

EndIf

EndIf

EndFunc ;==>OnAutoItStart

;Form opens, emails are written and sent

;some other stuff goes on

; This function checks to see if the ShowAutoSuggest was active when the script started

; If so it resets it and restarts outlook.

Func _Close()

If $regvar1 = 1 Then

RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Outlook\Preferences", "ShowAutoSug", "REG_DWORD", 1)

If ProcessExists("outlook.exe") Then

Opt("WinTitleMatchMode", 2)

WinClose("Microsoft Outlook", "")

Sleep(1000)

If FileExists(@ProgramFilesDir & "\Microsoft Office\OFFICE11\outlook.exe") Then

Run(@ProgramFilesDir & "\Microsoft Office\OFFICE11\outlook.exe", "", @SW_MAXIMIZE)

EndIf

EndIf

EndIf

_ExcelBookClose ($open)

Exit

EndFunc ;==>_Close

Link to comment
Share on other sites

I need to read a registry key during an AutoItStart function and possibly change that key before a GUI form opens. If the registry key was changed it will need to be changed back when the GUI form is closed so a variable will need to be created during the AutoItStart function and used during the shutdown function. I am guessing some variable creation will have to happen outside the AutoItStart function but I cant seem to get it right. Below is a synopsis of what I have going on. I am pretty sure this is a simple problem that is caused by my inexperience with the world of variables.

Any and all help is appreciated

CODE

Func OnAutoItStart()

; Reads registry to see if "SuggestAutoComplete" is on

$reg = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Outlook\Preferences", "ShowAutoSug")

; If "SuggestAutoComplete" is active the user is given the opportunity to

; turn it off and restart outlook or bypass the process

If $reg = 1 Then

$response = MsgBox(49, "EPOPS Data Gatherer", "The 'Suggest Auto Complete' function in Outlook is currently active." & @LF & "This needs to be deactivated during the email creation process." & @LF & "If Outlook is open it will be restarted. Please save any email that you may have open.")

$regvar1 = 1 ;Setting the variable so that the close function will know what the state of the reg key was

If $response = 1 Then ;closing outlook in preperation for the reg key change

If ProcessExists("outlook.exe") Then

Opt("WinTitleMatchMode", 2)

WinClose("Microsoft Outlook", "")

Sleep(500)

EndIf

RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Outlook\Preferences", "ShowAutoSug", "REG_DWORD", 0) ; changing the reg key

Sleep(500)

If FileExists(@ProgramFilesDir & "\Microsoft Office\OFFICE11\outlook.exe") Then ; Restarting outlook so the reg entry can take effect

Run(@ProgramFilesDir & "\Microsoft Office\OFFICE11\outlook.exe", "", @SW_MAXIMIZE)

Sleep(1000)

Opt("WinTitleMatchMode", 2)

WinActivate("Microsoft Outlook", "")

Else

$nomail = MsgBox(0, "Opening Outlook", "Outlook could not be found in the default location." & @LF & "This program will now terminate") ;just in case outlook has disapeared cause you can never assume anything

Exit

EndIf

Else

If $response = 2 Then ;This is in case they choose not to change the reg key

$regvar1 = 2 ; Setting the variable so that the close function will know what the state of the reg key was

$response2 = MsgBox(52, "EPOPS Data Gatherer", "You have chosen not to deactivate the 'Suggest Auto Complete' function. " & @LF & "Would you still like to run the EPOPS Data Gatherer application?" & @LF & "The email creation portion of the process amy not work properly")

If $response2 = 7 Then

Exit

EndIf

EndIf

EndIf

EndIf

EndFunc ;==>OnAutoItStart

;Form opens, emails are written and sent

;some other stuff goes on

; This function checks to see if the ShowAutoSuggest was active when the script started

; If so it resets it and restarts outlook.

Func _Close()

If $regvar1 = 1 Then

RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Outlook\Preferences", "ShowAutoSug", "REG_DWORD", 1)

If ProcessExists("outlook.exe") Then

Opt("WinTitleMatchMode", 2)

WinClose("Microsoft Outlook", "")

Sleep(1000)

If FileExists(@ProgramFilesDir & "\Microsoft Office\OFFICE11\outlook.exe") Then

Run(@ProgramFilesDir & "\Microsoft Office\OFFICE11\outlook.exe", "", @SW_MAXIMIZE)

EndIf

EndIf

EndIf

_ExcelBookClose ($open)

Exit

EndFunc ;==>_Close

I think what you're asking for is this, but I'm sure you'll correct me if I misunderstood.

Near the start of your script have something like

$RegChanged = false;the registry hasn't been changed.

In your OnAutoItStart function set $RegChanged to true if you changed the reg.

Then add a function OnAutoItExit which has something like

if $RegChanged then

correct the registry

endif

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I think what you're asking for is this, but I'm sure you'll correct me if I misunderstood.

Near the start of your script have something like

$RegChanged = false;the registry hasn't been changed.

In your OnAutoItStart function set $RegChanged to true if you changed the reg.

Then add a function OnAutoItExit which has something like

if $RegChanged then

correct the registry

endif

That's pretty close.

Start of script

$regentry = current state (1 or 0)

In AutoItStart function

The decision to accept the registry change has been made

AutoItExit function

The registry setting must be returned to its original state

Link to comment
Share on other sites

In my opinion you don't really need a function OnAutoItStart.

Get rid of that and start building your script having that part on top. Declare a globar variable which will act like a flag (0 if reg not changed by your script and 1 if it was changed) make your exit function read that var and to do whatever needed according to its value.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

In my opinion you don't really need a function OnAutoItStart.

Get rid of that and start building your script having that part on top. Declare a globar variable which will act like a flag (0 if reg not changed by your script and 1 if it was changed) make your exit function read that var and to do whatever needed according to its value.

enaiman, thank you very much. Like in so many cases the simplest answer is ithe best one. I was thinking that I needed to use the OnAutoItStart function to check the registry before the form started but that does not seem to be the case. Problem solved. thanks again
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...