Jump to content
Nighterfighter

Get value of "fake" checkbox inside Excel 2010

Recommended Posts

Nighterfighter

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
spudw2k

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
Nighterfighter

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

    • nooneclose
      By nooneclose
      Hey. I'm working on a new project and was wondering if there is a better way to "update" my Column E array. 
      Here is my code: 
      Local $nI  = 0                                                            ;Creates a name index of 0: nI = Name index Local $nII = 1                                                            ;Creates a name index of 1 for second loop: nII = Name Index 2 For    $iN = 0 To $IndexRows Step 1                                       ;Checks the roster for any names that appear twice      For $iN2 = 0 To $IndexRows Step 1          if $d_Names[$nI] == $d_Names[$nII] And $d_Names[$nII] <> "" Then              Local $timeSheetName = _ArraySearch($e_Names, $d_Names[$nI], 0, 0, 0, 0, 1)              ;MsgBox($MB_SYSTEMMODAL, "Found it", $d_Names[$nI] & " In column E on Row " & $timeSheetName)              Local $eI  = $timeSheetName + 1              ;ConsoleWrite($timeSheetName & @CRLF)              ;ConsoleWrite($eI & @CRLF)              ;ConsoleWrite(@CRLF)              _Excel_RangeInsert($OpenWorkbook.ActiveSheet, "E" & $eI & ":F" & $eI, $xlShiftDown)                                                                          ;Inserts a empty cell in columns E and F.              _Excel_RangeWrite($OpenWorkbook, $OpenWorkbook.ActiveSheet, $d_Names[$nII], "E" & $eI)                                                                         ;Fills the empty cell in columns E with the doubled name              $aArray_Index = 2                                           ;Array element counter              For $Index = 2 To $IndexRows Step 1                        ;Loops through every row in the Excel file unto no rows are found or a null row is found                  $Array_Value_E = _Excel_RangeRead($OpenWorkbook, Default, "E"&$Index)                  $e_names[$aArray_Index] = $Array_Value_E                ;While the code loops every value in column E is stored in the E array (updating the array)                  $aArray_Index += 1              Next              ExitLoop          EndIf      Next      $nI  += 1      $nII += 1 Next Basically, It checks a roster for people whose name appears twice then inserts a new "row" for that person because they work in two different departments.
      I have to find that name however in Column E if two appear in column D. My code works but I think it is not as efficient as it could be. 
      Any ideas on how to improve the "update" for my array?
      (once it finds the double names in Column D it then searches for that name by going name by name in the Column E array and once it finds it inserts a new row. However, the E array doesn't have that new row stored in it so I have to "update" the array to properly find the next name)
      Any and all tips would be greatly appreciated. 
       
      NOTE: Just assume I'm opening the excel file properly please do not add that code in, it only complicates your answer. 
    • smud
      By smud
      Currently, I'm working on a program that will display Dialog boxes with either Yes or No.
      For each dialog, I reward the user with X amount of Credits.
      I'm hoping to output the amount of credits to a cell in a column (there will be 20 different columns).
      It will only post to a row that is equal to today's date (first column). If no row exists yet with the current date, it will start a new row.
      Any suggestions?
      Thank you
    • nooneclose
      By nooneclose
      How do I properly convert this to Autoit? This is a VBA macro that I recorded in Excel.
       ActiveSheet.Outline.ShowLevels RowLevels:=2 I need this to close my subtotal once it is finished. 
      any help will be greatly appreciated. 
    • Skeletor
      By Skeletor
      Hi All,
      While creating a few excel spreadsheets using AutoIt, I came across something which to my limiting time to research the forums I don't anyone has mentioned. 
      The color pallettes are reversed. 
      Huge shock to me.
       
      I wanted to produce a red row but kept on getting blue. 
      Seems like 0xFF0000 was red on the charts but when running the script, I got blue. 
      I then played around with the colors, and after a few tries, I finally got Red. 
      Reversed the FF0000 and the result is 0000FF.
       
      So for Excel compared to Html
      0000FF (Red) - Excel
      0000FF (Blue) - Html

      FFFF00 (Cyan) - Excel
      FFFF00(Yellow) - Html
       
    • Jemboy
      By Jemboy
      Yesterday I had to make some little changes to an old Autoit program we use at my work.
      The program reads some data and convert it to Excel.
      Before writing the cell, it is changed to text and later on I slap the column with an autofitwidth.
      Furthermore weI execute a conditional format on the sheet, to make the data more readable.
      I quickly found out that because of the breaking changes Excel.udf had starting from AutoIt 3.3.12.0,
      a lot of things had to been changed.
      The changes I had to do, only took 10 minutes.
      After trying to adjust the script for over 5 hours, to get it working with the new Excel.udf, I gave up.
      I stopped changing the script, uninstalled the my Autoit and went looking for an older version.
      Luckilly I was able to find Autoit v3.3.8.1 (with corresponding Scite) in my software repository.
      Installing Autoit V3.3.8.1 and compiling the file, now took me  10 minutes .
       
      So why did I not get the old script working with the new Excel.udf?
      There are several reasons I failed getting the old script working with the new Excel UDF.
       I had some pressure from management to fix it ASAP (and got a little anxious)  Most all resources on the internet point to the working of the old EXcel.UDF And offcourse there were thosing "breaking changes",
      with new functions using diffrent parameters or using parameters in different order. One of the column's on the sheet is used to store EAN13 (barcode) and was formatted like 1,23E12.
      I couldn't change the cell to text, also autofitwidt was not working and using conditional formatting was also a no-no.
      So in the end I could use the new Excel UDF, but not desapointed management.
       
      What would I like to ask?
      I understand that sometimes you want to rewrite a program to make it better. I even understand that one has to make breaking changes sometimes.
      But in this case because of lacking examples/resources my day went completely down the drain.
      I would like to ask the Excel.udf developpers to:
      Make more functions available to do things like changing cell properties easily, changing cell color, do an autofit columnwidth, format data conditionally. 
        Or write an Example using the (new) Excel UDF, making examples how to format a cell, do conditional format, changing cell colors etc.
        I probably am more of an example guy.
      Having a good Excel.UDF Example showing a lot of common things normally makes, programming things easy for me.
      Because I can keep tweaking snippets until I get it working the way I want it.
       
      So dear developpers, could you help me and other future user out?
       
       
       
       
       
       
       
       
       
       
       
       
       
       
×