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-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

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-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

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-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

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-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

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-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

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-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

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-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

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-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

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

    • AzgarD
      By AzgarD
      Hi guys. I know this is a newbie topic, very newbie, but i've read a lot of stuff and still don't get it. I just need to copy something from Excel cell, paste this in other program, copy something in this program and paste in other Excel cell. Something like...
      Copy A2 Use some WindowActivate and MouseMove stuff and CTRL+C (not a problem) Go back to the Excel sheet Paste that content in C2 Then Copy A3 Use some WindowActivate and MouseMove stuff and CTRL+C (not a problem) Go back to the Excel sheet Paste that content in C3 ... And it goes on The problem is, how can i "communicate" with Excel and do this row change? Like A2 to C2 and A3 to C3 ... In a efficient way that can be done like hundreds of times.
      Very newbie question but still not understanding this.
       
      Ty guys.
    • Gowrisankar
      By Gowrisankar
      Dear members of the forum,
      I need to open excel files that may or may not need a password and finally move the files that needs password to manual queue.
      Is there a fastest way to do this?
       
      PS: I have a huge respect for the rules of this forum. I am not asking assistance to override any security measure. I just need to segregate the files that needs passwords.
    • MrCheese
      By MrCheese
      Hi guys,
      without including everything (unless you want it)
      I am copying data from a table in chrome and wanting to paste it into excel.
      Copying in Chrome works.
      I can paste it into the field i want by emulating goto -> ctrl V:
      WinActivate($dataload) WinWaitActive($dataload) Sleep(500) $oWorkbook1.Sheets("ItemReturn").Activate Sleep(500) $msg = "Measuring Sheet" conwrite() ttips2() Local Const $xlUp = -4162 With $oWorkbook1.ActiveSheet ; process active sheet $oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell) ; get a Range that contains the last used cells $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, "B")).Rows.Count ; get the the row count for the range starting in row/column 1 and ending at the last used row/column $iLastCell = .Cells($iRowCount + 1, "B").End($xlUp).Row ; start in the row following the last used row and move up to the first used cell in column "B" and grab this row number EndWith $NewStartCell = $iLastCell + 2 $msg = "moving to location" conwrite() ttips2() Sleep(250) Send("^g") WinWait("Go To") Sleep(100) Send("B" & $NewStartCell) Sleep(100) Send("{ENTER}") Sleep(500) Send("^v")  
      But, I want to use _excel_rangecopypaste, pasting from the clipboard
      _Excel_RangeCopyPaste($oWorkbook1.ActiveSheet, default, "B" & $NewStartCell,default,$xlPasteValuesAndNumberFormats) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeCopy Example 2", "Error pasting cells." & @CRLF & "@error = " & @error & ", @extended = " & @extended) however, this gives me error 4 , extended@:  -2147352567
      How can i fix this or find out how to debug this error?
       
      Thanks
    • Simpel
      By Simpel
      Hi.
      I try to figure out who is using a excel workbook which I can only open "read only". I use this code:
      #include <Array.au3> #include <Excel.au3> Local $sFile = ; excel file with path on a network drive Local $oExcel = _Excel_Open(True, True) Local $oTabelle = _Excel_BookOpen($oExcel, $sFile) Local $aUsers If IsObj($oTabelle) Then $aUsers = $oTabelle.UserStatus _ArrayDisplay($aUsers) EndIf If I am the one allowed to write to the excel file (I'm the first one who opened it) then I will get an array with myself:

      If my collegue opened the excel file first and I run the code I get the following error message:
      "H:\_Conrad lokal\Downloads\AutoIt3\_COX\Tests\test.au3" (9) : ==> The requested action with this object has failed.: $aUsers = $oTabelle.UserStatus $aUsers = $oTabelle^ ERROR The excel file is on a network drive. Is that's the problem?
      Regards, Conrad
    • robertocm
      By robertocm
      change linked image paths in excel 2007 Open XML Files with AutoIt and 7-zip:
      #include <File.au3> ;Change this Local $sFind = "C:\Users\MyUserName\Documents\MyImageFolder\My%20Image1.png" Local $sReplace = "C:\Users\ANOTHERUSERNAME\Documents\AnotherImageFolder\My%20Image1.png" Local Const $sMessage = "Directory to change excel image paths" Local $sFileSelectFolder = FileSelectFolder($sMessage, "") Local $sTempDir = @ScriptDir & "\testdir" ;Required 7-zip Local $PathZipProgram = @ProgramFilesDir & "\7-Zip\" If Not(FileExists($PathZipProgram & "\7z.exe")) Then MsgBox(16, "", "7z.exe not found in path " & $PathZipProgram) Exit EndIf ;look for excel files in selected directory and all subdirectories Local $SFileList = _FileListToArrayRec($sFileSelectFolder, "*.xls.;*.xlsm", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH) If Not @error Then For $i = 1 To $SFileList[0] DirRemove($sTempDir, 1) ;use x command to keep the folder stucture, -aoa Overwrite All existing files without prompt, use -r to unzip the subfolders from the zip file RunWait('"' & $PathZipProgram & '7z.exe" x -aoa -r "' & $SFileList[$i] & '" -o"' & $sTempDir & '" -y', $PathZipProgram, @SW_HIDE) __ReplaceImagePaths($sTempDir, $sFind, $sReplace) RunWait('"' & $PathZipProgram & '7z.exe" a -r "' & $SFileList[$i] & '" "' & $sTempDir & '\*" -tzip -y', $PathZipProgram, @SW_HIDE) Next Else MsgBox(16, "Error", "No files were found in the folder specified.") EndIf DirRemove($sTempDir, 1) Func __ReplaceImagePaths($sTempDir, $sFind, $sReplace) ;List all files with .xml.rels extension in the directory \xl\drawings\_rels Local $aFileList = _FileListToArray($sTempDir & "\xl\drawings\_rels", "*.xml.rels", 1, True) If @error = 1 Then ;MsgBox (0, "", "Path was invalid") SplashTextOn("Title", "Path was invalid", -1, -1, -1, -1, 1, "", 24) Sleep(2000) SplashOff() Exit EndIf If @error = 4 Then ;MsgBox (0, "No files", "No files were found") SplashTextOn("Title", "No files were found", -1, -1, -1, -1, 1, "", 24) Sleep(2000) SplashOff() Exit EndIf Local $iRetval ;Loop through the array For $i = 1 To $aFileList[0] $iRetval = _ReplaceStringInFile($aFileList[$i], $sFind, $sReplace) Next EndFunc  
      Some references:
      https://stackoverflow.com/questions/37145369/change-path-to-picture-links-in-excel http://www.jkp-ads.com/Articles/Excel2007FileFormat.asp EDITED:
      Note: it seems that if User Account Control (UAC) is enabled then 7zip is unable to overwrite the destination file (using the same name).
      In this case, a possible solution would be to rename the original excel file before (see _PathSplit in help file).
      In my case i prefer just to disable UAC
×