Solved

SendKeys in mex file

Posted on 2010-08-25
3
946 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

744 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

16 Experts available now in Live!

Get 1:1 Help Now