Nighterfighter

Get value of "fake" checkbox inside Excel 2010

3 posts in this topic

Hey guys, I am finishing up a script that will run a import a lof of VBA code, the .bas files, (more VBA code than can easily be ported into AutoIt. Thousands of lines...) to do some work on Excel spreadsheets.

 

However, Excel has a security setting where it blocks macros from being run/loaded, unless both the "Enable All Macros" setting is enabled, and the "Trust access to the VBA project object model" checkbox is enabled. However, that checkbox appears to be fake. Using the AutoIt WindowInfo tool, there is no control ID for that checkbox.

 

I have spent the last several hours trying to get this to work. Here is what I have so far, all of the control-send down/up etc are to get to the right menu options inside Excel.

 

#include <Excel.au3>
#include <FileConstants.au3>



Global $oExcel, $oWorkbook, $oCurrSheet, $sMsg
Global $sXLS = "C:\Users\SNIP\Downloads\test.xlsm"
Global $sWorkbook = @ScriptDir & "\test.xlsm"
Global $sBAS = @ScriptDir & "\Module1.bas", $sMacroName = "Main"

SelectExcelFile()
SelectBASFile()
$oExcel = _Excel_Open()
$sWorkbook = _Excel_BookOpen($oExcel, $sWorkbook);
WinWaitActive("Microsoft Excel - " & $sWorkbook)
;MsgBox($MB_OK,"Open","Workbook is open")

ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "!f");
Sleep(50)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "!t");
Sleep(50)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "!f");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{DOWN}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{DOWN}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{DOWN}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{DOWN}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{DOWN}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{DOWN}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{DOWN}");
Sleep(105)
ControlSend("Microsoft Excel - " , "", "", "{DOWN}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{DOWN}");
Sleep(105)
ControlSend("Microsoft Excel - " , "", "", "{TAB}");
Sleep(105)
ControlSend("Microsoft Excel - " , "", "", "{TAB}");
Sleep(105)
ControlSend("Microsoft Excel - " , "", "", "{TAB}");
Sleep(105)
ControlSend("Microsoft Excel - " , "", "", "{TAB}");
Sleep(105)
ControlSend("Microsoft Excel - " , "", "", "{TAB}");
Sleep(105)
ControlSend("Microsoft Excel - " , "", "", "{ENTER}");
Sleep(105)
ControlSend("Excel Options", "", "", "!t");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{UP}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{UP}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{UP}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{UP}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{UP}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{UP}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{UP}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{UP}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{UP}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{UP}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{UP}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{DOWN}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{DOWN}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{DOWN}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{DOWN}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{DOWN}");
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "!e")
Sleep(105)
ControlSend("Trust Center", "", "", "!v") ;This part is not working. It doesn't select the checkbox, even though alt+v does manually.
Sleep(100)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "{ENTER}")
Sleep(105)
ControlSend("Microsoft Excel - " & $sWorkbook, "", "", "!{F4}")
$oExcel.VBE.ActiveVBProject.VBComponents.Import($sBAS)
$oExcel.Application.Run("Main")

;These two functions are just to select the files.

