A weird problem of converting xls to xlsx Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
songdg
Posts: 559
Joined: 04 Oct 2017, 20:04

A weird problem of converting xls to xlsx

Post by songdg » 16 Mar 2023, 04:20

I want to convert some xls(tsv in fact) files to xlsx format.

Code: Select all

xlApp := ComObjActive("Excel.Application")
xlWb := xlApp.ActiveWorkbook
FilePathNoExt := RegExReplace(xlWb.FullName,"\..*?$") ; Remove last . and everything after
xlApp.DisplayAlerts := false ; avoid overwrite alerts
xlWB.SaveAs(FilePathNoExt ".xlsx", xlOpenXMLWorkbook := 51)
xlApp.DisplayAlerts := true
If I open one xls file manually, and then run the code above it works well. However if I want to open that xls file automatically and then convert it, problem occurs.

Code: Select all

Run, %A_WorkingDir%\Table.xls
Sleep, 2000
ControlGetPos , X, Y, Width, Height, Button1, Microsoft Excel
ControlClick, Button1, Microsoft Excel
Sleep, 2000
Click, %X%, %Y%
xlApp := ComObjActive("Excel.Application")
xlWb := xlApp.ActiveWorkbook
FilePathNoExt := RegExReplace(xlWb.FullName,"\..*?$") ; Remove last . and everything after
xlApp.DisplayAlerts := false ; avoid overwrite alerts
xlWB.SaveAs(FilePathNoExt ".xlsx", xlOpenXMLWorkbook := 51)
xlApp.DisplayAlerts := true
Image
Attachments

[The extension xls has been deactivated and can no longer be displayed.]


User avatar
mikeyww
Posts: 26879
Joined: 09 Sep 2014, 18:38

Re: A weird problem of converting xls to xlsx

Post by mikeyww » 16 Mar 2023, 07:09

You do need an open file if you are going to use ComObjActive. I think you complicated the testing process by combining so many steps. I simplified and found that the following basic "conversion" process (saving the file) worked. You could test this and see if it works for you. If it does, then you can expand the script.

Code: Select all

#Requires AutoHotkey v1.1.33
xls  := A_ScriptDir "\test2.xls"
out  := A_ScriptDir "\test2-new.xlsx"
XL   := ComObjActive("Excel.Application")
xlWb := XL.ActiveWorkbook
xlWB.SaveAs(out, xlOpenXMLWorkbook := 51)
MsgBox 64, Status, Done!
Start with a new XLS file, saved in XLS format. Verify that you can first open the file normally in Excel.

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: A weird problem of converting xls to xlsx  Topic is solved

Post by flyingDman » 16 Mar 2023, 13:05

Your table.xls file is not an Excel file (see error msg below). It won't open in excel if you double click it (but it will open in a text editor if you change the extension to .txt). It is just a tab separated values file and the best way to incorporate it into an existing xlsx file is to read it with fileread like so.

Code: Select all

fileread, var, table.xls
clipboard := Var
xl := ComObjActive("excel.application")
xl.activesheet.range("a1").pastespecial(-4104)
or creating a new xlsx file

Code: Select all

fileread, var, table.xls
clipboard := Var
xl := ComObjCreate("excel.application")
xl.visible := true
wrkbk := xl.workbooks.add
xl.activesheet.range("a1").pastespecial(-4104)
You can then use wrkbk.saveas(path) to save it.

If you had a "real" .xls file you would do something like this:

Code: Select all

xl := ComObjCreate("excel.application")
flnm := "testfile.xls"
wrkbk := xl.workbooks.open(flnm)
wrkbk.SaveAs(substr(wrkbk.fullname,1,-4), 51)
wrkbk.close(0)
xl.quit()
20230316_151516.jpg
20230316_151516.jpg (23.23 KiB) Viewed 323 times
To avoid using the clipboard use:
Spoiler
14.3 & 1.3.7

songdg
Posts: 559
Joined: 04 Oct 2017, 20:04

Re: A weird problem of converting xls to xlsx

Post by songdg » 21 Mar 2023, 02:45

flyingDman wrote:
16 Mar 2023, 13:05
Your table.xls file is not an Excel file (see error msg below). It won't open in excel if you double click it (but it will open in a text editor if you change the extension to .txt). It is just a tab separated values file and the best way to incorporate it into an existing xlsx file is to read it with fileread like so.

Code: Select all

fileread, var, table.xls
clipboard := Var
xl := ComObjActive("excel.application")
xl.activesheet.range("a1").pastespecial(-4104)
or creating a new xlsx file

Code: Select all

fileread, var, table.xls
clipboard := Var
xl := ComObjCreate("excel.application")
xl.visible := true
wrkbk := xl.workbooks.add
xl.activesheet.range("a1").pastespecial(-4104)
You can then use wrkbk.saveas(path) to save it.

If you had a "real" .xls file you would do something like this:

Code: Select all

xl := ComObjCreate("excel.application")
flnm := "testfile.xls"
wrkbk := xl.workbooks.open(flnm)
wrkbk.SaveAs(substr(wrkbk.fullname,1,-4), 51)
wrkbk.close(0)
xl.quit()
20230316_151516.jpg

To avoid using the clipboard use:
Spoiler
Thanks, much appreciated for your help :dance:

songdg
Posts: 559
Joined: 04 Oct 2017, 20:04

Re: A weird problem of converting xls to xlsx

Post by songdg » 21 Mar 2023, 02:50

mikeyww wrote:
16 Mar 2023, 07:09
You do need an open file if you are going to use ComObjActive. I think you complicated the testing process by combining so many steps. I simplified and found that the following basic "conversion" process (saving the file) worked. You could test this and see if it works for you. If it does, then you can expand the script.

Code: Select all

#Requires AutoHotkey v1.1.33
xls  := A_ScriptDir "\test2.xls"
out  := A_ScriptDir "\test2-new.xlsx"
XL   := ComObjActive("Excel.Application")
xlWb := XL.ActiveWorkbook
xlWB.SaveAs(out, xlOpenXMLWorkbook := 51)
MsgBox 64, Status, Done!
Start with a new XLS file, saved in XLS format. Verify that you can first open the file normally in Excel.
Thanks, it save a lot of click work by using FindText.

Post Reply

Return to “Ask for Help (v1)”