Using Octave to run Excel macro (from personal workbook) - excel

I'm following the methods of this Mathworks post (but using Octave 6.2.0 instead of Matlab)
https://www.mathworks.com/matlabcentral/answers/100938-how-can-i-run-an-excel-macro-from-matlab
to open an Excel file (Office 365 version) and execute a macro. It works very well to a point (i.e. on a test Excel file containing a test macro). However, in practice I'm using the xlsopen(...) , oct2xls(...), and xlsclose(...) functions in Octave to create the Excel file I ultimately want to use, so I think I either have to...
(a) use Octave to create an Excel file containing a VBA macro, or
(b) adapt one of the options in the Mathworks post to be able to run a macro from the 'PERSONAL.xlsb' workbook instead. I've tried changing the 'Macro1' name to PERSONAL.xlsb!Macro1 (with 'Macro1' coded into a module within the personal workbook) but this does not execute the macro and gives the following error in Octave:
error: com_invoke: property/method invocation on the COM object failed with error `0x80020009' - Exception occurred.
Invoking the macro coded into the personal workbook directly from the active workbook is successful (even if it's a different workbook), I just need Octave to do this step.
Are either of these possible to do, or is there another alternative? Thanks in advance.

Related

Run macro in blueprism

I writed a macro to get the status of the checkbox.But I encountered a little problem when running it on blueprism.
My macro name PERSONAL.XLSB!CheckBox.I can manually execute it successfully.enter image description here
But I got an error reminder when I run the Run Macro via blueprism.enter image description here
error message:The code stage could not be executed because an exception was thrown by the code stage. The macro '' PERSONAL.XLSB! 'CheckBox' cannot be executed. Macros may not be available in this workbook, or all macros may be disabled.
The methodology that Blue Prism uses when launching Excel does not automatically open the current user's PERSONAL.XLSB file.
There are three options to rectify this:
Open the current user's personal macro-enabled workbook using the MS Excel VBO's Open action, pointing the File attribute to the location of PERSONAL.XLSB. In environments running Windows 7 or later, this is usually C:\Users\<user name>\AppData\Local\Microsoft\Excel\XLStart (source). You should then be able to reference the macros as you have been with the Run Macro action within the MS Excel VBO.
Copy the pertinent macros to the file in question before processing it with Blue Prism, and invoke them simply by name using the Run Macro action of the MS Excel VBO.
Add the macro code to a modified version of the MS Excel VBO, refactoring per Blue Prism's official guidance on Extending the MS Excel VBO.

File corrupted after creating the second document based on template with macro

I have many Excel templates containing VBA code. Some of them create new documents based on other templates with VBA code. They all worked well until last Friday, but today I got a bad surprise: creating the first document based on a template with macro works well, but creating the second gets corrupted code.
I was able to reproduce the problem with two very small templates created from scratch:
double click on the first file to create the first document
double click on the second one to create another document
At this point you can see on the VBA editor that the VBA project for the second document is corrupted. Signs of corruption are wrong icons on the Wrokbook and Worksheet modules on the project explorer and message about catastrophic failure if you try to compile.
Using first the second template, then the first one will corrupt the second one that is opened.
Click here to get the files to reproduce the problem.
EDIT
If you don't want to download the files (I understand, thanks to the comment #Mathieu Guindon) you can create them by simply:
create a new Excel file
add a module with a simple sub that shows a message box
save it as a template with macro
repeat with a second file
Now you have the two files that you can use to reproduce the problem described above.
Microsoft has confirmed that the build 1905 is broken and corrupts the second Excel document created from a template with macros.
They sent me a (long) list of steps to uninstall Office 365, run a script to clear some cache, create a new configuration and reinstall specifying the semi-annual channel for the updates and prevent the 1905 from being installed.
Excel 2019 MSO (16.0.11629.20210) 32-bit.
=== Tested: Opening sequence and filetypes ===
• FAIL - Open an Excel template that contains VBA, then another one that contains VBA.
• OK - Open an Excel template that contains VBA, then Excel template with no code in any module.
• OK - Open an Excel template that contains VBA, then an XLSB (Excel binary file) that was created by resaving any of the previous code-containing workbooks.
=== Tested: VBA Editor's Immediate window ===
• FAIL - In VBA Editor, open a code module from the second or any subsequent workbook that has VBA in it, then run a command in the Immediate window.
• OK - In VBA Editor, open a code module from the first workbook, then run a command in the Immediate window.
=== Couldn't find any MS article about it. ===
Not listed on https://support.office.com/en-us/article/fixes-or-workarounds-for-recent-issues-in-excel-for-windows-49d932ce-0240-49cf-94df-1587d9d97093 but it's definitely an Excel issue:
Can't open more than one macro-enabled workbook and have macros actually run, unless the 2nd and all subsequent macro workbooks are .XLSB.
Even with no addins loaded.
Symptoms:
• Excel is configured to allow macros, but no macros run.
• Excel is configured to prompt for macros, but doesn't.
• Attempting to run a macro in workbooks after the initial workbook causes Catastrophic Error, Automation Error, or Compile Error.

VBA and Excel running in parallel

This is a question that I have always had but never really gave it much though.
What I have at the moment is a worksheet that displays data, and the user refreshes whenever needed. So:
User triggers a VBA Function
VBA Function gathers data and analyses WHILE USER WAITS
VBA Function dumps the result on the spreadsheet
User continues viewing data
Since the data analysis is all done internally in VBA (No use of workbook, only recordsets, arrays, library etc.) I wanted to somehow be able to allow the user to continue viewing the original data, while VBA works on getting and analyzing new data.
I know you cant use the workbook AND run VBA at the same time, but you can however, have two excel instances and work on one workbook while the other runs VBA.
So could I somehow have my original excel instance call another excel instance and have it run the VBA while I work on my first instance?
Any Ideas?
(Also, not sure if the tag "Multithreading" is technically correct)
First thing - there is no multithreading for VBA in Excel.
Second thing - since Excel 2007, Excel supports multithreaded recalculation of formulas.
Therefore to approach multithreading calculations in Excel you can do at least 2 things:
Create a second instance of Excel (new Application instance! Not a new workbook within the same Application!) and execute the macro remotely.
Create UDF functions (User Defined Functions) in VBA. Unfortunately you cannot edit other cells using UDF but you can save the results of your computations in a Global variable and then print the results.
My recommendation - go with option 2.
I haven't been able to try this, but it seems like you can launch Excel from VBA using Application.FollowHyperLink. The hyperlink would have to be the local path to the sheet. You might have to use VBA to also make a copy of the sheet first.
Have you thought through the concurrency issues with having two copies?

How to keep reference to add-in UDF when workbook moved to different folder than add-in?

I wrote an Excel add-in that provides UDFs (user-defined worksheet functions).
All is well until one user sends his workbook using those functions to another user, or just tries to use the workbook on more than one computer, where the add-in has been installed to different paths.
Even if the only difference in the paths is the drive letter, when the workbook is opened on the other computer, the old full path appears on the formulas in front of all the UDFs, and the formulas return an error.
One way of dealing with that is doing a search & replace of all formulas in the workbook, replacing the path with an empty string. Then the formulas reset themselves for the add-in's path on the current computer. Sometimes I then have to go into the VBE and run a CalculateFullRebuild to get the formulas to work. Though it works, it's a lot to ask of the less technical users, and it's annoying to have to do it frequently for those users who move their workbooks around a lot.
Also -- do COM add-ins have this problem? My add-in is an xla. Though I'm curious about that, it's a moot point in this case since COM add-ins don't work on Macintosh Excel and I need this add-in to work cross-platform.
UPDATE:
As requested, here's a screenshot:
This screenshot shows what happens if Fred, who put the add-in in C:\Fred's Stuff\Fred's Excel Stuff\MyAddin.xla, sends his workbook to Martha, who put the same add-in in another path, such as C:\Martha's Files\Martha's Excel Files\MyAddin.xla, and Martha opens the file Fred sent.
If Martha deletes the path, leaving only "=MyUDF()" in the formula, Excel will find MyAddin.xla on Martha's computer in the path where Martha put it (assuming she had previously installed MyAddin.xla as an add-in in Excel), and resolve the formula correctly.
This is one of the many bugbears Excel developers need to face at one time or another, and there are a few work-arounds for it, but which one you choose will depend on your own circumstances:
http://www.jkp-ads.com/articles/FixLinks2UDF00.asp
In case the link dies, here is a summary of the three suggested methods:
Use fixed locations.
Instead of keeping your UDF code inside the addin, you create a facility that copies the UDF routine into each workbook that uses it.
Redirect the UDFs to the new location.

Excel VBA macro not found by worksheet, #NAME

I'm trying to get a simple VBA function to run in an excel (.xlsm) worksheet.
I created this function:
Function abc()
abc = 2
End Function
in Module2, and it worked.
But after I copied the spreadsheet to another system, it now just shows "#name" as if it can't find it. The function shows up on the available list of functions however.
This makes me think there is some kind of setting I need to enable, but I've enabled whatever I was prompted for. Any ideas?
I should've known it was in the trust center:
http://office.microsoft.com/en-us/excel-help/change-macro-security-settings-in-excel-HP010096919.aspx
Basically, hit the ball and poke around until you find "trust" and "enable macros" and select the least secure options.
And then close and re-open the spreadsheet.
I think we may need more information. What version of Excel are you using? Is it different from the destination system version?
You'll want to make sure that Module2 was in the Workbook you copied into the other system. Excel 2007 stores macros on a personal workbook by default sometimes so you'll need to check the the code is actually inside of the .xlsm file.
If you're still stuck and need a quick fix just copy the code text into the new system's Excel workbook directly without making a .xlsm file (create new module in the other system then paste).
If you would like to learn how to put together add-ins you can get started here or here.

Resources