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.
Related
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.
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
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.
I've just upgraded to Excel 2010 from 2003. An internal add-in that is heavily used uses the Application.GetSaveAsFilename method to prompt for file names to be used for an export process (exporting information from the current Excel file into an xml configuration file).
In 2003, even if they selected a Read Only file, I didn't get any prompts (which is what I want) leaving it up to me to handle read-only issues (which I do...e.g. I check the file out of source control). However, after upgrading to 2010, I can't select a filename if that file exists and is readonly, forcing me to manually go checkout files first (which is a major downer in terms of proficiency when I'm exporting ~60 files per day).
Does anyone know of any settings/workarounds so that Excel 2010 doesn't prevent (or even prompt hopefully) selecting a filename of a previously existing/readonly file?
Thanks in advance.
Does it help if you use Application.GetOpenFilename instead?
Sub PromptForFilename()
ret = Application.GetOpenFilename
MsgBox ret
End Sub
This doesn't give me any errors for a read-only file.
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]