Command line switch to open Excel Template as a Workbook - excel

We have a shortcut (.lnk file) to an Excel 2010 Template (.xlxt file), with the following target specified:
"C:\Program Files\Microsoft Office\Office14\EXCEL.EXE" "\\MyPath\MySpreadsheet.xltx"
However, opening this shortcut opens the Template version of the file. We need it to open the Workbook version (as if someone had right-clicked on the file itself and selected 'New'.
Does anybody a command line switch I can add to the target above to force the file to open as a Workbook.
Many thanks for any help,
Martin

Removing the call to EXCEL.EXE has solved this.
So, the following target:
"\MyPath\MySpreadsheet.xltx"

I have replaced the "excel.exe" call and it works like a charm. The template generates a new workbook derivated from.
// System.Diagnostics.Process.Start("Excel.exe", #"C:\Book1.xltm");
System.Diagnostics.Process.Start(#"C:\Book1.xltm");
I have placed a automatic start of the VBA macro into the workbook module on Open event.
Private Sub Workbook_Open()
Module1.Macro1
End Sub
And the action is executed for any workbook generated from the template at start.
So few lines of code, but efficient.
An alternative is to use the official command line arguments : /t workbook path | file name
Starts Excel and opens the specified file as a template.
Example
excel.exe /t "c:\My Folder\book_name.xlsx".
See reference :
https://support.office.com/en-us/article/Command-line-switches-for-Excel-321cf55a-ace4-40b3-9082-53bd4bc10725

Related

Solidworks PDM Launching Excel with Parameters

I have a Solidworks PDM transition that launches a particular Excel sheet that has a Workbook_Open routine that receives a parameter passed in from PDM. That parameter is the %folderpath% of the file being transitioned in PDM. The Excel routines run fine. However, Excel is trying to do too much with the passed in path. After the routine in Excel runs, an Excel dialog is throw as shown here:
Excel is mashing the passed in path with an .xlsx extension (its running a .xlsm file). Here is the command line being executed in PDM "C:\Program Files (x86)\Microsoft Office\Root\Office16\EXCEL.EXE""C:\Users\guy\Desktop\TEST FOLDER RENAME.xlsm" /e/ %FolderPath% where the full folder path is C:\PTI\ECO\General\CR-0023482. Any ideas on why Excel is throwing this message and is there a way to suppress/stop it?
I found the issue and it is subtle. The command line in PDM's transitions action cannot have a space between the called spreadsheet and the passed in parameter. This argument works. "C:\Temp\F-Rename.xlsm"/e/%FolderPath% vs. "C:\Temp\F-Rename.xlsm" /e/%FolderPath%.

Application functions doesn't work when you open another file in "safe mode"

I have a workbook that runs every x minutes with Application.OnTime.
The problem is that if I open a different workbook and get the yellow ribbon thing asking if I want to edit the file (or macro safety warning) the Application.OnTime stops working.
There is another question quite similar to mine here:
Application.ontime failes, when another workbook is open in "safe mode"
But the answer is not really an answer since that will just skip the line and make the code not run OnTime.
Is there anything that can be done to still allow Application.OnTime to run even if I have another file open in safe mode?
I think the solution here is opening that conflictive workbook in a different instance of Excel, so it won't be affected by other files.
To manually open a new instance of Excel, hold key ALT and click on the Excel icon. But in your case, if several people are using the file, then everybody needs to do that.
I've been looking around about creating a BAT file that opens that file in a new instance, but I'm totally noob about ms-dos commands so I'm posting this answer and hoping somebody can help.
To create a BAT FILE is really easy: just create a TXT, type the commands, save it and change file extension to BAT.
Be aware that I'm using my PC paths, so you need to adapt it to your paths. I've tried a lot of things but can't manage it to work it out:
"C:\Program Files (x86)\Microsoft Office\Office12\EXCEL.EXE" will open Excel in a new instance.
start "excel.exe" "C:\filepath\yourfile.xlsx" will open the file but in active instance. IF there is no instance, it will create a new one, but this does not work as you need it
"filepath\yourfile.xlsm" will do the same than step 2
So I've been not able to find the exact commands. But I'm pretty sure there must be a way that a BAT file opens a new Excel instance (like in step 1) and opens the file in that instance.
About your macro, another shot in the dark, but you posted I have a workbook that runs every x minutes with Application.OnTime don't know how many times do you need to execute it, but maybe with Task Scheduler you could automate the process, because Task Scheduler got an option to open files in new instances.
Sorry, but could not find the exact commands. I'm able to open Excel in a new instance, (step 1) and I'm able to open a specific file, but I'm not able to combine both of them.
Hope somebody can help with that part, so you can create a BAT file to execute that conflictive workbook in a different instance just by double clicking on that BAT file (so everybody won't need to remember about the ALT thing)
WORKAROUND 2: Other way would be executing that Excel file but from a different APP, so you can create a new instance of Excel from VBA. I made a simple Word file that creates and opens an Excel file in a new instance, so you can work with rest of Excel files. The code must be in the event Document_Open.
Private Sub Document_Open()
Application.ScreenUpdating = False
Application.Visible = False
Dim ExcelAPP As Object
Dim ThisWB As Object
Set ExcelAPP = CreateObject("Excel.Application")
ExcelAPP.Visible = False
Set ThisWB = ExcelAPP.Workbooks.Open("filepath\yourfile.xlsm")
ThisWB.Close False
DoEvents
ExcelAPP.Quit
Application.ScreenUpdating = True
Application.Visible = True
Application.Quit False
End Sub
So just by opening this word file, your Excel File would be executed in a new instance and not visible.
KNOWN FAILS: This way is good if you already have an Excel instance opened, but if there is no Excel file opened (you close all of them), you execute this code, and then open an Excel File, the Excel file will be opened in that invisible instance, so it's not perfect.

Excel vba open read only file error

I have a vba macro to open Excel files, including Read-only files.
I use the following code to do this:
Workbooks.Open((FileName:=MyFileName, UpdateLinks:=0, IgnoreReadOnlyRecommended:=True)
Up until June 2013 this never failed. As of now this no longer works. Now when the macro encounters a read-only file the file appears in a separate window and a Save As prompt also appears (never happened before).
If I click Cancel I get a Path not found error. If I click Save the file gets saved to its original location. This is completely new. There is no Save As command in this part of my code.
It appears to be something new associated with Microsoft’s File > Protect Workbook > Mark as final stuff. Anyone seeing this? I can’t find a way to prevent it. I simply want to be able to open the read only file without further prompts.
I haven't used VBA with excel much, but is it possible that the macro security settings within excel were reset so that it no longer trusts macros or VBA script attempting to use it.
Thanks for the input. I did find a work-around. In newer versions of Excel there is a new ReadOnly designation: Final, i.e. File > Protect workbook > Mark as Final. This is the same as ReadOnly, just by another name, and it does not show up as an option in WorkBooks.Open(FileName:= ...). If I set the Workbook.Final property = False, then I can open the file w/o the SaveAs prompt.
If wbName.Final = True Then
wbName.Final = False
End If

Closing Excel from Applescript without generating errors

I have an Applescript that is designed to convert .xlsx files into .csv format, but it's having problems closing Excel after it's done processing, such that the next time Excel opens it gives the error message "excel saved changes to the file before the application quit unexpectedly". If I run the Applescript twice in a row, this error message causes the second execution to crash. Is there a way to either: close Excel more gently, so that it doesn't register the error; or tell Applescript to ignore the error and proceed with the rest of the program?
For reference, here is a simple Applescript that has the same problem (replace "etc.xlsx" with a path to some .xlsx file):
set fullpath to alias POSIX file "etc.xlsx" as text
tell application "Microsoft Excel"
activate
open fullpath as text
close active workbook
quit
end tell
I don't get the error you describe. You have some confusing file path stuff going on. Maybe that's your problem. To get the string path to a file use the following code and put the result path in for your fullpath variable.
(choose file) as text
Then here's the code to work with it.
set fullpath to "some:path:test.xlsx"
tell application "Microsoft Excel"
activate
open file fullpath
close active workbook
quit
end tell
It works without error.

bat file to open excel run macro close excel

Issue : I need to open an excel document, run the macro, save the document in a sepcific name (XXXX_YYYYMMDD), close excel document.
Solution 1:
I think i can use a .bat file to open the excel, run the macro , save and rename the file.
Issue of solution 1:
a) I do not no how to pass the path of the file as a parameter.
b) I do know how write the process in script language for the .bat
Solution 2:
I think i can use a .bat file to open the excel, and create a macro that once excel open it run and rename the excel and close it.
Issue of solution 1:
a) I do not no how to pass the path of the file as a parameter.
PS: Totally new with .bat coding
Other proposal are most welcome
Thank you very much for your help
To answer your first question, you can pass the filename as a parameter by using
batfile.bat yourexcelfile.xls
and access it from the batch file using %1.
However, I don't know how to do the other macro things, but if you find out you can use this to put it together! :)
I am not sure if this is possible with BAt-Files, but I think a powersheel script could do it....
If this is not an option you might try to add the macro to the startup of the file - so it gets run when the file is opened...
You can try with a hybrid bat/jscript file:
rem ("open macro")/*
#echo off
call cscript //nologo //E:jscript excelfile.xsls
goto :eof
*/
var objExcel = new ActiveXObject ("Excel.Application");
objExcel.Application.Run(""+WScript.Arguments.Item(0)+!mymacro");
rem (){}
If the macro is embeded in a sheet you'll need to create a sheet object and access trough it.

Resources