SetTitleMatchMode(2) global startTime := 0 global running := false global statusCell := "B17" ; Set the status cell to B17 global finalLapTime := "" ; Declare this variable globally to avoid scope issues ; Hotkey Alt+W: Start/Stop Stopwatch !w:: { global running, startTime, finalLapTime ; Declare global variables if (!running) { startTime := A_TickCount ; Record start time running := true sendStatusToExcel("Stopwatch Started") } else { elapsedTime := (A_TickCount - startTime) / 1000 ; Calculate time in seconds running := false finalLapTime := Format("{:0.2f}", elapsedTime) sendStatusToExcel("Stopwatch Stopped - Time: " . finalLapTime) } } ; Hotkey Alt+E: Snap to Excel and paste into I21 !e:: { global finalLapTime ; Declare global variable if (finalLapTime) { sendToExcel("I21", finalLapTime) ; Paste the final time into cell I21 sendStatusToExcel("Pasted " . finalLapTime . " into I21") } else { sendStatusToExcel("No stopwatch time recorded yet!") } } ; Hotkey Alt+Q: Reset Stopwatch !q:: { global startTime, running, finalLapTime ; Declare global variables startTime := 0 running := false finalLapTime := "" sendStatusToExcel("Stopwatch Reset") } ; Function to send status messages to a specific Excel cell using COM sendStatusToExcel(statusMessage) { sendToExcel(statusCell, statusMessage) } ; Helper function for AutoClosing MsgBox in v2 AutoCloseMsgBox(text, timeout := 2000) { MsgBox(text, "OK", timeout) } ; Main function to send data to Excel sendToExcel(cellAddress, data) { try { ; Attempt to get an active instance of Excel, or create a new one if not found xlApp := ComObject("Excel.Application") ; Ensure Excel COM connection if !xlApp { AutoCloseMsgBox("Failed to connect to Excel. Ensure Excel is running.", 2000) return } ; Check if there is a workbook open if (xlApp.Workbooks.Count = 0) { AutoCloseMsgBox("No workbook is open in Excel.", 2000) return } ; Debug message for current worksheet xlSheet := xlApp.ActiveSheet AutoCloseMsgBox("Current worksheet: " xlSheet.Name, 2000) ; Attempt to access the specified cell try { xlRange := xlApp.Range[cellAddress] ; Access the specific cell if !xlRange { AutoCloseMsgBox("Cell " cellAddress " not found.", 2000) return } } catch { AutoCloseMsgBox("Error accessing cell: " cellAddress, 2000) return } ; Write data to the cell try { xlRange.Value := data AutoCloseMsgBox("Data '" data "' written to " cellAddress, 2000) } catch { AutoCloseMsgBox("Failed to write data to " cellAddress, 2000) } } catch { AutoCloseMsgBox("Error connecting to Excel COM Object.", 2000) } }