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

    • VeryGut
      By VeryGut
      I'm trying to insert the following formula in cell A2 using my script:
      =if(A1=""; "YES"; "NO")
      To my understanding, the line of code should be similar to this:
      _Excel_RangeWrite($MasterFile, Default, "=if(A1=""; "YES"; "NO")", "A2")
      However, it does not work, probably due to the multiple quotation marks that confuse the script :C
      How do I avoid this problem?
    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have a question about Excel, i have to create several charts one below the other dynamically.
      I have thought to use:
       
      $oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell) $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column)).Rows.Count  
      And then to use it in this way:
      $Graph_position = "=Test1!A"&$iRowCount+2&":K"&$iRowCount+24 But it doesn't work with charts.
      Does anyone have a suggestion?
       
    • LoneWolf_2106
      By LoneWolf_2106
      Hi all,
      i have an empty csv file, i have a non formatted text file.
      What do i want to do?
      I want to automate the process "get external data" in Excel, i want to import the data from the text file and basically create a csv file with a specific character encoding.
      Is it possible with AutoIT?
       
    • breakbadsp
      By breakbadsp
      I  want to create a excel file from my script if it does not exist.
      _ExcelBookOpen throws error=2 if file does not exist, after this error i want to create new file at this point.
      can i use _FileCreate()?
      _Logger($sLogPath, "{INFO}------: Opening Excel File: " & $sExcelPath& "") While 1 Local $oExcelTestResult = _ExcelBookOpen($sExcelPath) If @error = 2 Then If not _FileCreate($sResExcelPath) Then MsgBox(0, "Error", "Error In Opening REsult Excel File: Error: " & String(@error)) _Logger($sLogPath, "{ERROR}------: Result Excel File does not exist.. tried to create new but :ERROR : " & String(@error) & "") ExitLoop Else _Logger($sLogPath, "{INFO}------: Result Excel File does not exist.. **Created New**: ") EndIf Else ExitLoop EndIf WEnd  
    • Jibberish
      By Jibberish
      Hello,
      I have used this forum to get help many times. I thought it was time to (hopefully) help others.
      I created a script that reads a text file with a list of videos, displays the videos in a GUI with checkboxes next to the names, and displays the selected videos. This will become a part of a larger script I am creating to test a video player. The tough part for me was creating the GUI and Dynamic list of videos. I had a lot of trouble finding samples to help me, but finally found one  written by Melba23. The link is in the code, so he gets credit for helping! I also have not used arrays much and they are very picky about looping through the arrays without getting the dreaded error " Array variable has incorrect number of subscripts or subscript dimension range exceeded."  However diligence paid off!
      To run this code, take the video names commented below and create a videos.txt file in your script execution directory. You can put however many video names in this list. Thus the dynamic features of the code.
      Cheers!
      Jibberish
      #include <MsgBoxConstants.au3> #include <StringConstants.au3> #include <array.au3> #include <File.au3> #include <GUIConstantsEx.au3> Local $sMediaFile = @ScriptDir & "\videos.txt" ;~ Videos in videos.txt are: ;~ bbb_1080_60s.mp4 ;~ bbb_1080_60s_1.mp4 ;~ bbb_1080_60s_2.mp4 ;~ tos_4K_60s_HEVC.mp4 ;~ tos_4K_60s_HEVC_1.mp4 ;~ tos_4K_60s_HEVC_2.mp4 ;~ ;~ Additional videos can be added to this list. The functions are Dynamic. Dim $aMediaManifest Local $aArrayFile Local $aVideos Local $sVideoName Local $i ; MAIN ; Put the Video File Names into an Array _FileReadToArray($sMediaFile, $aArrayFile) Local $iVideoCount = UBound($aArrayFile) -1 ; Get the number of videos - 1 to prevent errors _ArrayDelete($aArrayFile, 0) ;Counter just gets in the way ; Move backwards through the array deleting the blank lines For $i = $iVideoCount - 1 To 0 Step -1 If $aArrayFile[$i] = "" Then _ArrayDelete($aArrayFile, $i) EndIf Next $aVideos = DisplayVideos($aArrayFile) $iVideoCount = UBound($aArrayFile) -1 _ArrayDisplay($aVideos) ; Display the checked videos ;~ End of MAIN ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ; ; GUI to display Videos in checkboxes ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Func DisplayVideos($aArrayFile) Local $iTop = -1 Local $iLeft = -1 Local $iWidth Local $iHeight = $iVideoCount * 30 Local $iL = 10 Local $iT = 10 Local $iRow = 0 Local $aVideo Local $iA = 0, $iB = 0 Local $sFill = "" $iMMCount = UBound($aArrayFile) $iMMNewCount = $iMMCount - 1 Local $aGUICheckbox[$iMMCount] Local $aCheckedVideos[$iMMCount] ; Put the Video File Names into an Array $hGUI = GUICreate("Video Checkbox", $iLeft, $iTop, $iWidth, $iHeight) GUICtrlCreateLabel("Videos", 180, $iT) $iT = $iT + 30 ; This is a great example of using arrays to create GUI check boxes or radio buttons For $i = 0 To $iMMNewCount Step 1 $sMP4Text = $aArrayFile[$i] $aGUICheckbox[$i] = GUICtrlCreateCheckbox($sMP4Text, 30, $iT) $iT += 30 Next $idClose1 = GUICtrlCreateButton("Start", $iL, $iT) GUISetState(@SW_SHOW) ; This section reads the checkboxes and puts the video names in an array in their original position ; in case this is important (as it is to me) ; This was the toughest part to code, and I found no samples online until I saw Melba23's sample here: ; https://www.autoitscript.com/forum/topic/119843-dynamic-gui-problem/#comment-832672 ; I got this working with only a little modification. THANK YOU MELBA23 While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $idClose1 For $i = 0 To $iMMNewCount Step 1 Switch GUICtrlRead($aGUICheckbox[$i]) Case $GUI_CHECKED $aCheckedVideos[$i] = $aArrayFile[$i] Case $GUI_UNCHECKED EndSwitch Next ExitLoop EndSwitch WEnd GUIDelete($hGUI) Return $aCheckedVideos EndFunc ;==>DisplayVideos