• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 108
  • Last Modified:

How can I run an Excel macro from a command line

How can I run an Excel macro from a command line. The excel sheet is attached.

Regards,

Dallag
test3.xlsm
0
Mohammed Dallag
Asked:
Mohammed Dallag
  • 3
  • 3
1 Solution
 
ste5anSenior DeveloperCommented:
There is no simple way to do this. So the question is: Why do you want to this?

One option is to use the Workbook Open event, but then your macro is run always when you open the workbook:
Capture.PNG
Another option is to use a VBScript to automate Excel:

Option Explicit

Dim Excel

Set Excel = CreateObject("Excel.Application")
Excel.Visible = True
Excel.Workbooks.Open "C:\Temp\Book1.xlsm"
Excel.Run "YourMacroName"
Set Excel = Nothing

Open in new window


Where your macro is placed in a standard module.
0
 
Mohammed DallagPetroleum ConsultantAuthor Commented:
The VBS script is OK with me but I couldn't run the script. Please check

the vbscript.vbs file

Dim args, objExcel

Set args = wscript.arguments
set objExcel = createobject("Excel.application")

objExcel.workbooks.open args(0)
objExcel.visable = True

ObjExcel.Run "CommandButton1_Click"

ObjExcel.ActiveWorkbook.save
ObjExcel.ActiveWorkbook.Close(0)
ObjExcel.Quit 

Open in new window


the batch file to run the script
cscript script.vbs "G:\00 Aramco\000 Projects\000 Unconventional_Gas\UR_Jafurah\Codes\test3.xlsm" 

Open in new window

0
 
ste5anSenior DeveloperCommented:
The problem is that your CommandButton1_Click is an event procedure in the workbook. It is also declared private, thus it is not a runnable macro.

Modify your code.

Untitled.pngCapture.PNGCapture2.PNG
Option Explicit

Dim args, objExcel

Set args = WScript.Arguments
set objExcel = CreateObject("Excel.Application")

objExcel.Visible = True
objExcel.Workbooks.open args(0)
objExcel.Run args(1)

'objExcel.ActiveWorkbook.save
'objExcel.ActiveWorkbook.Close(0)
'objExcel.Quit 

Open in new window


Run as:
Capture.PNGtest3.xlsm
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mohammed DallagPetroleum ConsultantAuthor Commented:
very good but I am getting the warning attached. How can I get red of that?

Regards,

Dallag
2016-10-01_20-24-10.jpg
0
 
ste5anSenior DeveloperCommented:
You've entered either directly or by your default settings some "private" information.

See also Remove hidden data and personal information by inspecting workbooks.

But this should be a separate question.
0
 
Mohammed DallagPetroleum ConsultantAuthor Commented:
thank you for your help
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now