Solved

SendKeys in mex file

Posted on 2010-08-25
3
978 Views
Last Modified: 2016-03-02
Hey-- I have an Excel workbook containing macros that needs to be opened and run automatically with a Matlab EXE file. I have the VBA macros in an Auto_Open sub so that they are run immediately upon the opening of the workbook, and I've written Matlab code that automatically opens the Excel workbook. The problem is, when Excel opens, the "Enable or Disable Macros" prompt pops up, and it has to be clicked out of manually in order for the workbook to be opened and the macros to automatically run. The normal way around this is apparently to set the Macro Security level to "Low" which results in the workbook automatically being open with the macros enabled, but due to security settings on my computer I am unable to change the Macro Security level to anything lower than Medium.
 
In order to programmatically get past the prompt with macros enabled, it occurred to me that all that one would theoretically have to do is press "Tab" to select the "Enable Macros" button, and then "Enter" to get past the prompt, and that this could programmatically be done with the Matlab analog of the VBA "Sendkeys" function. After researching this, I can't find anything in Matlab that replicates the functionality of SendKeys, but it looks like it's actually possible to do in either C or C++, which can then be written into a mex file and executed in Matlab. I'm not really understanding how Sendkeys works in C/C++, though... is it possible to have C/C++ automatically press Tab,Enter in a mex file?

 Any help will be greatly appreciated!!
0
Comment
Question by:Jeff9687
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
exx1976 earned 500 total points
ID: 33524685
SendKeys isn't VBA, it's VBS.

If you need it to work, then write a VBS file with the proper sendkeys stuff in it, save it as a VBS, and then have matlab call that file instead of a C/++ executable.


HTH,
exx
0
 

Author Comment

by:Jeff9687
ID: 33524808
Hmm... OK well just using VB sendkeys (which I already know how to do) would certainly be easier than figuring out how to do it in C and then using a mex file to execute it in Matlab, but how do you call a VBS file from Matlab??
0
 

Author Comment

by:Jeff9687
ID: 33525705
nevermind, i figured it out:

set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 100
WshShell.AppActivate "Microsoft Excel"
WScript.Sleep 1000
WshShell.SendKeys "{TAB}"
WScript.Sleep 1000
WshShell.SendKeys "{ENTER}"

Save in notepad as enablemacros.vbs, then in Matlab, call:
system('cscript enablemacros.vbs')

thanks for suggesting doing it in vbs!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now