📎 AI Summary:
The thread discusses how to execute an Excel macro from another application without losing focus. The second user provides a solution using a VBScript file to open the Excel workbook, run the macro, and close Excel, suggesting adjustments for file paths and macro names. Overall, the conversation offers practical scripting methods to automate macro execution externally, indicating a helpful and solution-oriented tone.

Jose Luis9400

Active Member
Joined
Jan 19, 2017
Messages
2
Thread Author #1
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...

Josephur

Windows Forum Admin
Staff member
Premium Supporter
Microsoft Certified Professional
Joined
Aug 3, 2010
Messages
1,283
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