Jump to content
Smigacznr1

Check for macros in excel

Recommended Posts

Smigacznr1

Hi everyone,

I'm writing script for searching empty excel files in share folders.

I now how to check if is any text, value or formula in file but how to check for macros?

 

Regards

Smigacznr1

 

Share this post


Link to post
Share on other sites
javiwhite

Hi Smigacznr1,

You could always check the extension of the workbook?

I'm no excel wizard, but i believe macro's can only be stored in xlsm workbooks (macro enabled), with the exception of workbooks created on your local machine (as the macro's can be saved to your personal workbook (Which wouldn't be applicable in a shared folder anyway, as only the author of the macro, by default has access to his/her own personal workbook)

Hope this helps

Javi


give a man an application, and he'll be frustrated for the day, Teach him how to program applications and he'll be frustrated for a lifetime.

Share this post


Link to post
Share on other sites
JLogan3o13

Hi, Smigacznr1. There are a couple ways to do it. Remember that you will always have a default module with the "ThisWorkbook", "Sheet1", "Sheet2", and "Sheet3" in every workbook. I use this quick and dirty to check which modules or macros I have in a workbook, may give you some ideas.

#include <Excel.au3>

Local $aTemp[1] = [""]

Const $ProcedureName = "*"
Local $oExcel = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oExcel, @DesktopDir & "\Test.xls")
    $vbProj = $oWorkbook.VBProject

    For $vbComp In $vbProj.VBComponents
        _ArrayAdd($aTemp, $vbComp.Name)
    Next

    $aTemp[0] = UBound($aTemp) - 1

    For $i = 1 To $aTemp[0]
        Switch $aTemp[$i]
            Case "ThisWorkbook", "Sheet1", "Sheet2", "Sheet3"
                ;
            Case Else
                MsgBox(0, "", $aTemp[$i])
        EndSwitch
    Next

_Excel_BookClose($oWorkbook)
_Excel_Close($oExcel)

 

 

 

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
Smigacznr1

Hi JLogan3o13

I'm getting error:

"Variable must be of type "Object".:
For $vbComp In $vbProj.VBComponents
For $vbComp In $vbProj^ ERROR"

 

I rebuilded your saample to:

#include <Excel.au3>

Local $aTemp[1] = [""]

Const $ProcedureName = "*"

$oExcel = ObjCreate("Excel.Application")
$oExcel.Visible = 1
$oExcel.WorkBooks.Open (@DesktopDir & "\Huis-3D-Excel-Pendulum_97.xls")

    $vbProj = $oExcel.WorkBooks.VBProject

    For $vbComp In $vbProj.VBComponents
        _ArrayAdd($aTemp, $vbComp.Name)
    Next

    $aTemp[0] = UBound($aTemp) - 1

    For $i = 1 To $aTemp[0]
        Switch $aTemp[$i]
            Case "ThisWorkbook", "Sheet1", "Sheet2", "Sheet3"
                ;
            Case Else
                MsgBox(0, "", $aTemp[$i])
        EndSwitch
    Next


$oExcel.WorkBooks.Close(false)
$oExcel.Quit()

and I'm getting exactly this same error. I'm not good with object coding...

I'm using newest stable version of Autoit, fully patched Win 7 x64 and Office 2007.

Share this post


Link to post
Share on other sites
water

Add some error checking:

#include <Excel.au3>

Local $aTemp[1] = [""]
Const $ProcedureName = "*"
$oExcel = ObjCreate("Excel.Application")
$oExcel.Visible = 1
$oExcel.WorkBooks.Open (@DesktopDir & "\Huis-3D-Excel-Pendulum_97.xls")
    $vbProj = $oExcel.WorkBooks.VBProject
    If @error Or Not IsObj($vbProj) Then Exit ; Exit if no projects exist
    For $vbComp In $vbProj.VBComponents
        _ArrayAdd($aTemp, $vbComp.Name)
    Next
    $aTemp[0] = UBound($aTemp) - 1
    For $i = 1 To $aTemp[0]
        Switch $aTemp[$i]
            Case "ThisWorkbook", "Sheet1", "Sheet2", "Sheet3"
                ;
            Case Else
                MsgBox(0, "", $aTemp[$i])
        EndSwitch
    Next
$oExcel.WorkBooks.Close(false)
$oExcel.Quit()

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

BTW:

"ThisWorkbook", "Sheet1", "Sheet2", "Sheet3"