Func SelectExcelFile()
    ; Create a constant variable in Local scope of the message to display in FileOpenDialog.
    Local Const $sMessage = "Select Excel File To Sort."

    ; Display an open dialog to select a list of file(s).
    Local $sFileOpenDialog = FileOpenDialog($sMessage, @WindowsDir & "\", "Excel Files (*.xlsx;*xlsm;*.xls;)|", $FD_FILEMUSTEXIST + $FD_MULTISELECT)
    If @error Then
        ; Display the error message.
        MsgBox($MB_SYSTEMMODAL, "", "No file(s) were selected.")

        ; Change the working directory (@WorkingDir) back to the location of the script directory as FileOpenDialog sets it to the last accessed folder.
        FileChangeDir(@ScriptDir)
    Else
        ; Change the working directory (@WorkingDir) back to the location of the script directory as FileOpenDialog sets it to the last accessed folder.
        FileChangeDir(@ScriptDir)

        ; Replace instances of "|" with @CRLF in the string returned by FileOpenDialog.
        $sFileOpenDialog = StringReplace($sFileOpenDialog, "|", @CRLF)

        ; Display the list of selected files.
        $sWorkbook = $sFileOpenDialog ;Assign the file selected to the workbook file
       ; MsgBox($MB_SYSTEMMODAL, "", "You chose the following files:" & @CRLF & $sFileOpenDialog)
    EndIf
 EndFunc   ;==>Example

 Func SelectBASFile()
    ; Create a constant variable in Local scope of the message to display in FileOpenDialog.
    Local Const $sMessage = "Select BAS Macro."

    ; Display an open dialog to select a list of file(s).
    Local $sFileOpenDialog = FileOpenDialog($sMessage, @WindowsDir & "\", "BAS Files (*.bas;)|", $FD_FILEMUSTEXIST + $FD_MULTISELECT)
    If @error Then
        ; Display the error message.
        MsgBox($MB_SYSTEMMODAL, "", "No file(s) were selected.")

        ; Change the working directory (@WorkingDir) back to the location of the script directory as FileOpenDialog sets it to the last accessed folder.
        FileChangeDir(@ScriptDir)
    Else
        ; Change the working directory (@WorkingDir) back to the location of the script directory as FileOpenDialog sets it to the last accessed folder.
        FileChangeDir(@ScriptDir)

        ; Replace instances of "|" with @CRLF in the string returned by FileOpenDialog.
        $sFileOpenDialog = StringReplace($sFileOpenDialog, "|", @CRLF)

        ; Display the list of selected files.
        $sBAS = $sFileOpenDialog ;Assign the file selected to the workbook file
        ;MsgBox($MB_SYSTEMMODAL, "", "You chose the following files:" & @CRLF & $sFileOpenDialog)
    EndIf
EndFunc   ;==>Example

 

What's the best way to get the value of that imaginary checkbox, and if it isn't checked, check it? Or if it is checked, leave it alone? I tried doing a PixelSearch but that didn't work.

 

 

And I can't manually enable the developer settings. This program will go to a few other users who won't know how to do that, and I can't remote in.

Share this post


Link to post
Share on other sites



Those values should be set in the registry.  You could just apply the settings there...but it's not recommended (security risk).  Have you explored a solution that uses Trusted Locations?

Share this post


Link to post
Share on other sites

Thanks for that link, but that won't work for what I am trying to do. 

 

I was able to get it working after a few more hours. I ended up checking the pixel value of the checkbox, and if it was already checked, skip over it. Else, click it.

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

  • Similar Content

    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have to store an entire row of a Excel workbook into an array.  The row index is stored in a variable.
      How can i do it?
      Thanks in advance for your support.
    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have to write a value into an excel column.
      I know where it starts from, but i don't know what the end is, last non-empty cell.
      How can i get the number of last non-empty cell?
      Thanks in advance.
      Regards 
    • Nareshm
      By Nareshm
      Hi All,
      I have excel file like this
      and i want to cut cell/text from excel to other software.

       
      I have to cut the cell of B column one by one and past into other software
      If Winexists("No Data Found")
      then restore cuted cell and goto next/down side cell
      How to do it ?
    • Jibberish
      By Jibberish
      Hi,
      I am maybe an intermediate AutoIt script writer, but have no experience creating GUIs.
      I have a script with two functions. One for Checkboxes and another with radio buttons. Each function creates it's own window.
      I'd like to use one window with both checkboxes and radio buttons.
      I pulled samples from AutoIt Help and other places and worked it into this: (RadioCheck still uses the example Case and MsgBoxes. I will clean this up soon)
      Func CheckOptions() ; Create a GUI with various controls. Local $hGUI = GUICreate("SGX4CP Options", 275, 250) ; Create a checkbox control. Local $iLoopCheckbox = GUICtrlCreateCheckbox("Loop", 10, 10, 185, 25) Local $iFullScreenCheckbox = GUICtrlCreateCheckbox("Fullscreen", 10, 40, 185, 25) Local $iRestartPlaybackCheckbox = GUICtrlCreateCheckbox("Restart Playback from Sleep", 10, 70, 185, 25) GUICtrlSetState($iRestartPlaybackCheckbox, $GUI_CHECKED) Local $iDisableSleepCheckbox = GUICtrlCreateCheckbox("Disable Sleep", 10, 100, 185, 25) Local $iLogCheckbox = GUICtrlCreateCheckbox("Show Log", 10, 130, 185, 25) GUICtrlSetState($iLogCheckbox, $GUI_CHECKED) Local $idClose = GUICtrlCreateButton("Next", 110, 220, 85, 25) ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) ; Loop until the user exits. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $idClose ExitLoop Case $iLoopCheckbox If _IsChecked($iLoopCheckbox) Then $bLoopChecked = True Else $bLoopChecked = False EndIf Case $iFullScreenCheckbox if _IsChecked($iFullScreenCheckbox) Then $bFullScreenChecked = True Else $bFullScreenChecked = False EndIf Case $iRestartPlaybackCheckbox if _IsChecked($iRestartPlaybackCheckbox) Then $bRestartPlaybackChecked = True Else $bRestartPlaybackChecked = False EndIf Case $iDisableSleepCheckbox if _IsChecked($iDisableSleepCheckbox) Then $bDisableSleepChecked = True Else $bDisableSleepChecked = False EndIf Case $iLogCheckbox if _IsChecked($iLogCheckbox) Then $bLogChecked = True Else $bLogChecked = False EndIf EndSwitch WEnd ; Delete the previous GUI and all controls. GUIDelete($hGUI) EndFunc Func RadioCheck() GUICreate("Select Test",300,180) ; will create a dialog box that when displayed is centered Local $idRadio1 = GUICtrlCreateRadio("Loop Forever", 10, 10) Local $idRadio2 = GUICtrlCreateRadio("Play each video 3 times", 10, 40) Local $idRadio3 = GUICtrlCreateRadio("Play each video separately", 10, 70) GUICtrlSetState($idRadio1, $GUI_CHECKED) Local $idClose = GUICtrlCreateButton("Start Test", 120,100) GUISetState(@SW_SHOW) Local $idMsg ; Loop until the user exits. While 1 $idMsg = GUIGetMsg() Select Case $idMsg = $GUI_EVENT_CLOSE ExitLoop Case $idMsg = $idRadio1 And BitAND(GUICtrlRead($idRadio1), $GUI_CHECKED) = $GUI_CHECKED MsgBox($MB_SYSTEMMODAL, 'Info:', 'The app will run forever, playing each video once, then looping back to the first video.') $bTestSelectForever = True Case $idMsg = $idRadio2 And BitAND(GUICtrlRead($idRadio2), $GUI_CHECKED) = $GUI_CHECKED MsgBox($MB_SYSTEMMODAL, 'Info:', 'Each video will loop 3 times then move to the next video.') $bTestSelect3Times = True Case $idMsg = $idRadio3 And BitAND(GUICtrlRead($idRadio2), $GUI_CHECKED) = $GUI_CHECKED MsgBox($MB_SYSTEMMODAL, 'Info:', 'Player opens, first video plays, player closes. Player opens, second video plays, player closes, etc.') $bTestSelectSingleVideo = True EndSelect WEnd EndFunc I would like to combine the checkbox "Loop" and the radio button $idRadio2. Radio2 requires Loop to be checked.
      I planned to remove the Loop checkbox and only enable it if Radio2 is selected.
      Can I combine these two functions into one with one window with both Checkboxes and Radio Buttons?
      Thanks
      Jibberish
    • water
      By water
      Extensive library to control and manipulate Microsoft Excel charts.
      Theads: General Help & Support - Example Scripts
      BTW: If you like this UDF please click the "I like this" button. This tells me where to next put my development effort

      KNOWN BUGS (last changed: 2017-07-21)
      None. The COM error handling related bugs have been fixed.