为什么我不能使用AHK脚本保存2个excel文件?

时间:2019-07-12 23:28:30

标签: excel com autohotkey

我想从我公司的数据库中获取信息,单击“导出到excel”按钮(所有这些都可以正常工作),然后将导出保存到特定的文件夹中。

第一个可以正常工作,保存然后退出。

第二个没有。即使我通过delete命令进入MsgBox,第二个文件TEST1也不会被删除。

然后弹出一个灰色的Excel窗口,其中没有工作表,然后一秒钟后消失,而第二个导出的工作表仍坐在那里。

如果不是唯一打开的excel工作簿,我不知道X2指的是什么。我是否需要以某种方式“真正”退出X1或其他?如果我尝试执行两个saveAs,似乎就失败了。

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

+^g::

;; generate an excel spreadsheet from our DB

Sleep 5000

X1 := ComObjActive("Excel.Application")
X1.Visible := True

If FileExist("M:\Current Users\aparisi\MaryFiles\TEST.xlsx") {

    MsgBox, "We found TEST"
    FileDelete, "M:\Current Users\aparisi\MaryFiles\TEST.xlsx"  

}

X1.ActiveWorkbook.SaveAs("M:\Current Users\aparisi\MaryFiles\TEST.xlsx")
X1.Quit()

;; props

;; generate a 2nd excel spreadsheet from our DB

Sleep 5000

X2 := ComObjActive("Excel.Application")
X2.Visible := True

If FileExist("M:\Current Users\aparisi\MaryFiles\TEST1.xlsx") {

    MsgBox, "We found TEST1"
    FileDelete, "M:\Current Users\aparisi\MaryFiles\TEST1.xlsx"
    MsgBox, "We apparently just deleted TEST1"

}

X2.ActiveWorkbook.SaveAs("M:\Current Users\aparisi\MaryFiles\TEST1.xlsx")
X2.Quit()

WinActivate Act! Premium Plus - TenThirtyOneServices
WinWaitActive Act! Premium Plus - TenThirtyOneServices

CoordMode, Mouse, Screen

MouseMove, 1172, 312
Click
Click
Sleep 1000
MouseMove, 1154, 688
Click
Sleep 1000

Return

1 个答案:

答案 0 :(得分:1)

尝试这样的事情

+^g::

;; generate an excel spreadsheet from our DB

Sleep 5000

X1 := ComObjCreate("Excel.Application")  ; create a new instance of Excel
X1.Visible := true                       ; make Excel visible
X1 := ComObjActive("Excel.Application")  ; make Excel active
X1.Workbooks.Add()                       ; create a new blank workbook  in the active instance

If FileExist("M:\Current Users\aparisi\MaryFiles\TEST.xlsx") 
{
    MsgBox, "We found TEST"
    FileRecycle, M:\Current Users\aparisi\MaryFiles\TEST.xlsx
    Sleep 500
    If !FileExist("M:\Current Users\aparisi\MaryFiles\TEST.xlsx")
        MsgBox, "We just deleted TEST.xlsx"
    else
        MsgBox, "We could NOT delete TEST"
}

X1.ActiveWorkbook.SaveAs("M:\Current Users\aparisi\MaryFiles\TEST.xlsx")
X1.Quit()
X1 := ""                     ; clear the variable


;; props

;; generate a 2nd excel spreadsheet from our DB

Sleep 5000

X2 := ComObjCreate("Excel.Application")  ; create a new instance of Excel
X2.Visible := true                       ; make Excel visible
X2.Workbooks.Add()                       ; create a new blank workbook in the active instance of Excel

X2 := ComObjActive("Excel.Application")
X2.Visible := True

If FileExist("M:\Current Users\aparisi\MaryFiles\TEST1.xlsx") 
{
    MsgBox, "We found TEST1"
    FileRecycle, M:\Current Users\aparisi\MaryFiles\TEST1.xlsx
    Sleep 500
    If !FileExist("M:\Current Users\aparisi\MaryFiles\TEST1.xlsx")
        MsgBox, "We just deleted TEST1.xlsx"
    else
        MsgBox, "We could NOT delete TEST1.xlsx"
}
X2.ActiveWorkbook.SaveAs("M:\Current Users\aparisi\MaryFiles\TEST1.xlsx")
X2.ActiveWorkbook.Close
X2.Quit()
X2 := ""  

; .....
Return