is only true on English systems.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

Check (by adding _ArrayDisplay) if there are any components at all.

#include <Excel.au3>

Local $aTemp[1] = [""]
Const $ProcedureName = "*"
$oExcel = ObjCreate("Excel.Application")
$oExcel.Visible = 1
$oExcel.WorkBooks.Open (@DesktopDir & "\Huis-3D-Excel-Pendulum_97.xls")
    $vbProj = $oExcel.WorkBooks.VBProject
    If @error Or Not IsObj($vbProj) Then Exit ; Exit if no projects exist
    For $vbComp In $vbProj.VBComponents
        _ArrayAdd($aTemp, $vbComp.Name)
    Next
    _ArrayDisplay($aTemp) ; Display components
    $aTemp[0] = UBound($aTemp) - 1
    For $i = 1 To $aTemp[0]
        Switch $aTemp[$i]
            Case "ThisWorkbook", "Sheet1", "Sheet2", "Sheet3"
                ;
            Case Else
                MsgBox(0, "", $aTemp[$i])
        EndSwitch
    Next
$oExcel.WorkBooks.Close(false)
$oExcel.Quit()

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Smigacznr1

Nope. It's not working at all.

Exiting on line 9

If @error Or Not IsObj($vbProj) Then Exit ; Exit if no projects exist

Share this post


Link to post
Share on other sites
water

Ok, then let's check this line:

#include <Excel.au3>

Local $aTemp[1] = [""]
Const $ProcedureName = "*"
$oExcel = ObjCreate("Excel.Application")
$oExcel.Visible = 1
$oExcel.WorkBooks.Open (@DesktopDir & "\Huis-3D-Excel-Pendulum_97.xls")
    $vbProj = $oExcel.WorkBooks.VBProject
    If @error Or Not IsObj($vbProj) Then
        MsgBox(0, "", "@error: " & @error & @CRLF & "IsObj: " & IsObj($vbProj))
        Exit
    EndIf
    For $vbComp In $vbProj.VBComponents
        _ArrayAdd($aTemp, $vbComp.Name)
    Next
    _ArrayDisplay($aTemp) ; Display components
    $aTemp[0] = UBound($aTemp) - 1
    For $i = 1 To $aTemp[0]
        Switch $aTemp[$i]
            Case "ThisWorkbook", "Sheet1", "Sheet2", "Sheet3"
                ;
            Case Else
                MsgBox(0, "", $aTemp[$i])
        EndSwitch
    Next
$oExcel.WorkBooks.Close(false)
$oExcel.Quit()

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Smigacznr1

Error:

@error: -2147352570
IsObj: 0

Share this post


Link to post
Share on other sites
water

#include <Excel.au3>

Local $aTemp[1] = [""]
Const $ProcedureName = "*"
$oExcel = ObjCreate("Excel.Application")
$oExcel.Visible = 1
$oWorkbook = $oExcel.WorkBooks.Open(@DesktopDir & "\Huis-3D-Excel-Pendulum_97.xls")
$vbProj = $oWorkbook.VBProject
If @error Or Not IsObj($vbProj) Then
    MsgBox(0, "", "@error: " & @error & @CRLF & "IsObj: " & IsObj($vbProj))
    Exit
EndIf
For $vbComp In $vbProj.VBComponents
    _ArrayAdd($aTemp, $vbComp.Name)
Next
_ArrayDisplay($aTemp) ; Display components
$aTemp[0] = UBound($aTemp) - 1
For $i = 1 To $aTemp[0]
    Switch $aTemp[$i]
        Case "ThisWorkbook", "Sheet1", "Sheet2", "Sheet3"
            ;
        Case Else
            MsgBox(0, "", $aTemp[$i])
    EndSwitch
Next
$oExcel.WorkBooks.Close(false)
$oExcel.Quit()

​The VBProject collection is a member of the Workbook object, not the Workbooks collection.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Smigacznr1

I have not notice but after correction, I have still error:

@error: 1
IsObj: 0

Share this post


Link to post
Share on other sites
water

Looks much better. Could you please add

#include <Debug.au3>
_DebugSetup()
_DebugCOMError()

at the top of your script so we get more detailed error information?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Smigacznr1

Sure. I didn't know that in Autoit is any debugging include :)

Result:

