Pascal Galloway
Pascal Galloway

Reputation: 39

Executing and Interfacing with an External Program

Right, situation first:

  1. I have an excel workbook containing macros which carry out various operations.List item
  2. The resulting data is used by an extenal program (MathCad), which then outputs data to a .txt file. The catch is that Excel needs to be closed down before this operation
  3. The same workbook contains another macro to read the .txt results back into the excel workbook

I would like to be able to issue some commands which carry out all these operations at the click of a button. So far I have this:

Public Sub Execute_Mathcad()
Dim x As Variant
Dim Path As String
Dim File As String

'save application in current form (should be up to date)
ThisWorkbook.Save

'pause vb script so mathcad can run normally
Application.Wait (Now + TimeValue("0:00:20"))

'open the external program
Path = "C:\Program Files (x86)\Mathcad\Mathcad 15\mathcad.exe"
File = "C:\Users\blah\blah.xmcd"
x = Shell(Path + " " + File, vbNormalFocus)

'issue CTRL+F9 in active program - can the keystroke be issued through windows directly (this solves and saves the MathCad program)
SendKeys ("^{F9}")

'exit mathcad using VBA command


'import results (I have code for this)
End Sub

The main problem for me seems to be how to pause excel and allow the external program to run. It cannot run whilst excel is open since it has to pull data from the workbook for some reason?

Secondly, how can you issue a keyboard shortcut in an external program (e.g. CTRL+F9)

Thanks!

Upvotes: 2

Views: 8001

Answers (1)

RubberDuck
RubberDuck

Reputation: 12728

Write a bat or vbs file that will launch MathCad, Close Excel, and then relaunch Excel and execute the result import. Call the file from a shell command much like you're currently opening MathLab.

Upvotes: 1

Related Questions