Solved

How can I run an Excel macro from a command line

Posted on 2016-10-01
6
43 Views
Last Modified: 2016-10-02
How can I run an Excel macro from a command line. The excel sheet is attached.

Regards,

Dallag
test3.xlsm
0
Comment
Question by:dallagmm
  • 3
  • 3
6 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 41824857
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
 

Author Comment

by:dallagmm
ID: 41824869
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
 
LVL 33

Accepted Solution

by:
ste5an earned 500 total points
ID: 41824881
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:dallagmm
ID: 41824895
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
 
LVL 33

Expert Comment

by:ste5an
ID: 41824915
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
 

Author Closing Comment

by:dallagmm
ID: 41824932
thank you for your help
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Modern/Metro styled message box and input box that directly can replace MsgBox() and InputBox()in Microsoft Access 2013 and later. Also included is a preconfigured error box to be used in error handling.
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question