@@ DEBUG COM Error encountered in excel_macro_search.au3 (11) :
    Number            = 0x80020006 (-2147352570)
    WinDescription    = Unknown name
    Description       =
    Source            =
    HelpFile          =
    HelpContext       =
    LastDllError      = 0
    Retcode           = 0x00000000

Share this post


Link to post
Share on other sites
water

Is this a Workbook with a macro?
If no, could you please test with a Workbook with a macro?
So we know: @error = 1 means: no macro, @error = 0 means: go on checking for macros.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

Will check as soon as I'm on a PC with Windows and Excel.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Smigacznr1

Bump!

Could someone else help or show right direction?

Share this post


Link to post
Share on other sites
water

Only found this on the web to check if an Excel File has macros.
Could you please run the VB code and if it works it should be easy to translate to AutoIt.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

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

    • ajblandford
      By ajblandford
      I have embeded an Excel file in my autoit Script and want to use it as part of my GUI. I want to be able to select a range of cells and add the cell data to an edit box. Then dump that data to an array. I am using GUICtrlCreateObj to embed the spreadsheet. I cannot find any way to let my program see what cells are active so I can add that data to the edit box. 
       
      #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #include <excel.au3> $FileName = 'C:\VLog\book1.xlsx' If Not FileExists($FileName) Then MsgBox(0, "ERROR", "File not found") Exit EndIf ;Basic GUI $oExcelDoc = ObjGet($FileName) ; Get an excel Object from an existing filename If IsObj($oExcelDoc) Then $mainGUI = GUICreate("Production Room 2", 1800, 1200, 10, 10, $WS_MINIMIZEBOX + $WS_SYSMENU + $WS_CLIPCHILDREN) $GUI_ActiveX = GUICtrlCreateObj($oExcelDoc, 10, 70, @DesktopWidth - 250, @DesktopHeight - 260) $data = GUICtrlCreateEdit("", @DesktopWidth - 225, 75, 100) $btn = GUICtrlCreateButton( "GO", @DesktopWidth - 100, 75, 75) Else MsgBox(0, "", "failed") EndIf ;------------------ ;Turns off all command bars in excel to prevent user from making changes For $Bar In $oExcelDoc.CommandBars If $Bar.Enabled = True Then $Bar.Enabled = False If $Bar.Visible = True Then $Bar.Visible = False Next $oExcelDoc.Application.DisplayFormulaBar = False $oExcelDoc.Application.CommandBars("Shadow Settings").Visible = False $oExcelDoc.Application.DisplayScrollBars = True $oExcelDoc.Application.DisplayStatusBar = False GUISetState()  
    • Skeletor
      By Skeletor
      Hi Guys,
      Can anyone point me in the right direction. I'm trying to accomplished conditional formatting with arrows, but the code I have is wrong... 
      .Range("=$A3:$A4000").FormatConditions.Add = (xlCellValue, xlGreater, "=$A:$A") .IconSet(xl3Arrows) ;ActiveWorkbook.iconsets I also tried it like this:
      .Range("=$A3:$A4000").FormatConditions _ .Add(xlCellValue, xlGreater, "=$A:$A") .IconSet(xl3Arrows) ;ActiveWorkbook.iconsets  
    • Skeletor
      By Skeletor
      Hi Guys,
      How would you use _Excel_FilterSet to filter the excel sheet from largest number to smallest number?
      _Excel_FilterSet($oWorkbook, "Sheet1", "A2:I2", 1, ">20", 1, "<40") ; OR _Excel_FilterSet($oWorkbook, "Sheet1", "A2:I2", 1, ">", 1, "<") Excel Sheet attached. Trying to filter on the last column "I2"
      Inventory.xlsx
    • Skeletor
      By Skeletor
      Hi All,
      Has anyone come across this before?
      Code below:
      $SheetList = _Excel_SheetList($oWorkbook) _FileWriteFromArray("C:\" & $SheetListOutput, $SheetList, 1) Result:
      ResultABC| ResultDEF| ResultGHI| ResultJKL| ResultMNO| It created these "|" vertical bars?
    • Skeletor
      By Skeletor
      Hi All,

      I would like to know how you would take a FileLineRead and insert it into an array which then inserts it into Excel?
      One thing to know is the files content is broken up, so I only use half of the content within $FileRead1.
      So its imperative that the $value1, $value2, etc variables be used. 
      Code below:
      $FileRead1 = FileReadLine("C:\temp\sample.txt",1) For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] $value4 = $input[4] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value4, "D1") Next  
×