Link to home
Start Free TrialLog in
Avatar of Jeff9687
Jeff9687

asked on

modify built-in matlab mex file

Hey-- I am attempting to automatically open an excel workbook with Matlab code that has macros running automatically when it opens via an Auto_Open sub in VBA. When I use:

exlWkbk = Excel1.Workbooks;
exlFile = exlWkbk.Open('filename.xls')

Excel opens with the macros enabled, but the macros do not automatically run upon opening despite being in the Auto_Open sub, and I am also unable to change the Macro Security level on my computer. I noticed that if I use the Matlab winopen function to open the workbook, however, it prompts me to click disable or enable macros, and when I click enable, it opens and the Auto_Open macros do automatically run.

My idea was to open it with winopen and then write code to programmatically select enable from the prompt, which I've been able to do via Sendkeys code in VB which I've saved as a .vbs file... I am able to call it in Matlab and it does successfully click enable and get through the prompt. The problem is, I have to call winopen to get to the prompt and then call vbs to get through it, and winopen does not stop executing until the prompt is already clicked out of... Since Matlab is open-source my new idea was to modify winopen so that it just stops executing right when it gets to the prompt so taht the vbs file can then be executed to do it, but while winopen is an open-source m-file, it seems to use a helper win_open_mex function that is a Matlab mex file. I figured I could modify the C code in the mex file and go from there, but I'm unable to access the mex code... I open it in Notepad, WordPad, Word, etc and it's all just giberish...

Is there any way to either access the win_open_mex code to modify it, or to approach the whole thing any other way that may work better and be easier??

I really appreciate your help.
Avatar of LeeeRussell
LeeeRussell
Flag of United Kingdom of Great Britain and Northern Ireland image

I have to say that I'm struggling to understand what your question is.  You seem to be trying to solve a problem that's arisen because you're trying to solve a different problem.

I think your real issue is how to open an excel file with macros enabled, but, as I said, I'm struggling to say that for certain.

I strongly suggest that you either restate your original issue here, without all the references to Matlab or ask a new question that only states your original issue.
Avatar of Jeff9687
Jeff9687

ASKER

Hey-- yeah, I'm sorry... I do understand how the original post could come off as a little convoluted! Let me try to clarify the whole situation:

ORIGINAL ISSUE: I need to write Matlab code that automatically opens an Excel workbook. The workbook has macros in an auto_open sub, and when the Matlab code opens the workbook, the macros in auto_open need to automatically run.

Secondary problem: Workbooks.open in Matlab doesn't seem able to do this because of Excel macro security settings that I'm not allowed to change on my computer. Winopen opens the workbook, but the user is prompted to click "Enable macros"... afterwards, once "enable macros" is clicked in the prompt the macros in auto_open do run, but I need "Enable macros" to be automatically clicked, which I do via a vbs file using SendKeys that I call in Matlab. The problem is, I can't call the vbs file in Matlab until winopen is finished executing, and winopen doesn't stop executing until the prompt is already clicked out of and the workbook opens... my approach was to modify winopen since Matlab is open source so that it just stops once it gets to the prompt, but it includes a built-in mex file win_open_mex that I can't seem to access.

I hope this clarifies things... I'm really kind of stuck!
When you open the Excel spreadsheet directly (not from Matlab) do you have to click on "Enable macros"?
Yes, because the Macro Security level in the workbook is set to Medium... if it were set to low then there would be no prompt and it would automatically open with the macros enabled, but I am not allowed to change the security level on my computer.
There's a couple of other things you could try then I suppose.

Is there any chance you could contact your IT department and let you change the security level on you computer?

Have you thought about running something like an AutoIT script from Matlab?  You could write a script in AutoIT that would load the excel file and then press the enable macros button?  This doesn't sound much less convoluted than the method you were trying though.
I already tried contacting IT... no dice. The security level can't be changed...

As far as AutoIT in Matlab goes I actually don't know how this works, but I already have a vbs script that's called in Matlab anyway that's able to press the "enable macros" button... the problem is that I need to open the workbook in Matlab in a way that brings up the prompt, that also stops executing once the prompt comes up so that I can then call the vbs script to press the button... winopen opens the workbook and the prompt does come up, but it doesn't stop executing until the prompt is already clicked out of so I'm not able to call the vbs file to click it...

If I've understood your reply correctly, then something like an AutoIT script is capable of waiting until things have finished executing - that's one of the reasons I suggested it.
Yeah, the problem is, though, that the vbs script is what gets through the prompt, it can't be run until winopen is finished executing, and winopen doesn't stop executing until the prompt is already passed so winopen will never stop executing... that's why my original idea was to modify winopen so that it just stops executing when the prompt comes up instead of waiting until the prompt is gotten through, but I can't because it works via a built-in mex function that I haven't been able to access...
Yes, but if I've understood your problem correctly, you could get winopen to run the AutoIT script, which in turns opens the Excel file you need, and presses the buttons, then exits.  Would that solve your problem, or am I missing some intricacy of this problem?
ASKER CERTIFIED SOLUTION
Avatar of yuk99
yuk99
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial