#NoEnv
#SingleInstance Force
SetTitleMatchMode, 2
CoordMode, Mouse, Screen
SetWinDelay, 50
#WinActivateForce

; 全局变量
global g_bStop := false
global g_WPSClass := "OpusApp"  ; WPS的窗口类

; === 辅助函数 ===
ActivateWindow(title) {
    SetTitleMatchMode, 2
    if WinExist(title) {
        WinActivate
        WinWaitActive, %title%, , 3
        if !ErrorLevel {
            Sleep, 300
            return true
        }
    }
    MsgBox, 无法激活窗口: %title%
    return false
}

ShowToolTip(message, duration) {
    ToolTip, %message%, A_ScreenWidth//2, A_ScreenHeight//2
    SetTimer, RemoveToolTip, %duration%
}

RemoveToolTip:
    ToolTip
    SetTimer, RemoveToolTip, Off
return

; === 主热键 ===
^!r::  ; Ctrl+Alt+R
    g_bStop := false
    Hotkey, Escape, On
    
    ; 第一步
    if (!g_bStop) {
        MouseMove, 1042, 104
        Sleep, 300
        Click
        Sleep, 500
        Send, ^c
    }
    
    ; 第二步 - 跳转到Invoicing log
    if (!g_bStop) {
        if (!ActivateWindow("Invoicing log")) {
            g_bStop := true
        } else {
            Sleep, 300
            Send, ^1
            Sleep, 500
            MouseMove, 320, 242
            Sleep, 300
            Click
            Sleep, 500
            Send, ^v
            Sleep, 500
            Send, ^l
            Sleep, 700
            MouseMove, 29, 273
            Sleep, 300
            Click
            Sleep, 500
            MouseMove, 64, 322
            Sleep, 300
            Click
            Sleep, 500
            MouseMove, 27, 165
            Sleep, 300
            Click
            Sleep, 500
            MouseMove, 51, 208
            Sleep, 300
            Click
            Sleep, 500
            MouseMove, 240, 230
            Sleep, 300
            Click, 2
            Sleep, 500
            Send, {Left}
        }
    }
    
    ; 第三步 - 等到excel/wps打开后跳转到Print
    if (!g_bStop) {
        ; 等待WPS窗口激活
        WinWaitActive, ahk_class %g_WPSClass%, , 5
        if ErrorLevel {
            ShowToolTip("等待WPS超时", 1500)
            g_bStop := true
        } else {
            Sleep, 800  ; 等待WPS完全打开
            
            if (!ActivateWindow("Print customer invoice journal")) {
                g_bStop := true
            } else {
                Sleep, 300
                Send, ^1
                Sleep, 500
                Send, ^l
                Sleep, 700
                MouseMove, 97, 20
                Sleep, 300
                Click
            }
        }
    }
    
    ; 第四步 - 跳转到Invoice basis - Sales
    if (!g_bStop) {
        if (!ActivateWindow("Invoice basis - Sales")) {
            g_bStop := true
        } else {
            Sleep, 300
            Send, ^1
            Sleep, 500
            Send, ^l
            Sleep, 700
            MouseMove, 30, 166
            Sleep, 300
            Click
            Sleep, 500
            MouseMove, 60, 207
            Sleep, 300
            Click
            Sleep, 500
            MouseMove, 238, 219
            Sleep, 300
            Click, 2
            Sleep, 500
            Send, {Left}
        }
    }
    
    ; 第五步 - 跳转到Voucher list
    if (!g_bStop) {
        ; 等待WPS窗口激活
        WinWaitActive, ahk_class %g_WPSClass%, , 5
        if ErrorLevel {
            ShowToolTip("等待WPS超时", 1500)
            g_bStop := true
        } else {
            Sleep, 800  ; 等待WPS完全打开
            
            if (!ActivateWindow("Voucher list | Historical year 2022 July")) {
                g_bStop := true
            } else {
                Sleep, 300
                Send, ^1
                Sleep, 500
                Send, ^l
                Sleep, 700
                MouseMove, 26, 161
                Sleep, 300
                Click
                Sleep, 500
                MouseMove, 66, 208
                Sleep, 300
                Click
                Sleep, 500
                MouseMove, 213, 265
                Sleep, 300
                Click, 2
                Sleep, 500
                Send, {Left}
            }
        }
    }
    
    ; 完成提示
    if (!g_bStop) {
        ShowToolTip("操作完成", 1500)
    }
    
    Hotkey, Escape, Off
return

; 取消热键
Escape::
    g_bStop := true
    ToolTip, 操作已取消, A_ScreenWidth//2, A_ScreenHeight//2
    SetTimer, RemoveToolTip, 2000
