AHK open excel workbook and repair

286 Views Asked by At

hi can anyone help me with the below code, i have an excel sheet that i need to open and repair then save, i do this because it tends to break atlot. any help much appreciated

 ; Open the Excel document

xlApp := ComObjCreate("Excel.Application")    ; create a (new) instance of Excel
xlApp.Visible := true                         ; make Excel visible
xlApp := ComObjActive("Excel.Application")    ; make Excel active 

xlApp := xlApp.Workbooks.Open("C:\Users\Phill\Desktop\New Microsoft Excel Worksheet.xlsx", CorruptLoad := XlCorruptLoad.xlRepairFile)
xlApp := ""   ; clear the variable
return 
1

There are 1 best solutions below

0
On

There are two issues here:

  1. AHK doesn't support named parameters for COM
  2. AHK doesn't know what XlCorruptLoad.xlRepairFile means

Solutions:

  1. Based on the AHK documentation, use commas with nothing between to send the value in the proper position for CorruptLoad. According to the Microsoft documentation, it's the last of 15 parameters.
  2. The value for XlCorruptLoad.xlRepairFile is 1, so that's what you'd pass in for that parameter.

Here is the full (untested) code:

 ; Open the Excel document
xlRepairFile:= 1

xlApp := ComObjCreate("Excel.Application")    ; create a (new) instance of Excel
xlApp.Visible := true                         ; make Excel visible
xlApp := ComObjActive("Excel.Application")    ; make Excel active 

xlApp := xlApp.Workbooks.Open("C:\Users\Phill\Desktop\New Microsoft Excel Worksheet.xlsx", , , , , , , , , , , , , , xlRepairFile)
xlApp := ""   ; clear the variable
return