Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

HELP!!! I keep getting the following error when using the code snippet below:
-------------------------------------------- OLE exception from "Microsoft Office Excel" 'SLOC_Count_C_Sharp_Macro' could not be found. Check the spelling of the file name, and verify the file location is correct. Win32::OLE<0.1707> error 0x800a03ec in METHOD/PROPERTYGET "Run" at test.txt line 27 -------------------------------------------- <<< Code Snippet >>> use Cwd; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; $Win32::OLE::Warn = 3; ##my $myPersonalMacros = "D:\\Profiles\\P56739\\Application Data\\Micr +osoft\\Excel\\XLSTART\\Personal.xls"; my $metricsFile = "D:\\Profiles\\P65380\\Deskto\\myMetricsFile.csv"; my $Excel = Win32::OLE->new('Excel.Application') or die "\n\nOops, can +not start Excel\n";; my $Book = $Excel->Workbooks->Open("$metricsFile"); my $Sheet = $Book->Worksheets(1); $Sheet->Activate(); $Excel->Run("Personal.xls!SLOC_Count_C_Sharp_Macro"); $Book->Close; I have everything under the sun to get this to work! It makes no difference; same error returns. I.E. $Excel->Run("SLOC_Count_C_Sharp_Macro"); $Excel->Run("Personal.xls!SLOC_Count_C_Sharp_Macro"); Even... $Excel->Run("$myPersonalMacros!SLOC_Count_C_Sharp_Macro");
Ideally, what I want is to a have a perl script invoke the Win32::OLE macro 'Run' function in newly generated *.csv files that reference predefined macros residing in a Personal.xls file under D:\Profiles\P56739\Application Data\Microsoft\Excel\XLSTART. Manually, I can open a newly generated *.csv on my desktop, go to 'Tools', go to 'Macros', visibly see Personal.xls!SLOC_Count_C_Sharp_Macro in the Macro name box, highlight the name, and 'Run' the macro, which executes correctly every time. But for the like of me, I cannot get Win32::OLE via the 'Run' method to do the same thing. What's going here??? I would appreciate any insight to this silly problem. Thanks. BG

Code tags added by GrandFather

Replies are listed 'Best First'.
Re^2: Using Win32::OLE and Excel - Tips and Tricks
by jrsimmon (Hermit) on Aug 11, 2009 at 20:46 UTC

    This exception
    OLE exception from "Microsoft Office Excel" 'SLOC_Count_C_Sharp_Macro' could not be found. Check the spelling of the file name, and verify the file location is correct tells me that the string
    "Personal.xls!SLOC_Count_C_Sharp_Macro" is not meaningful to Win32::OLE, or is simply incorrect.

    I'm not sure that win32::ole allows you to run a macro from a workbook that you haven't opened. Have you tried this:

    my $macroExcel = Win32::OLE::Strict->new('Excel.Application') or die; my $macroBook = $macroExcel->Workbooks->open($myPersonalMacros) or die +; $macroExcel->run("SLOC_Count_C_Sharp_Macro");

    And please, please, read How do I compose an effective node title?, How do I post a question effectively?, and Markup in the Monastery!

Re^2: Using Win32::OLE and Excel - Tips and Tricks
by roboticus (Chancellor) on Aug 11, 2009 at 20:31 UTC

    Ouch! This post hurt my eyes. Using <code>code tags</code> would certainly help....

    ...roboticus