从VBA执行和连接外部程序

时间:2014-08-28 14:54:46

标签: excel-vba vba excel

对,情况优先:

  1. 我有一个excel工作簿,其中包含执行各种操作的宏。列表项
  2. 结果数据使用结果数据(MathCad),然后将数据输出到.txt文件。问题是Excel需要在此操作之前关闭
  3. 同一工作簿包含另一个宏,用于将.txt结果读回到excel工作簿
  4. 我希望能够发出一些命令,只需点击一下按钮即可执行所有这些操作。到目前为止,我有这个:

    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

    我的主要问题似乎是如何暂停excel并允许外部程序运行。它在excel打开时无法运行,因为它必须出于某种原因从工作簿中提取数据?

    其次,如何在外部程序中发出键盘快捷键(例如CTRL + F9)

    谢谢!

1 个答案:

答案 0 :(得分:0)

编写将启动MathCad,关闭Excel,然后重新启动Excel并执行结果导入的bat或vbs文件。从shell命令调用该文件,就像您当前打开MathLab一样。

相关问题