Windows 7 Execute a Excel macro from other aplication

Jose Luis9400

Active Member
Joined
Jan 19, 2017
Messages
2
Hi,

I have a Excel macro that copies a specific cell value. Is posible to execute it from another application without losing the focus?

Thanks in advance.

Regards,
Jose Luis
 


Solution
I know you can do this via a VBS file, copy the following and paste into notepad and save with a .vbs extension:

Code:
Option Explicit

On Error Resume Next

ExcelMacroExample

Sub ExcelMacroExample()

  Dim xlApp
  Dim xlBook

  Set xlApp = CreateObject("Excel.Application")
  Set xlBook = xlApp.Workbooks.Open("C:\temp\Book1.xlsm", 0, True)
  xlApp.Run "MyFunction"
  xlApp.Quit

  Set xlBook = Nothing
  Set xlApp = Nothing

End Sub

Adjust the workbook location and Function name to your function you wish to execute.
If you need to run this as a command I'm sure you could execute "wscript yourfile.vbs", or perhaps cscript instead.

If you need to do this within your own program you are righting without any external scripting Microsoft...
I know you can do this via a VBS file, copy the following and paste into notepad and save with a .vbs extension:

Code:
Option Explicit

On Error Resume Next

ExcelMacroExample

Sub ExcelMacroExample()

  Dim xlApp
  Dim xlBook

  Set xlApp = CreateObject("Excel.Application")
  Set xlBook = xlApp.Workbooks.Open("C:\temp\Book1.xlsm", 0, True)
  xlApp.Run "MyFunction"
  xlApp.Quit

  Set xlBook = Nothing
  Set xlApp = Nothing

End Sub

Adjust the workbook location and Function name to your function you wish to execute.
If you need to run this as a command I'm sure you could execute "wscript yourfile.vbs", or perhaps cscript instead.

If you need to do this within your own program you are righting without any external scripting Microsoft has extensive support in .NET via the Excel.Application COM interface.
 


Solution
Back
Top