• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 808
  • Last Modified:

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.
0
Jeff9687
Asked:
Jeff9687
  • 5
  • 4
1 Solution
 
LeeeRussellCommented:
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.
0
 
Jeff9687Author Commented:
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!
0
 
LeeeRussellCommented:
When you open the Excel spreadsheet directly (not from Matlab) do you have to click on "Enable macros"?
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Jeff9687Author Commented:
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.
0
 
LeeeRussellCommented:
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.
0
 
Jeff9687Author Commented:
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...

0
 
LeeeRussellCommented:
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.
0
 
Jeff9687Author Commented:
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...
0
 
LeeeRussellCommented:
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?
0
 
yuk99Commented:
First of all MATLAB is not open source. It's true that most of the functions are implemented as m-files, but they cannot be redistributed. MEX files are compiled functions usually written in C/C++. Source codes for MEX files are not available (see my first sentence), so they cannot be modified without some kind of reverse engineering. Just forget about it.

For your problem you could add a line to your VBS script to open the Excel file first. Then use winopen from MATLAB to run this VBS script.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now