return

以上是需要修改的ahk脚本，主要原因是EXCEL / WPS还没完全打开就跳转到了PRINT SUPPLIER INVOICE JOURNAL，导致当EXCEL / WPS真的完全打开的时候，又从PRINT SUPPLIER INVOICE JOURNAL跳转回到了EXCEL / WPS。真正要在PRINT SUPPLIER INVOICE JOURNAL执行的操作没法执行。以下是成功的AHK 脚本，需要按照成功脚本修改以上脚本。务必让所有操作在正确的页面进行

#NoEnv
#SingleInstance Force
SetTitleMatchMode, 2
CoordMode, Mouse, Screen
SetWinDelay, 50
#WinActivateForce

; 全局变量
global g_bStop := false
global g_WPSClass := "OpusApp"  ; WPS的窗口类

; Windows 消息常量
global WM_KEYDOWN := 0x100
global WM_KEYUP := 0x101
global WM_CHAR := 0x102
global WM_SETTEXT := 0x000C
global WM_PASTE := 0x302

; Windows API 常量
global SPI_SETFOREGROUNDLOCKTIMEOUT := 0x2000
global SPIF_SENDCHANGE := 2
global WS_EX_TOPMOST := 0x8
global HWND_TOPMOST := -1
global HWND_NOTOPMOST := -2
global SWP_NOMOVE := 0x2
global SWP_NOSIZE := 0x1

; === 辅助函数 ===
LockWindowSwitching() {
    DllCall("SystemParametersInfo", "UInt", SPI_SETFOREGROUNDLOCKTIMEOUT, "UInt", 0, "UInt", 0, "UInt", SPIF_SENDCHANGE)
}

UnlockWindowSwitching() {
    DllCall("SystemParametersInfo", "UInt", SPI_SETFOREGROUNDLOCKTIMEOUT, "UInt", 0, "UInt", 200000, "UInt", SPIF_SENDCHANGE)
}

SetWindowAlwaysOnTop(hwnd, onTop := true) {
    flag := onTop ? HWND_TOPMOST : HWND_NOTOPMOST
    return DllCall("SetWindowPos", "Ptr", hwnd, "Ptr", flag, "Int", 0, "Int", 0, "Int", 0, "Int", 0, "UInt", SWP_NOMOVE | SWP_NOSIZE)
}

DisableWindowActivation(hwnd) {
    WinSet, ExStyle, +0x80, ahk_id %hwnd%  ; 添加 WS_EX_TOOLWINDOW 样式
}

EnableWindowActivation(hwnd) {
    WinSet, ExStyle, -0x80, ahk_id %hwnd%  ; 移除 WS_EX_TOOLWINDOW 样式
}

ProcessPrintWindow(wpsHwnd) {
    if WinExist("Print supplier invoice journal") {
        WinGet, printHwnd, ID
        
        ; 先禁用WPS窗口的激活能力
        WinSet, ExStyle, +0x80, ahk_id %wpsHwnd%  ; 添加 WS_EX_TOOLWINDOW 样式
        Sleep, 500  ; 保持500ms等待时间
        
        ; 激活Print窗口
        WinActivate, ahk_id %printHwnd%
        WinWaitActive, ahk_id %printHwnd%, , 3
        if !ErrorLevel {
            ; 设置Print窗口始终置顶
            WinSet, AlwaysOnTop, On, ahk_id %printHwnd%
            Sleep, 500  ; 保持500ms等待时间
            
            ; 执行Print窗口的操作
            Send, ^l
            Sleep, 800
            
            MouseMove, 96, 20
            Sleep, 1000
            Click
            Sleep, 1000
            
            ; 发送Enter并等待
            Send, {Enter}
            Sleep, 800  ; 减少到800ms
            
            ; 等待Print窗口关闭
            WinWaitClose, ahk_id %printHwnd%, , 5
            Sleep, 500
            
            ; 快速恢复窗口状态
            WinSet, AlwaysOnTop, Off, ahk_id %printHwnd%
            WinSet, ExStyle, -0x80, ahk_id %wpsHwnd%  ; 移除 WS_EX_TOOLWINDOW 样式
            
            ; 返回WPS窗口
            WinActivate, ahk_id %wpsHwnd%
            WinWaitActive, ahk_id %wpsHwnd%, , 2
            Sleep, 300
            
            return true
        }
    }
    ShowToolTip("无法找到或激活Print窗口", 1500)
    return false
}

