Export XLS file as HTML from Excel with Applescript - excel

I'm trying to export an Excel document (all sheets) as HTML. When using the code below I receive no errors and the script runs for several seconds (about 15) but never actually produces any output. I've tried replacing the file name "test.html" with a full path in both HFS and POSIX formats and I still get the same results -- nothing
tell application "Microsoft Excel"
tell active workbook
save as sheets filename "test.html" file format HTML file format -- doesn't seem to actually work
end tell
end tell
If anyone can see my error and/or has successfully gotten this to work in the past, I would love an explanation to my problem.
Thanks in advance.

Both of these worked for me:
tell application "Microsoft Excel"
tell active workbook
save as active sheet filename (POSIX file "...full path here..." as text) file format HTML file format
end tell
end tell
tell application "Microsoft Excel"
tell active workbook
save as active sheet filename "...full path here..." file format HTML file format
end tell
end tell
(Although I would expect it to just export the active sheet, it actually dumps out all of the sheets in the workbook.)

Related

How does Excel know that a file "was not recalculated before it was last saved". Can I trick Excel into thinking the opposite?

When linking workbooks in Excel, I often get an error like:
Links to xxxx.xlsx were not updated because xxxx.xlsx was not recalculated before it was last saved
This error pops up once for every linked value, which means in my case about 100 alerts I need to press OK for. Mysteriously, this alert comes even if xxxx.xlsx contains no formulas and hence no recalculation at all: it's completely full of values only.
So how does Excel know that a file has not been recalculated before saving? Is it looking at a particular xml value inside the ZIP file (xlsx) which I could tamper with? Is it looking at open date vs modified date that I could circumvent with the touch linux command? I'd like a solution Using the command line ubuntu if possible (I run windows WSL), so that I can use a script.
And what's more, xxxx.xlsx is really big, which over network (thanks COVID) at home is slow to open / recalc / save. So I really don't want to ever open this file in Excel.
Any ideas?
You could try adding this macro to your PERSONAL.XLSB file and then running it. It will ask you to select a file and then open it without allowing links to update.
Sub OpenWithoutUpdatingLinks()
Dim strFileName As String
strFileName = Application.GetOpenFilename
If strFileName <> "" Then Workbooks.Open FileName:=strFileName, UpdateLinks:=False
End Sub
This will allow you to open the file you're working on without getting the message about updating links.
However, if you actually need the links to update or need to create more links, then you need the linked file to be recalculated.
Let us know if you need instructions on adding a macro to your personal file and running it.

Excel VBA suddenly not able to open workbooks?

My problem is very strange.
I was programming a macro to open workbooks and extract data which was going fine when it hung one time. I had to end process in task manager and restarted excel.
Now even basic file opening code doesn't work.
My basic code is like this
File path is C:/ and fileName is Book1
Set wbOpen (filePath & fileName)
wbopen.activeSheet.Range ("A1") = "Test"
.close SaveChanges:= True
Msgbox "ok"
.... end sub
The code used to work and i would see book1 being opened on the taskbar and the cell A1 will be changed. Now i still get the Ok message but the cells arent changed and i dont see book1 being opened. Any idea? Pls and thank you
Tried it on another PC and it doesnt work, could be excel settings
Already reset settings using the /regserver method and deleted the .vbe files under AppData too.
Sorry, was a very stupid mistake, must have accidentally set file path to "C:/Excel" instead of "C:/Excel/"
Tracked it down by using the msg box to print out the file im trying to access

Applescript: Open Excel File and Save in a Different Format (same name and path)

I have a folder of .xls files of which some are genuine excel files and some are excel xml files (but still with the .xls extension). To work with the files in a separate program, I'm trying to convert them all to a consistent format (Excel 98-2004) and want to use an applescript to do this while retaining the original file names and directory structure.
As of now, I have a working loop and list of files, and am accomplishing everything but the save function:
set file_Name to "/Users/me/path/to/data.xls"
tell application "Microsoft Excel"
activate
open file_Name
save workbook as workbook (active workbook) filename file_Name file format (Excel98to2004 file format) with overwrite
close workbooks
quit
end tell
When I run this code, I get the following replies:
tell application "Microsoft Excel"
activate
open "/Users/drewmcdonald/Desktop/Madhupur_10February2014.xls"
get active workbook
--> active workbook
save workbook as workbook (active workbook) filename "/Users/drewmcdonald/Desktop/test.xlsx"
--> missing value
close every workbook
quit
end tell
The spreadsheets are successfully opening and closing, but the file format is not changing and I don't know what to make of the "missing value" error. Any idea how to get this to work? Thanks!
EDIT: Just noticed that the file format parameter is missing from the reply text, so I'm guessing the problem is in there.
Got it.
I needed to set a workbook name variable to get full name of active workbook before trying to save it. This is because the workbook name that Excel assigns as it opens sheets is somewhat inconsistent. Final code looks like this:
set file_Name to "/Users/me/path/to/data.xls"
tell application "Microsoft Excel"
activate
open file_Name
set wkbk_name to get full name of active workbook
save workbook as workbook wkbk_name filename file_Name file format Excel98to2004 file format with overwrite
close workbooks
quit
end tell

