Open excel spreadsheet from command line

Is there a way to hide the Excel spreadsheet from the command line. I am trying to execute a macro against a spreadsheet from the command line and do not want the spreadsheet to be visible.

Is this possible?
Who is Participating?
CEHJConnect With a Mentor Commented:

(see )
String[] command = {
"vbs.exe", "x.vbs" };
Process p = Runtime.getRuntime().exec(command);

Open in new window

Check excel /e
Though I don't know how to run the macro with that - still read here
Steve KnightIT ConsultancyCommented:
Can you put the macro in the worksheet_open, i.e. something like this ...
Alt F11 to go to VBA editor

Private Sub Workbook_Open()
Application.Visible = False

MsgBox "blah blah"

End Sub

That will hide the window, show a message box (you could run your macro here) and then quit.

You could also start it minimized from cmd prompt with

START /MIN "" "something.xls"
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Please don't post duplicate questions. This is a duplicate of http:Q_27492005.html

Since the above mentioned is not really a Java question and dragon-it seems to be on the case here, you should close the earlier one
waht kind of macro do you want to execute in your speradsheet without shoowing it?
Maybe it is simpler to run this macro in java code wfrom which you are calling it?
illfusion82Author Commented:
The macro formats a spreadsheet into a pivot table. I need this macro to run against the spreadsheet when no one is logged on to the computer. I have tried this without hiding the excel spreadsheet and it does not work. The process just hangs and I have to terminate it. I know this can be done in vbscript with the below code. But how would I achieve this with Java code?

Option Explicit

Dim oArgs

set oArgs = wscript.Arguments

Call OpenSpreadSheet( oArgs(0), oArgs(1) )

Sub OpenSpreadSheet( strFilePathCustomRpt, strMacroSheetName )
      'On Error Resume Next
      Dim xlMacro
      Set xlMacro = CreateObject("Excel.application")

      xlMacro.Application.Workbooks.Open strMacroSheetName
      xlMacro.Application.Workbooks.Open strFilePathCustomRpt
      xlMacro.Application.Visible = False "'RetinaPivotMacro.xls'!Retina_Macro"

      xlMacro.DisplayAlerts = False
      xlMacro.workbooks( "RetinaSummRpt.txt" ).SaveAs(strFilePathCustomRpt), 56
      xlMacro.DisplayAlerts = True

      Set xlMacro = Nothing

End Sub
Steve KnightIT ConsultancyCommented:
so why not add code to the sheet that does that on startup as suggested?

You could always do something like check an environment variable, or a file exists that you drop in the java in the worksheet_open event to see if you should open the sheet 'normally' or run the relevant macro?

If not over to the java coders...

>> I know this can be done in vbscript with the below code.

In that case, just use java to Runtime.exec the vb script you posted
illfusion82Author Commented:
dragon-it, I have tried with the worksheet_open event, but it still hangs and I have to terminate the excel.exe process.

CEHJ, your suggestion does work, and that is what I am currently doing, but would like to have Java handle executing the macro.
I can't think of any better way other than by looking into the Open Office UNO API to see if you can get it to run the macro
Steve KnightIT ConsultancyCommented:
Do we know WHY it hangs?  You could soon add exactly the same is in your VBScript to the workbook_open?

Maybe it is asking something silly like "enable macros?" ... does it show at all if yo open it visible?

What about if you try first launching a balnk sheet from your java with just an workbook_open event that makes a msgbox or something.

Then try your actual sheet with the same in...

And see how far that gets.  If that works then we can work on your macro part.

>>Do we know WHY it hangs?

Also do we know how you're doing it when it *does* hang? I'm not sure.

If you're doing it with Runtime.exec and it hangs, then you should read the link i posted carefully and thoroughly, including sub-links
illfusion82Author Commented:
I don't think it is asking to enable macros. Below is an article I found on running the excel macro against the spreadsheet through a scheduled task, which is what I am doing. I may just stick with the calling the vbscript to execute the macro for now, until I can find a better solution -- or this may be the best solution. I will leave this open for two more days to get more input.
Start /mim excel.exe {File path}\{File Name}
Steve KnightIT ConsultancyCommented:
@erobby http:#37281808
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.