Jump to content

Check for macros in excel


Recommended Posts

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.

Link to comment
Share on other sites

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

BTW:

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

is only true on English systems.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

#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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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...