How to open a csv file from Excel with a particular line highlighted?

I have a large CSV file and I want to programmatically open it in excel with a particular line highlighted (I know the line number). What is the easiest way to this?
I think my options are:
Auto convert the csv file to an xlsx file. How can I do this from a script?
Give Excel some arguments when it opens up. No idea what command line arguments Microsoft products take.
Somehow interact with Excel after it opens up the CSV file and tell it to highlight a particular. Again not sure how.
I prefer Java/Python/Shell or anything that would work across Mac/Windows assuming the system has Excel installed. So, my best bet is probably #1 which brings me back to the question how can I convert a CSV file to a xlsx file.
You could run a basic vbs which avoids the need to have Excel already open, and conversion isn't necessary.
Paste the code below into a text editor NotePad
Change the path to your CSV file to suit (ie "c:\temp\test2.csv")
Save the file as something like MyCSV.vbs say to your Desktop
Click on the final vbs to open the CSV file to Row X (8 in the sample below)
Dim objExcel
Dim WB
Set objExcel = CreateObject("excel.application")
Set WB = objExcel.Workbooks.Open("c:\temp\test2.csv")
With objExcel
.Goto WB.Sheets(1).Rows(8)
.Visible = True
End With
this works simple save it in an empty workbook.
Private Sub Workbook_Open()
Workbooks.Open ("test.csv")
Range("8:8").Select
End Sub
also if you save that in your normal.dot (the default template document when opening excel) it will run on any document it opens. so what you could do is:
save this to your normal.dot
Private Sub Workbook_Open()
Range("8:8").Select
End Sub
then change the default application for opening .csv files to excel. then whenever you double-click on .csv file it will be opened with excel and excel will run the Workbook_Open() sub and viola!

Why is this file always saved as FALSE.xls?

I wrote a VB script that creates an .xls file, based on .xlt file. Then it calls a macro from the .xls file that populates it with information from a database. In the last step the script saves the .xls file on the disk.
I did this before with VB and Excel 2003. Now I upgraded to Excel 2007 and before it saves the file, a window pops up and tells me that:
"The following features can't be saved in macro free workbooks:
VB project
...some yada yada about what the Yes and No option do.
And the yes and no buttons in the dialog box.
"
I want the script to automatically select and execute Yes in the dialog box. But I can't figure how to do this. I've also posted the script I wrote.
If you have a better approach for this please share.
Thank you,
Steve
Sub Main()
Dim xl_app
Set xl_app = CreateObject("Excel.Application")
xl_app.Workbooks.Open("E:\Work\Send Mail\Clienti.xls")
'Run the macro
xl_app.Run( "ImportData(""Data Source=SFA;Initial Catalog=Campofrio;
Integrated Security=SSPI;Connect Timeout=3000"", -1, 47)")
xl_app.ActiveWorkbook.SaveAs FileName="E:\Work\Send
Mail\Clients.xls",FileFormat=xlNormal
xl_app.Quit
Set xl_app = Nothing
End Sub
Now the cod works but instead of saving the file at the specified location, it saves it in My Documents folder under FALSE.xls.
Merging responses from the two duplicate questions the poster asked:
1
Preventing False.xls when saving files in Excel
2
You are using named parameters in the .SaveAs wrong. When writing out the named parameter you'll have to do it in the format
FileName:="e:\myfile.xls"
Notice the colon before the equal sign.
If you just write Filename="myfile.xls" then its a boolean comparison that will return false. And thats why it save the file as false.xls.
Really funny error I think. ;)
I'm not sure if this is the problem, but shouldn't you just be saving as a .xlsm instead of .xls, which lets you save a macro-enabled workbook in Office 2007? You can check which version the macro is running on and if it's Excel 2007 then save as .xlsm, .xls otherwise...
Do you really need to save it with the macros included? If not, use:
ActiveWorkbook.SaveAs Filename:="E:\Work\Send Mail\Clients.xls", FileFormat:=xlNormal
EDIT: the key is that the extension used should match the FileFormat specified. The above works for me (to exclude macros) and the below works for me (to include macros). Neither has any popup, and both end up in the right directory (as the other poster mentioned, you have to have the := if you specify FileName; otherwise, use the form below.
ActiveWorkbook.SaveAs "c:\temp\wordmacros\mybook.xlsm", xlOpenXMLWorkbookMacroEnabled

Resources