AHK create new excel file

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ExcelHotKeys
Posts: 41
Joined: 21 Dec 2020, 10:27

AHK create new excel file

Post by ExcelHotKeys » 11 Apr 2021, 15:33

What is wrong with my code?

I cannot saveas the current excel file using the name saved in the clipboard......

Code: Select all

^+Y::
dir = C:\Users\sebas\Desktop\Saved
Gui, New
Gui, Font, s12 w500
Gui, Color, F8DC75
Gui, Add, Text, w320 Center, Please wait....
Gui, Show,, Working
If !FileExist(dir) {
 FileCreateDir, %dir%
 If ErrorLevel {
  Gui, Destroy
  MsgBox, 48, Error, An error occurred while creating the directory. Aborting.
  Return
 }
}
FileRecycle, % file := dir "\" excelFix(Clipboard) ".xlsx"
xl := ComObjCreate("Excel.Application"), x1.visible := true, xl.activeworkbook.Saveas(file)
Gui, Destroy
If FileExist(file)
 MsgBox, 64, Done, Done!
Else MsgBox, 48, Error, The file could not be created.`n`n%file%

excelFix(string) {
 new := RTrim(string, "`r`n") ; Remove the trailing CRLF
 new := Instr(new, "`n") ? RegExReplace(new, """(.+)""", "$1") : new ; Remove bounding quotation marks
 Return StrReplace(new, """""", """") ; Fix double quotation marks
}
User avatar
mikeyww
Posts: 27070
Joined: 09 Sep 2014, 18:38

Re: AHK create new excel file

Post by mikeyww » 11 Apr 2021, 15:35

I believe that you will need the Quit function from your other post.

https://www.autohotkey.com/boards/viewtopic.php?p=393276#p393276
ExcelHotKeys
Posts: 41
Joined: 21 Dec 2020, 10:27

Re: AHK create new excel file

Post by ExcelHotKeys » 11 Apr 2021, 15:44

But that doesn't solve the issue. Spent 2 hours researching to no avail. Even with x1.visible :=True, it is not helping.
User avatar
mikeyww
Posts: 27070
Joined: 09 Sep 2014, 18:38

Re: AHK create new excel file

Post by mikeyww » 11 Apr 2021, 16:02

Code: Select all

^+y::
dir = %A_Desktop%\Saved
Gui, New
Gui, Font, s12 w500
Gui, Color, F8DC75
Gui, Add, Text, w320 Center, Please wait....
Gui, Show,, Working
If !FileExist(dir) {
 FileCreateDir, %dir%
 If ErrorLevel {
  Gui, Destroy
  MsgBox, 48, Error, An error occurred while creating the directory. Aborting.
  Return
 }
}
FileRecycle, % file := dir "\" (fn := RegExReplace(Clipboard, "[\r\n]")) ".xlsx"
xl := ComObjCreate("Excel.Application"), xl.Workbooks.Add, xl.ActiveWorkbook.SaveAs(file), xl.Quit()
Gui, Destroy
If FileExist(file) {
 Run, %file%
 WinWaitActive, %fn% ahk_exe EXCEL.exe,, 10
 If ErrorLevel
  MsgBox, 48, Error, An error occurred while waiting for the window.
} Else MsgBox, 48, Error, The file could not be created.`n`n%file%
Return
ExcelHotKeys
Posts: 41
Joined: 21 Dec 2020, 10:27

Re: AHK create new excel file

Post by ExcelHotKeys » 11 Apr 2021, 17:29

Code: Select all

^+y::
dir = %A_Desktop%\Saved
Gui, New
Gui, Font, s12 w500
Gui, Color, F8DC75
Gui, Add, Text, w320 Center, Please wait....
Gui, Show,, Working
If !FileExist(dir) {
 FileCreateDir, %dir%
 If ErrorLevel {
  Gui, Destroy
  MsgBox, 48, Error, An error occurred while creating the directory. Aborting.
  Return
 }
}
FileRecycle, % file := dir "\" (fn := RegExReplace(Clipboard, "[\r\n]")) ".xlsx"
xl := ComObjActive("Excel.Application"), xl.activeworkbook.SaveAs(file), xl.quit()
Gui, Destroy
If FileExist(file) {
 Run, %file%
 WinWaitActive, %fn% ahk_exe EXCEL.exe,, 10
 If ErrorLevel
  MsgBox, 48, Error, An error occurred while waiting for the window.
} Else MsgBox, 48, Error, The file could not be created.`n`n%file%
Return

Here you go.

Had to use ComObjActive instead of ComObjCreate
Post Reply

Return to “Ask for Help (v1)”