Jump to content
Sign in to follow this  
levanduyet

Open Excel file (2007) and Enable macro

Recommended Posts

levanduyet

Dear All,

How can I use AutoIt to Open one Excel File in Excel 2007 and Enable Macro also.

Tks,

LVD

Share this post


Link to post
Share on other sites
exodius

For opening Excel files, look at the Help File for functions that start with _Excel.

As for enabling macros... Personally I'd try to rewrite the macro in AutoIt (for universality's sake) or try this post.

Edited by exodius

Share this post


Link to post
Share on other sites
levanduyet

For opening Excel files, look at the Help File for functions that start with _Excel.

As for enabling macros... Personally I'd try to rewrite the macro in AutoIt (for universality's sake) or try this post.

Thank Exodius,

Do you have another way?

LVD

Share this post


Link to post
Share on other sites
99ojo

Hi,

copied from a 'Juvigy' post, if you want to run the macro.

For macro security settings the link from @exodius should help.

Try:

$oExcel = ObjCreate("Excel.Application") ;crete excel object
$oExcel.WorkBooks.Open($FileName) ;open desired file containing the macro
$oExcel.Run("Macro1") ;run the macro

;-))

Stefan

Edited by 99ojo

Share this post


Link to post
Share on other sites
laffo16

hey guys im getting some strange things with excel, when i run _ExcelBookAttach from the latest offical build, i have an excel.exe process appear for a split second in my task manager, i think this causes the @error to return incorrectly. if i change bookattach method to FileName, it no longer causes the .exe to open. so i'm forced to use filename to have the error return correctly.

i want to use the $oExcel.Run("") macro method mentioned above, but it only seems to work for me with the returned obj from _ExcelBookOpen, the returned obj from _ExcelBookAttach does not let me use .Run, any ideas?

#include <Excel.au3>

Global $filename = "test1.xls"
Global $filepath = "C:\Documents and Settings\Administrator\Desktop\test1.xls"

$oExcel = _ExcelBookAttach($filename, "FileName")
If $oExcel = 0 Then
    $oExcel = _ExcelBookOpen($filepath, 1)
EndIf

_ExcelWriteCell($oExcel, "abc", 2, 2)
$oExcel.Run("TestMacro")
Edited by laffo16

Share this post


Link to post
Share on other sites
l3ill

I didnt know about this command when I wrote my code for something similar so I just used the normal Excel Open Sheet commands and the a Send command to run the macro from inside excel, you have to assign the macro a shortcut first though ;-)

...in case you cant get it to work the other way.

Share this post


Link to post
Share on other sites
levanduyet

hey guys im getting some strange things with excel, when i run _ExcelBookAttach from the latest offical build, i have an excel.exe process appear for a split second in my task manager, i think this causes the @error to return incorrectly. if i change bookattach method to FileName, it no longer causes the .exe to open. so i'm forced to use filename to have the error return correctly.

i want to use the $oExcel.Run("") macro method mentioned above, but it only seems to work for me with the returned obj from _ExcelBookOpen, the returned obj from _ExcelBookAttach does not let me use .Run, any ideas?

#include <Excel.au3>

Global $filename = "test1.xls"
Global $filepath = "C:\Documents and Settings\Administrator\Desktop\test1.xls"

$oExcel = _ExcelBookAttach($filename, "FileName")
If $oExcel = 0 Then
    $oExcel = _ExcelBookOpen($filepath, 1)
EndIf

_ExcelWriteCell($oExcel, "abc", 2, 2)
$oExcel.Run("TestMacro")

Yeah, It's a good question.

Anybody know how to run the macro, enable the macro in the attached workbook.

Tks,

LVD

Share this post


Link to post
Share on other sites
Juvigy

Change

$oExcel.Run("TestMacro")

to

$oExcel.Application.Run("TestMacro")

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
Sign in to follow this  

×