SendMessageToWindow(title, msg, wParam := 0, lParam := 0) {
    if WinExist(title) {
        SendMessage, %msg%, %wParam%, %lParam%
        return ErrorLevel
    }
    return false
}

SimulateKeyPress(hwnd, key) {
    PostMessage, WM_KEYDOWN, %key%, 0,, ahk_id %hwnd%
    Sleep, 50
    PostMessage, WM_KEYUP, %key%, 0,, ahk_id %hwnd%
    Sleep, 50
}

SafeActivateWindow(title) {
    if WinExist(title) {
        WinGet, hwnd, ID
        WinGet, state, MinMax
        if (state = -1)  ; 如果窗口最小化
            WinRestore, ahk_id %hwnd%
        
        ; 使用 PostMessage 来激活窗口
        PostMessage, 0x0086  ; WM_NCACTIVATE
        Sleep, 50
        return hwnd
    }
    return 0
}

WaitForWPSStable() {
    Sleep, 200
    WinWaitActive, ahk_class %g_WPSClass%, , 2
    Sleep, 100
}

; === 热键定义 ===
Escape::
    g_bStop := true
    ToolTip, 操作已取消, A_ScreenWidth//2, A_ScreenHeight//2
    SetTimer, RemoveToolTip, 2000
return

^!p::
    ; 初始化
    g_bStop := false
    Hotkey, Escape, On
    CoordMode, Mouse, Screen

    ; === 原始步骤完整保留 ===
    
    ; 第1步
    if (!g_bStop) {
        Send, ^r
        Sleep, 800
    }
    
    ; 第2步
    if (!g_bStop) {
        MouseMove, 94, 103
        Sleep, 800
        Click
    }
    
    ; 第4步
    if (!g_bStop) {
        Sleep, 700
        Send, ^c
    }
    
    ; 第5步
    if (!g_bStop) {
        Sleep, 800
        Send, ^s
        Sleep, 800
    }
    
    ; 激活Supplier窗口
    if (!g_bStop) {
        if (!ActivateWindow("Supplier invoice log")) {
            g_bStop := true
        }
    }
    
    ; 继续后续步骤
    if (!g_bStop) {
        MouseMove, 69, 88
        Sleep, 800
        Click
    }
    
    if (!g_bStop) {
        MouseMove, 238, 281
        Sleep, 800
        Click
    }
    
    if (!g_bStop) {
        Sleep, 700
        Send, ^v
        Sleep, 500
        Send, {Tab}
        Sleep, 1000
        Send, ^l
        Sleep, 1000
    }
    
    ; 更多步骤...
    if (!g_bStop) {
        MouseMove, 23, 252
        Sleep, 1000
        Click
    }
    
    if (!g_bStop) {
        MouseMove, 53, 273
        Sleep, 900
        Click
    }
    
    if (!g_bStop) {
        Sleep, 800
        Send, {Enter}
    }
    
    if (!g_bStop) {
        MouseMove, 29, 168
        Sleep, 800
        Click
    }
    
    if (!g_bStop) {
        MouseMove, 55, 216
        Sleep, 800
        Click
    }
    
if (!g_bStop) {
    MouseMove, 313, 308
        Sleep, 1000
        Click
        Sleep, 79
        Click
    }
    
    if (!g_bStop) {
        Sleep, 100
        Send, {Left}
        Sleep, 800
        Send, {Enter}
    }
    
    ; 等待WPS窗口并获取句柄
    if (!g_bStop) {
        WinWaitActive, Supplier invoice log-List.xlsx - WPS Office ahk_class %g_WPSClass%
        WinGet, wpsHwnd, ID, A
        Sleep, 800
    }
    
    ; 处理Print窗口
    if (!g_bStop) {
        if (!ProcessPrintWindow(wpsHwnd)) {
            g_bStop := true
            ShowToolTip("无法找到Print窗口", 1500)
        }
    }
    
    ; 激活Voucher窗口
    if (!g_bStop) {
        if (!ActivateWindow("Voucher list | Historical year 2022 July")) {
            g_bStop := true
        }
    }
    
    if (!g_bStop) {
        Sleep, 1000
        Send, ^l
        Sleep, 800
    }
    
    ; 最终步骤
    if (!g_bStop) {
        MouseMove, 22, 165
        Sleep, 1000
        Click
    }
    
    if (!g_bStop) {
        MouseMove, 56, 208
        Sleep, 100
        Click
    }
    
    if (!g_bStop) {
        MouseMove, 254, 328
        Sleep, 1000
        Click
        Sleep, 79
        Click
    }
    
    if (!g_bStop) {
        Sleep, 100
        Send, {Left}
        Sleep, 800
        Send, {Enter}
    }
    
    if (!g_bStop) {
        WinWaitActive, Voucher list-List.xlsx - WPS Office ahk_class %g_WPSClass%
        Sleep, 800
    }
    
    if (!g_bStop) {
        Send, {Alt down}
        Sleep, 100
        Send, 6
        Sleep, 100
        Send, f
        Sleep, 100
        Send, {Alt up}
    }
    
    ; 只有所有步骤完成才显示
    if (!g_bStop) {
        ShowToolTip("操作完成", 1500)
    }
    
    Hotkey, Escape, Off
