Jump to content

Get value of "fake" checkbox inside Excel 2010


Recommended Posts

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.

Link to comment
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?

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

×
×
  • Create New...