bat file to open excel run macro close excel - 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.

Related

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.

Ignoring Command Line Arguments in Excel

Is there a way to make Excel ignore certain command line arguments? I am launching via a command similar to:
Excel "C:\Temp\TestingCLAs.xlsx" /e service=http://localhost:61023/api/PortlandDev/2/ReportSource?Order='100146309'^&Line='0010'
The service string gets read by some VBA in the Excel file and is used to update a query. However, when I launch Excel the command line argument interpreter see's two things:
Open "C:\Temp\TestingCLASs.xlsx"
Open service=http://....
I would like the interpreter to ignore the second argument, however I get the following error when opening:
"Sorry, we couldn't find
service=http://localhost:61023/api/PortlandDev/2/ReportSource?Order='100146309'&Line='0010'.xlsx.
Is it possible it was moved, renamed or deleted?
I know this isn't due to the VBA within the excel file because it happens even when testing with a blank excel file.
I found this reference article: https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/ and tried to escape the URL with a '^' character but, got the same result.
Also looking at the error above, a .xlsx is tacked on to the end of the second URL above when it wasn't specified in the initial call.
Is there a way to make the command interpreter not try to execute the URL?
Any ideas are very much appreciated. Thanks
Not sure why it works but passing a "/p" switch makes it so that the command interpreter does not try to automatically open the URL. For example, the following can be consumed by my internal VBA without the command interpreter trying to create a new workbook from the URL.
Excel "C:\Temp\Template.xlsm" /e /p http://localhost:61023/api/PortlandDev/2/ReportSource?Order='100142574'&Line='0010'

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

Command line switch to open Excel Template as a Workbook

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

Opening Excel and PDf files with Tcl Tk

I am having problems opening an existing Excel file with Tcl Tk. I am able to open an existing MS Word file with no problems. The code that I am using is as follows, also my test application has "package require tcom" included:
proc OpenFile {} {
#Path to file
set app [::tcom::ref getobject "C:\\Users\\Me\\Desktop\\Test.doc"]
#Change path to application
set this [$app Application]
#Open application
$this Visible 1
}
This code is executed by a button. Basically, Test.doc gets opened after the button is pressed.
I tried changing the file to an existing Excel file, and when I press the button the file opens for a split second, and then closes. This also happens with MS Access files, as well.
Does anyone know how to open an existing Excel file with Tcl Tk, and make it stay open? Additionally, for PDFs and text files, I understand that I cannot use Tcom to open these files. Does anyone know how to open PDFs, text, and other non-MS files with Tcl Tk?
I really appreciate your help!
Thank you,
DFM
Assuming you're on Windows and you just want to open a file (.xls, .pdf, ...) with its usual application (ie. not modifying the file from your script) you can just use "start" like this:
set TestDoc "My Test.xls"
eval exec [auto_execok start \"\" [list $TestDoc]

Resources