return

^!m::
    ; 初始化
    g_bStop := false
    Hotkey, Escape, On
    CoordMode, Mouse, Screen

    ; === 完整保留所有原始步骤 ===
    
    ; 第2步
    if (!g_bStop) {
        MouseMove, 23, 252
        Sleep, 1000
        Click
    }
    
    ; 第4步
    if (!g_bStop) {
        MouseMove, 53, 273
        Sleep, 900
        Click
    }
    
    ; 第6步
    if (!g_bStop) {
        Sleep, 800
        Send, {Enter}
    }
    
    ; 第7步
    if (!g_bStop) {
        MouseMove, 29, 168
        Sleep, 800
        Click
    }
    
    ; 第9步
    if (!g_bStop) {
        MouseMove, 55, 216
        Sleep, 800
        Click
    }
    
if (!g_bStop) {
    MouseMove, 313, 308
        Sleep, 1000
        Click
        Sleep, 79
        Click
    }
    
    if (!g_bStop) {
        Sleep, 100
        Send, {Left}
        Sleep, 800
        Send, {Enter}
    }
    
    ; 等待WPS窗口并获取句柄
    if (!g_bStop) {
        WinWaitActive, Supplier invoice log-List.xlsx - WPS Office ahk_class %g_WPSClass%
        WinGet, wpsHwnd, ID, A
        Sleep, 800
    }
    
    ; 处理Print窗口
    if (!g_bStop) {
        if (!ProcessPrintWindow(wpsHwnd)) {
            g_bStop := true
            ShowToolTip("无法找到Print窗口", 1500)
        }
    }
    
    ; 激活Voucher窗口
    if (!g_bStop) {
        if (!ActivateWindow("Voucher list | Historical year 2022 July")) {
            g_bStop := true
        }
    }
    
    if (!g_bStop) {
        Sleep, 1000
        Send, ^l
        Sleep, 800
    }
    
    ; 后续步骤
    if (!g_bStop) {
        MouseMove, 22, 165
        Sleep, 1000
        Click
    }
    
    if (!g_bStop) {
        MouseMove, 56, 208
        Sleep, 100
        Click
    }
    
    if (!g_bStop) {
        MouseMove, 254, 328
        Sleep, 1000
        Click
        Sleep, 79
        Click
    }
    
    if (!g_bStop) {
        Sleep, 100
        Send, {Left}
        Sleep, 800
        Send, {Enter}
    }
    
    ; 等待窗口激活
    if (!g_bStop) {
        WinWaitActive, Voucher list-List.xlsx - WPS Office ahk_class %g_WPSClass%
        Sleep, 800
    }
    
    ; Alt组合键操作
    if (!g_bStop) {
        Send, {Alt down}
        Sleep, 100
        Send, 6
        Sleep, 100
        Send, f
        Sleep, 100
        Send, {Alt up}
    }
    
    ; 最后一步
    if (!g_bStop) {
        MouseMove, 550, 263
        Sleep, 200
        Click
    }
    
    ; 只有所有步骤完成才显示
    if (!g_bStop) {
        ShowToolTip("操作完成", 1500)
    }
    
    Hotkey, Escape, Off
return

; === 辅助函数 ===
ActivateWindow(title) {
    SetTitleMatchMode, 2
    if WinExist(title) {
        WinActivate
        WinWaitActive, %title%, , 3
        if !ErrorLevel {
            Sleep, 800
            return true
        }
    }
    
    if WinExist("ahk_class " . g_WPSClass) {
        WinActivate
        WinWaitActive, ahk_class %g_WPSClass%, , 3
        if !ErrorLevel {
            Sleep, 800
            return true
        }
    }
    
    MsgBox, 无法激活窗口: %title%
    return false
}

ShowToolTip(message, duration) {
    ToolTip, %message%, A_ScreenWidth//2, A_ScreenHeight//2
    SetTimer, RemoveToolTip, %duration%
}

RemoveToolTip:
    ToolTip
    SetTimer, RemoveToolTip, Off
return