Excel.au3 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  1. #include-once
  2. #include "Array.au3"
  3. #include "ExcelConstants.au3"
  4. ; #INDEX# =======================================================================================================================
  5. ; Title .........: Microsoft Excel Function Library
  6. ; AutoIt Version : 3.3.14.5
  7. ; Language ......: English
  8. ; Description ...: A collection of functions for accessing and manipulating Microsoft Excel files
  9. ; Author(s) .....: SEO (Locodarwin), DaLiMan, Stanley Lim, MikeOsdx, MRDev, big_daddy, PsaltyDS, litlmike, water, spiff59, golfinhu, bowmore, GMX, Andreu, danwilli
  10. ; Resources .....:
  11. ; ===============================================================================================================================
  12. ; #CURRENT# =====================================================================================================================
  13. ; _Excel_Open
  14. ; _Excel_Close
  15. ; _Excel_BookAttach
  16. ; _Excel_BookClose
  17. ; _Excel_BookList
  18. ; _Excel_BookNew
  19. ; _Excel_BookOpen
  20. ; _Excel_BookOpenText
  21. ; _Excel_BookSave
  22. ; _Excel_BookSaveAs
  23. ; _Excel_ColumnToLetter
  24. ; _Excel_ColumnToNumber
  25. ; _Excel_ConvertFormula
  26. ; _Excel_Export
  27. ; _Excel_FilterGet
  28. ; _Excel_FilterSet
  29. ; _Excel_PictureAdd
  30. ; _Excel_Print
  31. ; _Excel_RangeCopyPaste
  32. ; _Excel_RangeDelete
  33. ; _Excel_RangeFind
  34. ; _Excel_RangeInsert
  35. ; _Excel_RangeLinkAddRemove
  36. ; _Excel_RangeRead
  37. ; _Excel_RangeReplace
  38. ; _Excel_RangeSort
  39. ; _Excel_RangeValidate
  40. ; _Excel_RangeWrite
  41. ; _Excel_SheetAdd
  42. ; _Excel_SheetCopyMove
  43. ; _Excel_SheetDelete
  44. ; _Excel_SheetList
  45. ; ===============================================================================================================================
  46. ; #INTERNAL_USE_ONLY#============================================================================================================
  47. ; __Excel_CloseOnQuit
  48. ; __Excel_COMErrFunc
  49. ; ===============================================================================================================================
  50. ; #FUNCTION# ====================================================================================================================
  51. ; Author ........: water
  52. ; Modified ......:
  53. ; ===============================================================================================================================
  54. Func _Excel_Open($bVisible = Default, $bDisplayAlerts = Default, $bScreenUpdating = Default, $bInteractive = Default, $bForceNew = Default)
  55. ; Error handler, automatic cleanup at end of function
  56. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  57. #forceref $oError
  58. Local $oExcel, $bApplCloseOnQuit = False
  59. If $bVisible = Default Then $bVisible = True
  60. If $bDisplayAlerts = Default Then $bDisplayAlerts = False
  61. If $bScreenUpdating = Default Then $bScreenUpdating = True
  62. If $bInteractive = Default Then $bInteractive = True
  63. If $bForceNew = Default Then $bForceNew = False
  64. If Not $bForceNew Then $oExcel = ObjGet("", "Excel.Application")
  65. If $bForceNew Or @error Then
  66. $oExcel = ObjCreate("Excel.Application")
  67. If @error Or Not IsObj($oExcel) Then Return SetError(1, @error, 0)
  68. $bApplCloseOnQuit = True
  69. EndIf
  70. __Excel_CloseOnQuit($oExcel, $bApplCloseOnQuit)
  71. $oExcel.Visible = $bVisible
  72. $oExcel.DisplayAlerts = $bDisplayAlerts
  73. $oExcel.ScreenUpdating = $bScreenUpdating
  74. $oExcel.Interactive = $bInteractive
  75. Return SetError(0, $bApplCloseOnQuit, $oExcel)
  76. EndFunc ;==>_Excel_Open
  77. ; #FUNCTION# ====================================================================================================================
  78. ; Author ........: water
  79. ; Modified ......:
  80. ; ===============================================================================================================================
  81. Func _Excel_Close(ByRef $oExcel, $bSaveChanges = Default, $bForceClose = Default)
  82. ; Error handler, automatic cleanup at end of function
  83. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  84. #forceref $oError
  85. If $bSaveChanges = Default Then $bSaveChanges = True
  86. If $bForceClose = Default Then $bForceClose = False
  87. If Not IsObj($oExcel) Or ObjName($oExcel, 1) <> "_Application" Then Return SetError(1, 0, 0)
  88. If $bSaveChanges Then
  89. For $oWorkbook In $oExcel.Workbooks
  90. If Not $oWorkbook.Saved Then
  91. $oWorkbook.Save()
  92. If @error Then Return SetError(3, @error, 0)
  93. EndIf
  94. Next
  95. EndIf
  96. If __Excel_CloseOnQuit($oExcel) Or $bForceClose Then
  97. $oExcel.Quit()
  98. If @error Then Return SetError(2, @error, 0)
  99. __Excel_CloseOnQuit($oExcel, False)
  100. $oExcel = 0
  101. EndIf
  102. Return 1
  103. EndFunc ;==>_Excel_Close
  104. ; #FUNCTION# ====================================================================================================================
  105. ; Author ........: Bob Anthony (big_daddy)
  106. ; Modified.......: water
  107. ; ===============================================================================================================================
  108. Func _Excel_BookAttach($sString, $sMode = Default, $oInstance = Default)
  109. ; Error handler, automatic cleanup at end of function
  110. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  111. #forceref $oError
  112. Local $oWorkbook, $iCount = 0, $sCLSID_Workbook = "{00020819-0000-0000-C000-000000000046}" ; Microsoft.Office.Interop.Excel.WorkbookClass
  113. If $sMode = Default Then $sMode = "FilePath"
  114. While True
  115. $oWorkbook = ObjGet("", $sCLSID_Workbook, $iCount + 1)
  116. If @error Then Return SetError(1, @error, 0)
  117. $iCount += 1
  118. If $oInstance <> Default And $oInstance <> $oWorkbook.Parent Then ContinueLoop
  119. Switch $sMode
  120. Case "filename"
  121. If $oWorkbook.Name = $sString Then Return $oWorkbook
  122. Case "filepath"
  123. If $oWorkbook.FullName = $sString Then Return $oWorkbook
  124. Case "title"
  125. If $oWorkbook.Application.Caption = $sString Then Return $oWorkbook
  126. Case Else
  127. Return SetError(2, 0, 0)
  128. EndSwitch
  129. WEnd
  130. EndFunc ;==>_Excel_BookAttach
  131. ; #FUNCTION# ====================================================================================================================
  132. ; Author ........: SEO <locodarwin at yahoo dot com>
  133. ; Modified.......: big_daddy, litlmike, water
  134. ; ===============================================================================================================================
  135. Func _Excel_BookClose(ByRef $oWorkbook, $bSave = Default)
  136. ; Error handler, automatic cleanup at end of function
  137. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  138. #forceref $oError
  139. If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
  140. If $bSave = Default Then $bSave = True
  141. If $bSave And Not $oWorkbook.Saved Then
  142. $oWorkbook.Save()
  143. If @error Then Return SetError(2, @error, 0)
  144. EndIf
  145. $oWorkbook.Close()
  146. If @error Then Return SetError(3, @error, 0)
  147. $oWorkbook = 0
  148. Return 1
  149. EndFunc ;==>_Excel_BookClose
  150. ; #FUNCTION# ====================================================================================================================
  151. ; Author ........: water
  152. ; Modified.......:
  153. ; ===============================================================================================================================
  154. Func _Excel_BookList($oExcel = Default)
  155. ; Error handler, automatic cleanup at end of function
  156. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  157. #forceref $oError
  158. Local $aBooks[1][3], $iIndex = 0
  159. If IsObj($oExcel) Then
  160. If ObjName($oExcel, 1) <> "_Application" Then Return SetError(1, 0, 0)
  161. Local $iTemp = $oExcel.Workbooks.Count
  162. ReDim $aBooks[$iTemp][3]
  163. For $iIndex = 0 To $iTemp - 1
  164. $aBooks[$iIndex][0] = $oExcel.Workbooks($iIndex + 1)
  165. $aBooks[$iIndex][1] = $oExcel.Workbooks($iIndex + 1).Name
  166. $aBooks[$iIndex][2] = $oExcel.Workbooks($iIndex + 1).Path
  167. Next
  168. Else
  169. If $oExcel <> Default Then Return SetError(1, 0, 0)
  170. Local $oWorkbook, $sCLSID_Workbook = "{00020819-0000-0000-C000-000000000046}"
  171. While True
  172. $oWorkbook = ObjGet("", $sCLSID_Workbook, $iIndex + 1)
  173. If @error Then ExitLoop
  174. ReDim $aBooks[$iIndex + 1][3]
  175. $aBooks[$iIndex][0] = $oWorkbook
  176. $aBooks[$iIndex][1] = $oWorkbook.Name
  177. $aBooks[$iIndex][2] = $oWorkbook.Path
  178. $iIndex += 1
  179. WEnd
  180. EndIf
  181. Return $aBooks
  182. EndFunc ;==>_Excel_BookList
  183. ; #FUNCTION# ====================================================================================================================
  184. ; Author ........: SEO <locodarwin at yahoo dot com>
  185. ; Modified.......: litlmike, water
  186. ; ===============================================================================================================================
  187. Func _Excel_BookNew($oExcel, $iSheets = Default)
  188. ; Error handler, automatic cleanup at end of function
  189. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  190. #forceref $oError
  191. If Not IsObj($oExcel) Or ObjName($oExcel, 1) <> "_Application" Then Return SetError(1, 0, 0)
  192. With $oExcel
  193. If $iSheets <> Default Then
  194. If $iSheets < 1 Or $iSheets > 255 Then Return SetError(4, 0, 0)
  195. Local $iSheetsBackup = .SheetsInNewWorkbook
  196. .SheetsInNewWorkbook = $iSheets
  197. If @error Then Return SetError(2, @error, 0)
  198. EndIf
  199. Local $oWorkbook = .Workbooks.Add()
  200. If @error Then
  201. Local $iError = @error
  202. If $iSheets <> Default Then .SheetsInNewWorkbook = $iSheetsBackup
  203. Return SetError(3, $iError, 0)
  204. EndIf
  205. If $iSheets <> Default Then .SheetsInNewWorkbook = $iSheetsBackup
  206. EndWith
  207. Return $oWorkbook
  208. EndFunc ;==>_Excel_BookNew
  209. ; #FUNCTION# ====================================================================================================================
  210. ; Author ........: SEO <locodarwin at yahoo dot com>
  211. ; Modified.......: litlmike, water, GMK, willichan
  212. ; ===============================================================================================================================
  213. Func _Excel_BookOpen($oExcel, $sFilePath, $bReadOnly = Default, $bVisible = Default, $sPassword = Default, $sWritePassword = Default, $bUpdateLinks = Default)
  214. ; Error handler, automatic cleanup at end of function
  215. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  216. #forceref $oError
  217. If Not IsObj($oExcel) Or ObjName($oExcel, 1) <> "_Application" Then Return SetError(1, @error, 0)
  218. If StringLeft($sFilePath, "HTTP") = 0 And Not FileExists($sFilePath) Then Return SetError(2, 0, 0)
  219. If $bReadOnly = Default Then $bReadOnly = False
  220. If $bVisible = Default Then $bVisible = True
  221. Local $oWorkbook = $oExcel.Workbooks.Open($sFilePath, $bUpdateLinks, $bReadOnly, Default, $sPassword, $sWritePassword)
  222. If @error Then Return SetError(3, @error, 0)
  223. Local $oWindow = $oExcel.Windows($oWorkbook.Name)
  224. If IsObj($oWindow) Then $oWindow.Visible = $bVisible
  225. ; If a read-write workbook was opened read-only then set @extended = 1
  226. If $bReadOnly = False And $oWorkbook.Readonly = True Then Return SetError(0, 1, $oWorkbook)
  227. Return $oWorkbook
  228. EndFunc ;==>_Excel_BookOpen
  229. ; #FUNCTION# ====================================================================================================================
  230. ; Author ........: water
  231. ; Modified.......:
  232. ; ===============================================================================================================================
  233. Func _Excel_BookOpenText($oExcel, $sFilePath, $iStartRow = Default, $iDataType = Default, $sTextQualifier = Default, $bConsecutiveDelimiter = Default, $sDelimiter = Default, $aFieldInfo = Default, $sDecimalSeparator = Default, $sThousandsSeparator = Default, $bTrailingMinusNumbers = Default, $iOrigin = Default)
  234. ; Error handler, automatic cleanup at end of function
  235. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  236. #forceref $oError
  237. Local $bTab = False, $bSemicolon = False, $bComma = False, $bSpace = False, $aDelimiter[1], $bOther = False, $sOtherChar
  238. If Not IsObj($oExcel) Or ObjName($oExcel, 1) <> "_Application" Then Return SetError(1, @error, 0)
  239. If StringLeft($sFilePath, "HTTP") = 0 And Not FileExists($sFilePath) Then Return SetError(2, 0, 0)
  240. If $iStartRow = Default Then $iStartRow = 1
  241. If $sTextQualifier = Default Then $sTextQualifier = $xlTextQualifierDoubleQuote
  242. If $bConsecutiveDelimiter = Default Then $bConsecutiveDelimiter = False
  243. If $sDelimiter = Default Then $sDelimiter = ","
  244. If $bTrailingMinusNumbers = Default Then $bTrailingMinusNumbers = True
  245. If StringInStr($sDelimiter, @TAB) > 0 Then $bTab = True
  246. If StringInStr($sDelimiter, ";") > 0 Then $bSemicolon = True
  247. If StringInStr($sDelimiter, ",") > 0 Then $bComma = True
  248. If StringInStr($sDelimiter, " ") > 0 Then $bSpace = True
  249. $aDelimiter = StringRegExp($sDelimiter, "[^;, " & @TAB & "]", $STR_REGEXPARRAYMATCH)
  250. If Not @error Then
  251. $sOtherChar = $aDelimiter[0]
  252. $bOther = True
  253. EndIf
  254. $oExcel.Workbooks.OpenText($sFilePath, $iOrigin, $iStartRow, $iDataType, $sTextQualifier, $bConsecutiveDelimiter, _
  255. $bTab, $bSemicolon, $bComma, $bSpace, $bOther, $sOtherChar, $aFieldInfo, Default, $sDecimalSeparator, $sThousandsSeparator, _
  256. $bTrailingMinusNumbers, False)
  257. If @error Then Return SetError(3, @error, 0)
  258. Return $oExcel.ActiveWorkbook ; Method OpenText doesn't return the Workbook object
  259. EndFunc ;==>_Excel_BookOpenText
  260. ; #FUNCTION# ====================================================================================================================
  261. ; Author ........: SEO <locodarwin at yahoo dot com>
  262. ; Modified.......: litlmike, water
  263. ; ===============================================================================================================================
  264. Func _Excel_BookSave($oWorkbook)
  265. ; Error handler, automatic cleanup at end of function
  266. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  267. #forceref $oError
  268. If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
  269. If Not $oWorkbook.Saved Then
  270. $oWorkbook.Save()
  271. If @error Then Return SetError(2, @error, 0)
  272. Return SetError(0, 1, 1)
  273. EndIf
  274. Return 1
  275. EndFunc ;==>_Excel_BookSave
  276. ; #FUNCTION# ====================================================================================================================
  277. ; Author ........: SEO <locodarwin at yahoo dot com>
  278. ; Modified.......: litlmike, water
  279. ; ===============================================================================================================================
  280. Func _Excel_BookSaveAs($oWorkbook, $sFilePath, $iFormat = Default, $bOverWrite = Default, $sPassword = Default, $sWritePassword = Default, $bReadOnlyRecommended = Default)
  281. ; Error handler, automatic cleanup at end of function
  282. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  283. #forceref $oError
  284. If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
  285. If $iFormat = Default Then
  286. $iFormat = $xlWorkbookDefault
  287. Else
  288. If Not IsNumber($iFormat) Then Return SetError(2, 0, 0)
  289. EndIf
  290. If $bOverWrite = Default Then $bOverWrite = False
  291. If $bReadOnlyRecommended = Default Then $bReadOnlyRecommended = False
  292. If FileExists($sFilePath) Then
  293. If Not $bOverWrite Then Return SetError(3, 0, 0)
  294. Local $iResult = FileDelete($sFilePath)
  295. If $iResult = 0 Then Return SetError(4, 0, 0)
  296. EndIf
  297. $oWorkbook.SaveAs($sFilePath, $iFormat, $sPassword, $sWritePassword, $bReadOnlyRecommended)
  298. If @error Then Return SetError(5, @error, 0)
  299. Return 1
  300. EndFunc ;==>_Excel_BookSaveAs
  301. ; #FUNCTION# ====================================================================================================================
  302. ; Name ..........: _Excel_ColumnToLetter
  303. ; Description ...: Converts the column number to letter(s).
  304. ; Syntax ........: _ExcelColumnToLetter($iColumn)
  305. ; Parameters ....: $iColumn - The column number which you want to turn into letter(s)
  306. ; Return values .: Success - Returns the column letter(s)
  307. ; Failure - Returns "" and sets @Error:
  308. ; Author(s): Spiff59
  309. ; Modified ......:
  310. ; ===============================================================================================================================
  311. Func _Excel_ColumnToLetter($iColumn)
  312. If Not StringRegExp($iColumn, "^[0-9]+$") Then Return SetError(1, 0, "")
  313. Local $sLetters, $iTemp
  314. While $iColumn
  315. $iTemp = Mod($iColumn, 26)
  316. If $iTemp = 0 Then $iTemp = 26
  317. $sLetters = Chr($iTemp + 64) & $sLetters
  318. $iColumn = ($iColumn - $iTemp) / 26
  319. WEnd
  320. Return $sLetters
  321. EndFunc ;==>_Excel_ColumnToLetter
  322. ; #FUNCTION# ====================================================================================================================
  323. ; Author ........: Golfinhu
  324. ; Modified ......:
  325. ; ===============================================================================================================================
  326. Func _Excel_ColumnToNumber($sColumn)
  327. $sColumn = StringUpper($sColumn)
  328. If Not StringRegExp($sColumn, "^[A-Z]+$") Then Return SetError(1, 0, 0)
  329. Local $sLetters = StringSplit($sColumn, "")
  330. Local $iNumber = 0
  331. Local $iLen = StringLen($sColumn)
  332. For $i = 1 To $sLetters[0]
  333. $iNumber += 26 ^ ($iLen - $i) * (Asc($sLetters[$i]) - 64)
  334. Next
  335. Return $iNumber
  336. EndFunc ;==>_Excel_ColumnToNumber
  337. ; #FUNCTION# ====================================================================================================================
  338. ; Author ........: water
  339. ; Modified ......:
  340. ; ===============================================================================================================================
  341. Func _Excel_ConvertFormula($oExcel, $sFormula, $iFromStyle, $iToStyle = Default, $iToAbsolute = Default, $vRelativeTo = Default)
  342. ; Error handler, automatic cleanup at end of function
  343. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  344. #forceref $oError
  345. If Not IsObj($oExcel) Or ObjName($oExcel, 1) <> "_Application" Then Return SetError(1, 0, "")
  346. If $vRelativeTo <> Default Then
  347. If Not IsObj($vRelativeTo) Then $vRelativeTo = $oExcel.Range($vRelativeTo)
  348. If @error Or Not IsObj($vRelativeTo) Then Return SetError(2, 0, "")
  349. EndIf
  350. Local $sConverted = $oExcel.ConvertFormula($sFormula, $iFromStyle, $iToStyle, $iToAbsolute, $vRelativeTo)
  351. Return $sConverted
  352. EndFunc ;==>_Excel_ConvertFormula
  353. ; #FUNCTION# ====================================================================================================
  354. ; Author ........: water
  355. ; Modified ......:
  356. ; ===============================================================================================================
  357. Func _Excel_Export($oExcel, $vObject, $sFileName, $iType = Default, $iQuality = Default, $bIncludeProperties = Default, $iFrom = Default, $iTo = Default, $bOpenAfterPublish = Default)
  358. ; Error handler, automatic cleanup at end of function
  359. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  360. #forceref $oError
  361. If Not IsObj($oExcel) Or ObjName($oExcel, 1) <> "_Application" Then Return SetError(1, 0, 0)
  362. If Not IsObj($vObject) Then $vObject = $oExcel.Range($vObject)
  363. If @error Or Not IsObj($vObject) Then Return SetError(2, @error, 0)
  364. If $sFileName = "" Then Return SetError(3, 0, 0)
  365. If $iType = Default Then $iType = $xlTypePDF
  366. If $iQuality = Default Then $iQuality = $xlQualityStandard
  367. If $bIncludeProperties = Default Then $bIncludeProperties = True
  368. If $bOpenAfterPublish = Default Then $bOpenAfterPublish = False
  369. $vObject.ExportAsFixedFormat($iType, $sFileName, $iQuality, $bIncludeProperties, Default, $iFrom, $iTo, $bOpenAfterPublish)
  370. If @error Then Return SetError(4, @error, 0)
  371. Return $vObject
  372. EndFunc ;==>_Excel_Export
  373. ; #FUNCTION# ====================================================================================================================
  374. ; Author ........: water
  375. ; Modified.......:
  376. ; ===============================================================================================================================
  377. Func _Excel_FilterGet($oWorkbook, $vWorksheet = Default)
  378. ; Error handler, automatic cleanup at end of function
  379. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  380. #forceref $oError
  381. If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
  382. If Not IsObj($vWorksheet) Then
  383. If $vWorksheet = Default Then
  384. $vWorksheet = $oWorkbook.ActiveSheet
  385. Else
  386. $vWorksheet = $oWorkbook.WorkSheets.Item($vWorksheet)
  387. EndIf
  388. If @error Or Not IsObj($vWorksheet) Then Return SetError(2, @error, 0)
  389. ElseIf ObjName($vWorksheet, 1) <> "_Worksheet" Then
  390. Return SetError(2, @error, 0)
  391. EndIf
  392. Local $iIndex = 0, $iRecords, $iItems = $vWorksheet.AutoFilter.Filters.Count
  393. If $iItems > 0 Then
  394. Local $aFilters[$iItems][7]
  395. For $oFilter In $vWorksheet.AutoFilter.Filters
  396. $aFilters[$iIndex][0] = $oFilter.On
  397. $aFilters[$iIndex][1] = $oFilter.Count
  398. $aFilters[$iIndex][2] = $oFilter.Criteria1
  399. If IsArray($oFilter.Criteria1) Then $aFilters[$iIndex][2] = _ArrayToString($aFilters[$iIndex][2])
  400. $aFilters[$iIndex][3] = $oFilter.Criteria2
  401. If IsArray($oFilter.Criteria2) Then $aFilters[$iIndex][3] = _ArrayToString($aFilters[$iIndex][3])
  402. $aFilters[$iIndex][4] = $oFilter.Operator
  403. $aFilters[$iIndex][5] = $oFilter.Parent.Range
  404. $iRecords = 0
  405. For $oArea In $oFilter.Parent.Range.SpecialCells($xlCellTypeVisible).Areas
  406. $iRecords = $iRecords + $oArea.Rows.Count
  407. Next
  408. $aFilters[$iIndex][6] = $iRecords
  409. $iIndex = $iIndex + 1
  410. Next
  411. Return $aFilters
  412. Else
  413. Return SetError(3, 0, "")
  414. EndIf
  415. EndFunc ;==>_Excel_FilterGet
  416. ; #FUNCTION# ====================================================================================================================
  417. ; Author ........: water
  418. ; Modified.......:
  419. ; ===============================================================================================================================
  420. Func _Excel_FilterSet($oWorkbook, $vWorksheet, $vRange, $iField, $sCriteria1 = Default, $iOperator = Default, $sCriteria2 = Default)
  421. ; Error handler, automatic cleanup at end of function
  422. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  423. #forceref $oError
  424. If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
  425. If Not IsObj($vWorksheet) Then
  426. If $vWorksheet = Default Then
  427. $vWorksheet = $oWorkbook.ActiveSheet
  428. Else
  429. $vWorksheet = $oWorkbook.WorkSheets.Item($vWorksheet)
  430. EndIf
  431. If @error Or Not IsObj($vWorksheet) Then Return SetError(2, @error, 0)
  432. ElseIf ObjName($vWorksheet, 1) <> "_Worksheet" Then
  433. Return SetError(2, @error, 0)
  434. EndIf
  435. If $vRange = Default Then
  436. $vRange = $vWorksheet.Usedrange
  437. ElseIf Not IsObj($vRange) Then
  438. $vRange = $vWorksheet.Range($vRange)
  439. If @error Or Not IsObj($vRange) Then Return SetError(3, @error, 0)
  440. EndIf
  441. If $iField <> 0 Then ; Set a new filter
  442. $vRange.AutoFilter($iField, $sCriteria1, $iOperator, $sCriteria2)
  443. If @error Then Return SetError(4, @error, 0)
  444. ; If no filters remain then AutoFiltermode is set off
  445. If $vWorksheet.Filtermode = False Then $vWorksheet.AutoFilterMode = False
  446. Else ; remove all filters
  447. $vWorksheet.AutoFilterMode = False
  448. EndIf
  449. Return 1
  450. EndFunc ;==>_Excel_FilterSet
  451. ; #FUNCTION# ====================================================================================================================
  452. ; Author ........: DanWilli
  453. ; Modified.......: water
  454. ; ===============================================================================================================================
  455. Func _Excel_PictureAdd($oWorkbook, $vWorksheet, $sFile, $vRangeOrLeft, $iTop = Default, $iWidth = Default, $iHeight = Default, $bKeepRatio = True)
  456. ; Error handler, automatic cleanup at end of function
  457. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  458. #forceref $oError
  459. Local $oReturn, $iPosLeft, $iPosTop
  460. If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
  461. If Not FileExists($sFile) Then Return SetError(5, 0, 0)
  462. If Not IsObj($vWorksheet) Then
  463. If $vWorksheet = Default Then
  464. $vWorksheet = $oWorkbook.ActiveSheet
  465. Else
  466. $vWorksheet = $oWorkbook.WorkSheets.Item($vWorksheet)
  467. EndIf
  468. If @error Or Not IsObj($vWorksheet) Then Return SetError(2, @error, 0)
  469. ElseIf ObjName($vWorksheet, 1) <> "_Worksheet" Then
  470. Return SetError(2, @error, 0)
  471. EndIf
  472. If IsNumber($vRangeOrLeft) Then
  473. $iPosLeft = $vRangeOrLeft
  474. $iPosTop = $iTop
  475. Else
  476. If Not IsObj($vRangeOrLeft) Then
  477. $vRangeOrLeft = $vWorksheet.Range($vRangeOrLeft)
  478. If @error Or Not IsObj($vRangeOrLeft) Then Return SetError(3, @error, 0)
  479. EndIf
  480. $iPosLeft = $vRangeOrLeft.Left
  481. $iPosTop = $vRangeOrLeft.Top
  482. EndIf
  483. If IsNumber($vRangeOrLeft) Or ($vRangeOrLeft.Columns.Count = 1 And $vRangeOrLeft.Rows.Count = 1) Then
  484. If $iWidth = Default And $iHeight = Default Then
  485. $oReturn = $vWorksheet.Shapes.AddPicture($sFile, -1, -1, $iPosLeft, $iPosTop, 0, 0)
  486. If @error Then Return SetError(4, @error, 0)
  487. $oReturn.Scalewidth(1, -1, 0)
  488. $oReturn.Scaleheight(1, -1, 0)
  489. ElseIf $iWidth = Default Then
  490. $oReturn = $vWorksheet.Shapes.AddPicture($sFile, -1, -1, $iPosLeft, $iPosTop, 0, 0)
  491. If @error Then Return SetError(4, @error, 0)
  492. $oReturn.Visible = 0
  493. $oReturn.Scalewidth(1, -1, 0)
  494. $oReturn.Scaleheight(1, -1, 0)
  495. $oReturn.Scalewidth($iHeight / $oReturn.Height, -1, 0)
  496. $oReturn.Scaleheight($iHeight / $oReturn.Height, -1, 0)
  497. $oReturn.Visible = 1
  498. ElseIf $iHeight = Default Then
  499. $oReturn = $vWorksheet.Shapes.AddPicture($sFile, -1, -1, $iPosLeft, $iPosTop, 0, 0)
  500. If @error Then Return SetError(4, @error, 0)
  501. $oReturn.Visible = 0
  502. $oReturn.Scalewidth(1, -1, 0)
  503. $oReturn.Scaleheight(1, -1, 0)
  504. $oReturn.Scaleheight($iWidth / $oReturn.Width, -1, 0)
  505. $oReturn.Scalewidth($iWidth / $oReturn.Width, -1, 0)
  506. $oReturn.Visible = 1
  507. Else
  508. $oReturn = $vWorksheet.Shapes.AddPicture($sFile, -1, -1, $iPosLeft, $iPosTop, $iWidth, $iHeight)
  509. If @error Then Return SetError(4, @error, 0)
  510. EndIf
  511. Else
  512. If $bKeepRatio = True Then
  513. $oReturn = $vWorksheet.Shapes.AddPicture($sFile, -1, -1, $iPosLeft, $iPosTop, 0, 0)
  514. If @error Then Return SetError(4, @error, 0)
  515. $oReturn.Visible = 0
  516. $oReturn.Scalewidth(1, -1, 0)
  517. $oReturn.Scaleheight(1, -1, 0)
  518. Local $iRw = $vRangeOrLeft.Width / $oReturn.Width
  519. Local $iRh = $vRangeOrLeft.Height / $oReturn.Height
  520. If $iRw < $iRh Then
  521. $oReturn.Scaleheight($iRw, -1, 0)
  522. $oReturn.Scalewidth($iRw, -1, 0)
  523. Else
  524. $oReturn.Scaleheight($iRh, -1, 0)
  525. $oReturn.Scalewidth($iRh, -1, 0)
  526. EndIf
  527. $oReturn.Visible = 1
  528. Else
  529. $oReturn = $vWorksheet.Shapes.AddPicture($sFile, -1, -1, $iPosLeft, $iPosTop, $vRangeOrLeft.Width, $vRangeOrLeft.Height)
  530. If @error Then Return SetError(4, @error, 0)
  531. EndIf
  532. EndIf
  533. Return $oReturn
  534. EndFunc ;==>_Excel_PictureAdd
  535. ; #FUNCTION# ====================================================================================================
  536. ; Author ........: water
  537. ; Modified ......:
  538. ; ===============================================================================================================
  539. Func _Excel_Print($oExcel, $vObject, $iCopies = Default, $sPrinter = Default, $bPreview = Default, $iFrom = Default, $iTo = Default, $bPrintToFile = Default, $bCollate = Default, $sPrToFileName = "")
  540. ; Error handler, automatic cleanup at end of function
  541. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  542. #forceref $oError
  543. If Not IsObj($oExcel) Or ObjName($oExcel, 1) <> "_Application" Then Return SetError(1, 0, 0)
  544. If IsString($vObject) Then $vObject = $oExcel.Range($vObject)
  545. If @error Or Not IsObj($vObject) Then Return SetError(2, @error, 0)
  546. $vObject.PrintOut($iFrom, $iTo, $iCopies, $bPreview, $sPrinter, $bPrintToFile, $bCollate, $sPrToFileName)
  547. If @error Then Return SetError(3, @error, 0)
  548. Return $vObject
  549. EndFunc ;==>_Excel_Print
  550. ; #FUNCTION# ====================================================================================================================
  551. ; Author ........: water
  552. ; Modified.......:
  553. ; ===============================================================================================================================
  554. Func _Excel_RangeCopyPaste($oWorksheet, $vSourceRange, $vTargetRange = Default, $bCut = Default, $iPaste = Default, $iOperation = Default, $bSkipBlanks = Default, $bTranspose = Default)
  555. ; Error handler, automatic cleanup at end of function
  556. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  557. #forceref $oError
  558. If Not IsObj($oWorksheet) Or ObjName($oWorksheet, 1) <> "_Worksheet" Then Return SetError(1, 0, 0)
  559. If $bCut = Default Then $bCut = False
  560. If $vSourceRange = Default And $vTargetRange = Default Then Return SetError(7, 0, 0)
  561. If Not IsObj($vSourceRange) And $vSourceRange <> Default Then
  562. $vSourceRange = $oWorksheet.Range($vSourceRange)
  563. If @error Then Return SetError(2, @error, 0)
  564. EndIf
  565. If Not IsObj($vTargetRange) And $vTargetRange <> Default Then
  566. $vTargetRange = $oWorksheet.Range($vTargetRange)
  567. If @error Then Return SetError(3, @error, 0)
  568. EndIf
  569. If $vSourceRange = Default Then ; Paste from the clipboard
  570. If $bSkipBlanks = Default Then $bSkipBlanks = False
  571. If $bTranspose = Default Then $bTranspose = False
  572. $vTargetRange.PasteSpecial($iPaste, $iOperation, $bSkipBlanks, $bTranspose)
  573. If @error Then Return SetError(4, @error, 0)
  574. Else
  575. If $bCut Then
  576. $vSourceRange.Cut($vTargetRange)
  577. If @error Then Return SetError(5, @error, 0)
  578. Else
  579. $vSourceRange.Copy($vTargetRange)
  580. If @error Then Return SetError(6, @error, 0)
  581. EndIf
  582. EndIf
  583. If $vTargetRange <> Default Then
  584. Return $vTargetRange
  585. Else
  586. Return 1
  587. EndIf
  588. EndFunc ;==>_Excel_RangeCopyPaste
  589. ; #FUNCTION# ====================================================================================================================
  590. ; Author ........: water
  591. ; Modified.......:
  592. ; ===============================================================================================================================
  593. Func _Excel_RangeDelete($oWorksheet, $vRange, $iShift = Default, $iEntireRowCol = Default)
  594. ; Error handler, automatic cleanup at end of function
  595. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  596. #forceref $oError
  597. If Not IsObj($oWorksheet) Or ObjName($oWorksheet, 1) <> "_Worksheet" Then Return SetError(1, 0, 0)
  598. If Not IsObj($vRange) Then
  599. $vRange = $oWorksheet.Range($vRange)
  600. If @error Then Return SetError(2, @error, 0)
  601. EndIf
  602. If $iEntireRowCol = 1 Then
  603. $vRange.EntireRow.Delete($iShift)
  604. ElseIf $iEntireRowCol = 2 Then
  605. $vRange.EntireColumn.Delete($iShift)
  606. Else
  607. $vRange.Delete($iShift)
  608. EndIf
  609. If @error Then Return SetError(3, @error, 0)
  610. Return 1
  611. EndFunc ;==>_Excel_RangeDelete
  612. ; #FUNCTION# ====================================================================================================================
  613. ; Author ........: water
  614. ; Modified.......:
  615. ; ===============================================================================================================================
  616. Func _Excel_RangeFind($oWorkbook, $sSearch, $vRange = Default, $iLookIn = Default, $iLookAt = Default, $bMatchcase = Default)
  617. ; Error handler, automatic cleanup at end of function
  618. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  619. #forceref $oError
  620. If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
  621. If StringStripWS($sSearch, BitOR($STR_STRIPLEADING, $STR_STRIPTRAILING)) = "" Then Return SetError(2, 0, 0)
  622. If $iLookIn = Default Then $iLookIn = $xlValues
  623. If $iLookAt = Default Then $iLookAt = $xlPart
  624. If $bMatchcase = Default Then $bMatchcase = False
  625. Local $oMatch, $sFirst = "", $bSearchWorkbook = False, $oSheet
  626. If $vRange = Default Then
  627. $bSearchWorkbook = True
  628. $oSheet = $oWorkbook.Sheets(1)
  629. $vRange = $oSheet.UsedRange
  630. ElseIf IsString($vRange) Then
  631. $vRange = $oWorkbook.Activesheet.Range($vRange)
  632. If @error Then Return SetError(3, @error, 0)
  633. EndIf
  634. Local $aResult[100][6], $iIndex = 0, $iIndexSheets = 1, $oTemp
  635. While 1
  636. $oMatch = $vRange.Find($sSearch, Default, $iLookIn, $iLookAt, Default, Default, $bMatchcase)
  637. If @error Then Return SetError(4, @error, 0)
  638. If IsObj($oMatch) Then
  639. $sFirst = $oMatch.Address
  640. While 1
  641. $aResult[$iIndex][0] = $oMatch.Worksheet.Name
  642. $oTemp = $oMatch.Name
  643. If Not @error Then $aResult[$iIndex][1] = $oTemp.Name
  644. $aResult[$iIndex][2] = $oMatch.Address
  645. $aResult[$iIndex][3] = $oMatch.Value
  646. $aResult[$iIndex][4] = $oMatch.Formula
  647. $oTemp = $oMatch.Comment
  648. If IsObj($oTemp) Then $aResult[$iIndex][5] = $oTemp.Text
  649. $iIndex = $iIndex + 1
  650. If Mod($iIndex, 100) = 0 Then ReDim $aResult[UBound($aResult, 1) + 100][6]
  651. $oMatch = $vRange.Findnext($oMatch)
  652. If Not IsObj($oMatch) Or $sFirst = $oMatch.Address Then ExitLoop
  653. WEnd
  654. EndIf
  655. If Not $bSearchWorkbook Then ExitLoop
  656. $iIndexSheets = $iIndexSheets + 1
  657. $sFirst = ""
  658. $oSheet = $oWorkbook.Sheets($iIndexSheets)
  659. If @error Then ExitLoop
  660. $vRange = $oSheet.UsedRange
  661. WEnd
  662. ReDim $aResult[$iIndex][6]
  663. Return $aResult
  664. EndFunc ;==>_Excel_RangeFind
  665. ; #FUNCTION# ====================================================================================================================
  666. ; Author ........: water
  667. ; Modified.......:
  668. ; ===============================================================================================================================
  669. Func _Excel_RangeInsert($oWorksheet, $vRange, $iShift = Default, $iCopyOrigin = Default)
  670. ; Error handler, automatic cleanup at end of function
  671. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  672. #forceref $oError
  673. If Not IsObj($oWorksheet) Or ObjName($oWorksheet, 1) <> "_Worksheet" Then Return SetError(1, 0, 0)
  674. If Not IsObj($vRange) Then
  675. $vRange = $oWorksheet.Range($vRange)
  676. If @error Then Return SetError(2, @error, 0)
  677. EndIf
  678. $vRange.Insert($iShift, $iCopyOrigin)
  679. If @error Then Return SetError(3, @error, 0)
  680. Return $vRange
  681. EndFunc ;==>_Excel_RangeInsert
  682. ; #FUNCTION# ====================================================================================================================
  683. ; Author ........: water
  684. ; Modified ......: Added parameter $sTextToDisplay
  685. ; ===============================================================================================================================
  686. Func _Excel_RangeLinkAddRemove($oWorkbook, $vWorksheet, $vRange, $sAddress, $sSubAddress = Default, $sScreenTip = Default, $sTextToDisplay = Default)
  687. ; Error handler, automatic cleanup at end of function
  688. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  689. #forceref $oError
  690. Local $oLink
  691. If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
  692. If Not IsObj($vWorksheet) Then
  693. If $vWorksheet = Default Then
  694. $vWorksheet = $oWorkbook.ActiveSheet
  695. Else
  696. $vWorksheet = $oWorkbook.WorkSheets.Item($vWorksheet)
  697. EndIf
  698. If @error Or Not IsObj($vWorksheet) Then Return SetError(2, @error, 0)
  699. ElseIf ObjName($vWorksheet, 1) <> "_Worksheet" Then
  700. Return SetError(2, @error, 0)
  701. EndIf
  702. If Not IsObj($vRange) Then
  703. $vRange = $vWorksheet.Range($vRange)
  704. If @error Or Not IsObj($vRange) Then Return SetError(3, @error, 0)
  705. EndIf
  706. If $sAddress = "" Then
  707. $vRange.Hyperlinks.Delete()
  708. If @error Then Return SetError(4, @error, 0)
  709. Return 1
  710. Else
  711. $oLink = $vWorksheet.Hyperlinks.Add($vRange, $sAddress, $sSubAddress, $sScreenTip, $sTextToDisplay)
  712. If @error Then Return SetError(4, @error, 0)
  713. Return $oLink
  714. EndIf
  715. EndFunc ;==>_Excel_RangeLinkAddRemove
  716. ; #FUNCTION# ====================================================================================================================
  717. ; Author ........: SEO <locodarwin at yahoo dot com>
  718. ; Modified.......: litlmike, water, GMK
  719. ; ===============================================================================================================================
  720. Func _Excel_RangeRead($oWorkbook, $vWorksheet = Default, $vRange = Default, $iReturn = Default, $bForceFunc = Default)
  721. ; Error handler, automatic cleanup at end of function
  722. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  723. #forceref $oError
  724. If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
  725. If Not IsObj($vWorksheet) Then
  726. If $vWorksheet = Default Then
  727. $vWorksheet = $oWorkbook.ActiveSheet
  728. Else
  729. $vWorksheet = $oWorkbook.WorkSheets.Item($vWorksheet)
  730. EndIf
  731. If @error Or Not IsObj($vWorksheet) Then Return SetError(2, @error, 0)
  732. ElseIf ObjName($vWorksheet, 1) <> "_Worksheet" Then
  733. Return SetError(2, @error, 0)
  734. EndIf
  735. If $vRange = Default Then
  736. $vRange = $vWorksheet.Usedrange
  737. ElseIf Not IsObj($vRange) Then
  738. $vRange = $vWorksheet.Range($vRange)
  739. If @error Or Not IsObj($vRange) Then Return SetError(3, @error, 0)
  740. EndIf
  741. If $iReturn = Default Then
  742. $iReturn = 1
  743. ElseIf $iReturn < 1 Or $iReturn > 4 Then
  744. Return SetError(4, 0, 0)
  745. EndIf
  746. If $bForceFunc = Default Then $bForceFunc = False
  747. Local $vResult, $iCellCount = $vRange.Columns.Count * $vRange.Rows.Count
  748. If $iReturn = 3 And $iCellCount > 1 Then Return SetError(8, @error, 0)
  749. ; The max number of elements in an AutoIt array is limited to 2^24 = 16,777,216
  750. If $iCellCount > 16777216 Then Return SetError(6, 0, 0)
  751. ; Transpose has an undocumented limit on the number of cells or rows it can transpose. This limit increases with the Excel version
  752. ; Limits:
  753. ; Excel 97 - 5461 cells
  754. ; Excel 2000 - 5461 cells
  755. ; Excel 2003 - ?
  756. ; Excel 2007 - 65535 cells
  757. ; Excel 2010 - ?
  758. ; Excel 2013 - ?
  759. If $iCellCount > 65535 Then $bForceFunc = True
  760. If $bForceFunc Then
  761. Switch $iReturn
  762. Case 1
  763. $vResult = $vRange.Value
  764. Case 2
  765. $vResult = $vRange.Formula
  766. Case 3
  767. $vResult = $vRange.Text
  768. Case Else
  769. $vResult = $vRange.Value2
  770. EndSwitch
  771. If @error Then Return SetError(7, @error, 0)
  772. If $iCellCount > 1 Then _ArrayTranspose($vResult)
  773. Else
  774. Local $oExcel = $oWorkbook.Parent
  775. Switch $iReturn
  776. Case 1
  777. $vResult = $oExcel.Transpose($vRange.Value)
  778. Case 2
  779. $vResult = $oExcel.Transpose($vRange.Formula)
  780. Case 3
  781. $vResult = $oExcel.Transpose($vRange.Text)
  782. Case Else
  783. $vResult = $oExcel.Transpose($vRange.Value2)
  784. EndSwitch
  785. If @error Then Return SetError(5, @error, 0)
  786. EndIf
  787. Return $vResult
  788. EndFunc ;==>_Excel_RangeRead
  789. ; #FUNCTION# ====================================================================================================================
  790. ; Author ........: water
  791. ; Modified.......:
  792. ; ===============================================================================================================================
  793. Func _Excel_RangeReplace($oWorkbook, $vWorksheet, $vRange, $sSearch, $sReplace, $iLookAt = Default, $bMatchcase = Default)
  794. ; Error handler, automatic cleanup at end of function
  795. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  796. #forceref $oError
  797. If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
  798. If Not IsObj($vWorksheet) Then
  799. If $vWorksheet = Default Then
  800. $vWorksheet = $oWorkbook.ActiveSheet
  801. Else
  802. $vWorksheet = $oWorkbook.WorkSheets.Item($vWorksheet)
  803. EndIf
  804. If @error Or Not IsObj($vWorksheet) Then Return SetError(2, @error, 0)
  805. ElseIf ObjName($vWorksheet, 1) <> "_Worksheet" Then
  806. Return SetError(2, @error, 0)
  807. EndIf
  808. If StringStripWS($sSearch, BitOR($STR_STRIPLEADING, $STR_STRIPTRAILING)) = "" Then Return SetError(3, 0, 0)
  809. If $vRange = Default Then
  810. $vRange = $vWorksheet.Usedrange
  811. ElseIf Not IsObj($vRange) Then
  812. $vRange = $vWorksheet.Range($vRange)
  813. If @error Or Not IsObj($vRange) Then Return SetError(4, @error, 0)
  814. EndIf
  815. If $iLookAt = Default Then $iLookAt = $xlPart
  816. If $bMatchcase = Default Then $bMatchcase = False
  817. Local $bReplace
  818. $bReplace = $vRange.Replace($sSearch, $sReplace, $iLookAt, Default, $bMatchcase)
  819. If @error Then Return SetError(5, @error, 0)
  820. Return SetError(0, $bReplace, $vRange)
  821. EndFunc ;==>_Excel_RangeReplace
  822. ; #FUNCTION# ====================================================================================================================
  823. ; Author ........: water
  824. ; Modified.......:
  825. ; ===============================================================================================================================
  826. Func _Excel_RangeSort($oWorkbook, $vWorksheet, $vRange, $vKey1, $iOrder1 = Default, $iSortText = Default, $iHeader = Default, _
  827. $bMatchcase = Default, $iOrientation = Default, $vKey2 = Default, $iOrder2 = Default, $vKey3 = Default, $iOrder3 = Default)
  828. ; Error handler, automatic cleanup at end of function
  829. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  830. #forceref $oError
  831. If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
  832. If Not IsObj($vWorksheet) Then
  833. If $vWorksheet = Default Then
  834. $vWorksheet = $oWorkbook.ActiveSheet
  835. Else
  836. $vWorksheet = $oWorkbook.WorkSheets.Item($vWorksheet)
  837. EndIf
  838. If @error Or Not IsObj($vWorksheet) Then Return SetError(2, @error, 0)
  839. ElseIf ObjName($vWorksheet, 1) <> "_Worksheet" Then
  840. Return SetError(2, @error, 0)
  841. EndIf
  842. If $vRange = Default Then
  843. $vRange = $vWorksheet.Usedrange
  844. ElseIf Not IsObj($vRange) Then
  845. $vRange = $vWorksheet.Range($vRange)
  846. If @error Or Not IsObj($vRange) Then Return SetError(3, @error, 0)
  847. EndIf
  848. $vKey1 = $vWorksheet.Range($vKey1)
  849. If @error Or Not IsObj($vKey1) Then Return SetError(4, @error, 0)
  850. If $vKey2 <> Default Then
  851. $vKey2 = $vWorksheet.Range($vKey2)
  852. If @error Or Not IsObj($vKey2) Then Return SetError(5, @error, 0)
  853. EndIf
  854. If $vKey3 <> Default Then
  855. $vKey3 = $vWorksheet.Range($vKey3)
  856. If @error Or Not IsObj($vKey3) Then Return SetError(6, @error, 0)
  857. EndIf
  858. If $iHeader = Default Then $iHeader = $xlNo
  859. If $bMatchcase = Default Then $bMatchcase = False
  860. If $iOrientation = Default Then $iOrientation = $xlSortColumns
  861. If $iOrder1 = Default Then $iOrder1 = $xlAscending
  862. If $iSortText = Default Then $iSortText = $xlSortNormal
  863. If $iOrder2 = Default Then $iOrder2 = $xlAscending
  864. If $iOrder3 = Default Then $iOrder3 = $xlAscending
  865. If Int($oWorkbook.Parent.Version) < 112 Then ; Use Sort method for Excel 2003 and older
  866. $vRange.Sort($vKey1, $iOrder1, $vKey2, Default, $iOrder2, $vKey3, $iOrder3, $iHeader, Default, $bMatchcase, $iOrientation, Default, $iSortText, $iSortText, $iSortText)
  867. Else
  868. ; http://www.autoitscript.com/forum/topic/136672-excel-multiple-column-sort/?hl=%2Bexcel+%2Bsort+%2Bcolumns#entry956163
  869. ; http://msdn.microsoft.com/en-us/library/ff839572(v=office.14).aspx
  870. $vWorksheet.Sort.SortFields.Clear
  871. $vWorksheet.Sort.SortFields.Add($vKey1, $xlSortOnValues, $iOrder1)
  872. If $vKey2 <> Default Then $vWorksheet.Sort.SortFields.Add($vKey2, $xlSortOnValues, $iOrder2)
  873. If $vKey3 <> Default Then $vWorksheet.Sort.SortFields.Add($vKey3, $xlSortOnValues, $iOrder3)
  874. $vWorksheet.Sort.SetRange($vRange)
  875. $vWorksheet.Sort.Header = $iHeader
  876. $vWorksheet.Sort.MatchCase = $bMatchcase
  877. $vWorksheet.Sort.Orientation = $iOrientation
  878. $vWorksheet.Sort.Apply
  879. EndIf
  880. If @error Then Return SetError(7, @error, 0)
  881. Return $vRange
  882. EndFunc ;==>_Excel_RangeSort
  883. ; #FUNCTION# ====================================================================================================================
  884. ; Author ........: water
  885. ; Modified.......:
  886. ; ===============================================================================================================================
  887. Func _Excel_RangeValidate($oWorkbook, $vWorksheet, $vRange, $iType, $sFormula1, $iOperator = Default, $sFormula2 = Default, $bIgnoreBlank = Default, $iAlertStyle = Default, $sErrorMessage = Default, $sInputMessage = Default)
  888. ; Error handler, automatic cleanup at end of function
  889. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  890. #forceref $oError
  891. If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
  892. If Not IsObj($vWorksheet) Then
  893. If $vWorksheet = Default Then
  894. $vWorksheet = $oWorkbook.ActiveSheet
  895. Else
  896. $vWorksheet = $oWorkbook.WorkSheets.Item($vWorksheet)
  897. EndIf
  898. If @error Or Not IsObj($vWorksheet) Then Return SetError(2, @error, 0)
  899. ElseIf ObjName($vWorksheet, 1) <> "_Worksheet" Then
  900. Return SetError(2, @error, 0)
  901. EndIf
  902. If $vRange = Default Then
  903. $vRange = $vWorksheet.Usedrange
  904. ElseIf Not IsObj($vRange) Then
  905. $vRange = $vWorksheet.Range($vRange)
  906. If @error Or Not IsObj($vRange) Then Return SetError(3, @error, 0)
  907. EndIf
  908. If $bIgnoreBlank = Default Then $bIgnoreBlank = True
  909. If $iAlertStyle = Default Then $iAlertStyle = $xlValidAlertStop
  910. $vRange.Validation.Delete() ; delete existing validation before adding a new one
  911. $vRange.Validation.Add($iType, $iAlertStyle, $iOperator, $sFormula1, $sFormula2)
  912. If @error Then Return SetError(4, @error, 0)
  913. $vRange.Validation.IgnoreBlank = $bIgnoreBlank
  914. If $sInputMessage <> Default Then
  915. $vRange.Validation.InputMessage = $sInputMessage
  916. $vRange.Validation.ShowInput = True
  917. EndIf
  918. If $sErrorMessage <> Default Then
  919. $vRange.Validation.ErrorMessage = $sErrorMessage
  920. $vRange.Validation.ShowError = True
  921. EndIf
  922. Return $vRange
  923. EndFunc ;==>_Excel_RangeValidate
  924. ; #FUNCTION# ====================================================================================================================
  925. ; Author ........: SEO <locodarwin at yahoo dot com>
  926. ; Modified.......: litlmike, PsaltyDS, Golfinhu, water
  927. ; ===============================================================================================================================
  928. Func _Excel_RangeWrite($oWorkbook, $vWorksheet, $vValue, $vRange = Default, $bValue = Default, $bForceFunc = Default)
  929. ; Error handler, automatic cleanup at end of function
  930. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  931. #forceref $oError
  932. If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
  933. If Not IsObj($vWorksheet) Then
  934. If $vWorksheet = Default Then
  935. $vWorksheet = $oWorkbook.ActiveSheet
  936. Else
  937. $vWorksheet = $oWorkbook.WorkSheets.Item($vWorksheet)
  938. EndIf
  939. If @error Or Not IsObj($vWorksheet) Then Return SetError(2, @error, 0)
  940. ElseIf ObjName($vWorksheet, 1) <> "_Worksheet" Then
  941. Return SetError(2, @error, 0)
  942. EndIf
  943. If $vRange = Default Then $vRange = "A1"
  944. If $bValue = Default Then $bValue = True
  945. If $bForceFunc = Default Then $bForceFunc = False
  946. If Not IsObj($vRange) Then
  947. $vRange = $vWorksheet.Range($vRange)
  948. If @error Or Not IsObj($vRange) Then Return SetError(3, @error, 0)
  949. EndIf
  950. If Not IsArray($vValue) Then
  951. If $bValue Then
  952. $vRange.Value = $vValue
  953. Else
  954. $vRange.Formula = $vValue
  955. EndIf
  956. If @error Then Return SetError(4, @error, 0)
  957. Else
  958. If $vRange.Columns.Count = 1 And $vRange.Rows.Count = 1 Then
  959. If UBound($vValue, 0) = 1 Then
  960. $vRange = $vRange.Resize(UBound($vValue, 1), 1)
  961. Else
  962. $vRange = $vRange.Resize(UBound($vValue, 1), UBound($vValue, 2))
  963. EndIf
  964. EndIf
  965. ; ==========================
  966. ; Transpose has an undocument limit on the number of cells or rows it can transpose. This limit increases with the Excel version
  967. ; Limits:
  968. ; Excel 97 - 5461 cells
  969. ; Excel 2000 - 5461 cells
  970. ; Excel 2003 - ?
  971. ; Excel 2007 - 65536 rows ?
  972. ; Excel 2010 - ?
  973. ; Example: If $oExcel.Version = 14 And $vRange.Columns.Count * $vRange.Rows.Count > 1000000 Then $bForceFunc = True
  974. If $bForceFunc Then
  975. _ArrayTranspose($vValue)
  976. If $bValue Then
  977. $vRange.Value = $vValue
  978. Else
  979. $vRange.Formula = $vValue
  980. EndIf
  981. If @error Then Return SetError(5, @error, 0)
  982. Else
  983. Local $oExcel = $oWorkbook.Parent
  984. If $bValue Then
  985. $vRange.Value = $oExcel.Transpose($vValue)
  986. Else
  987. $vRange.Formula = $oExcel.Transpose($vValue)
  988. EndIf
  989. If @error Then Return SetError(6, @error, 0)
  990. EndIf
  991. EndIf
  992. Return $vRange
  993. EndFunc ;==>_Excel_RangeWrite
  994. ; #FUNCTION# ====================================================================================================================
  995. ; Author ........: water
  996. ; Modified.......:
  997. ; ===============================================================================================================================
  998. Func _Excel_SheetAdd($oWorkbook, $vSheet = Default, $bBefore = Default, $iCount = Default, $sName = Default)
  999. ; Error handler, automatic cleanup at end of function
  1000. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  1001. #forceref $oError
  1002. If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
  1003. Local $bInsertAtEnd = False, $iStartSheet, $oBefore = Default, $oAfter = Default
  1004. If $iCount = Default Then $iCount = 1
  1005. If $iCount > 255 Then Return SetError(6, 0, 0)
  1006. If $bBefore = Default Then $bBefore = True
  1007. If $vSheet = Default Then
  1008. $vSheet = $oWorkbook.ActiveSheet
  1009. ElseIf Not IsObj($vSheet) Then
  1010. If $vSheet = -1 Then
  1011. $vSheet = $oWorkbook.WorkSheets.Item($oWorkbook.WorkSheets.Count)
  1012. Else
  1013. $vSheet = $oWorkbook.WorkSheets.Item($vSheet)
  1014. EndIf
  1015. If @error Then Return SetError(2, @error, 0)
  1016. If $vSheet.Index = $oWorkbook.WorkSheets.Count And $bBefore = False Then $bInsertAtEnd = True
  1017. EndIf
  1018. If $sName <> Default Then
  1019. Local $aName = StringSplit($sName, "|")
  1020. SetError(0) ; Reset @error if the separator was not found
  1021. If $aName[1] <> "" Then ; Name provided
  1022. For $iIndex1 = 1 To $aName[0]
  1023. For $iIndex2 = 1 To $oWorkbook.WorkSheets.Count
  1024. If $oWorkbook.WorkSheets($iIndex2).Name = $aName[$iIndex1] Then Return SetError(3, $iIndex1, 0)
  1025. Next
  1026. Next
  1027. Else
  1028. $sName = Default ; No name provided
  1029. EndIf
  1030. EndIf
  1031. If $bBefore Then
  1032. $oBefore = $vSheet
  1033. Else
  1034. $oAfter = $vSheet
  1035. EndIf
  1036. Local $oSheet = $oWorkbook.WorkSheets.Add($oBefore, $oAfter, $iCount)
  1037. If @error Then Return SetError(4, @error, 0)
  1038. If $sName <> Default Then
  1039. ; If sheets are added after the last sheet then the returned sheet is the rightmost, else it is the leftmost
  1040. If $bInsertAtEnd = True Then
  1041. $iStartSheet = $oSheet.Index - $iCount + 1
  1042. Else
  1043. $iStartSheet = $oSheet.Index
  1044. EndIf
  1045. $iIndex2 = 1
  1046. For $iSheet = $iStartSheet To $iStartSheet + $iCount - 1
  1047. If $aName[$iIndex2] <> "" Then $oWorkbook.WorkSheets($iSheet).Name = $aName[$iIndex2]
  1048. If @error Then Return SetError(5, @error, 0)
  1049. $iIndex2 += 1
  1050. If $iIndex2 > $aName[0] Then ExitLoop
  1051. Next
  1052. EndIf
  1053. Return $oSheet
  1054. EndFunc ;==>_Excel_SheetAdd
  1055. ; #FUNCTION# ====================================================================================================================
  1056. ; Author ........: SEO <locodarwin at yahoo dot com>
  1057. ; Modified.......: litlmike, water
  1058. ; ===============================================================================================================================
  1059. Func _Excel_SheetCopyMove($oSourceBook, $vSourceSheet = Default, $oTargetBook = Default, $vTargetSheet = Default, $bBefore = Default, $bCopy = Default)
  1060. ; Error handler, automatic cleanup at end of function
  1061. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  1062. #forceref $oError
  1063. Local $vBefore = Default, $vAfter = Default
  1064. If Not IsObj($oSourceBook) Or ObjName($oSourceBook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
  1065. If $vSourceSheet = Default Then $vSourceSheet = $oSourceBook.ActiveSheet
  1066. If $oTargetBook = Default Then $oTargetBook = $oSourceBook
  1067. If Not IsObj($oTargetBook) Or ObjName($oTargetBook, 1) <> "_Workbook" Then Return SetError(2, 0, 0)
  1068. If $vTargetSheet = Default Then $vTargetSheet = 1
  1069. If $bBefore = Default Then $bBefore = True
  1070. If $bCopy = Default Then $bCopy = True
  1071. If Not IsObj($vSourceSheet) Then
  1072. $vSourceSheet = $oSourceBook.Sheets($vSourceSheet)
  1073. If @error Or Not IsObj($vSourceSheet) Then SetError(3, @error, 0)
  1074. EndIf
  1075. If Not IsObj($vTargetSheet) Then
  1076. $vTargetSheet = $oTargetBook.Sheets($vTargetSheet)
  1077. If @error Or Not IsObj($vTargetSheet) Then SetError(4, @error, 0)
  1078. EndIf
  1079. If $bBefore Then
  1080. $vBefore = $vTargetSheet
  1081. Else
  1082. $vAfter = $vTargetSheet
  1083. EndIf
  1084. If $bCopy Then
  1085. $vSourceSheet.Copy($vBefore, $vAfter)
  1086. Else
  1087. $vSourceSheet.Move($vBefore, $vAfter)
  1088. EndIf
  1089. If @error Then Return SetError(5, @error, 0)
  1090. If $bBefore Then
  1091. Return $oTargetBook.Sheets($vTargetSheet.Index - 1)
  1092. Else
  1093. Return $oTargetBook.Sheets($vTargetSheet.Index + 1)
  1094. EndIf
  1095. EndFunc ;==>_Excel_SheetCopyMove
  1096. ; #FUNCTION# ====================================================================================================================
  1097. ; Author ........: water
  1098. ; Modified.......:
  1099. ; ===============================================================================================================================
  1100. Func _Excel_SheetDelete($oWorkbook, $vSheet = Default)
  1101. ; Error handler, automatic cleanup at end of function
  1102. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  1103. #forceref $oError
  1104. If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
  1105. Local $oSheet
  1106. If $vSheet = Default Then
  1107. $oSheet = $oWorkbook.ActiveSheet
  1108. ElseIf Not IsObj($vSheet) Then
  1109. $oSheet = $oWorkbook.WorkSheets.Item($vSheet)
  1110. Else
  1111. $oSheet = $vSheet
  1112. EndIf
  1113. If @error Then Return SetError(2, @error, 0)
  1114. $oSheet.Delete()
  1115. If @error Then Return SetError(3, @error, 0)
  1116. Return 1
  1117. EndFunc ;==>_Excel_SheetDelete
  1118. ; #FUNCTION# ====================================================================================================================
  1119. ; Author ........: SEO <locodarwin at yahoo dot com>
  1120. ; Modified.......: litlmike, water
  1121. ; ===============================================================================================================================
  1122. Func _Excel_SheetList($oWorkbook)
  1123. ; Error handler, automatic cleanup at end of function
  1124. Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
  1125. #forceref $oError
  1126. If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
  1127. Local $iSheetCount = $oWorkbook.Sheets.Count
  1128. Local $aSheets[$iSheetCount][2]
  1129. For $iIndex = 0 To $iSheetCount - 1
  1130. $aSheets[$iIndex][0] = $oWorkbook.Sheets($iIndex + 1).Name
  1131. $aSheets[$iIndex][1] = $oWorkbook.Sheets($iIndex + 1)
  1132. Next
  1133. Return $aSheets
  1134. EndFunc ;==>_Excel_SheetList
  1135. ; #INTERNAL_USE_ONLY#============================================================================================================
  1136. ; Name...........: __Excel_CloseOnQuit
  1137. ; Description ...: Sets or returns the state used to determine if the Excel instance can be closed by _Excel_Close.
  1138. ; Syntax.........: __Excel_CloseOnQuit($oExcel[, $bNewState = Default])
  1139. ; Parameters ....: $oExcel - Object of the Excel instance to be processed
  1140. ; $bNewState - Optional: The following values can be passed:
  1141. ; |True if the Excel instance was started by function _Excel_Open and can be closed by _Excel_Close
  1142. ; |False if the Excel instance was just closed by _Excel_Close and needs to be removed from the table
  1143. ; |Default returns the current state (True, False) for the specified Excel instance
  1144. ; Return values .: Success - Current state. Can be either True (Instance will be closed by _Excel_Close) or False (Instance will not be closed by _Excel_Close)
  1145. ; Author ........: Valik
  1146. ; Modified ......: water
  1147. ; Remarks .......:
  1148. ; Related .......:
  1149. ; Link ..........:
  1150. ; Example .......: No
  1151. ; ===============================================================================================================================
  1152. Func __Excel_CloseOnQuit($oExcel, $bNewState = Default)
  1153. Static $bState[101] = [0]
  1154. If $bNewState = True Then ; Add new Excel instance to the table. Will be closed on _Excel_Close
  1155. For $i = 1 To $bState[0]
  1156. If Not IsObj($bState[$i]) Or $bState[$i] = $oExcel Then ; Empty cell found or instance already stored
  1157. $bState[$i] = $oExcel
  1158. Return True
  1159. EndIf
  1160. Next
  1161. $bState[0] = $bState[0] + 1 ; No empty cell found and instance not already in table. Create a new entry at the end of the table
  1162. $bState[$bState[0]] = $oExcel
  1163. Return True
  1164. Else
  1165. For $i = 1 To $bState[0]
  1166. If $bState[$i] = $oExcel Then ; Excel instance found
  1167. If $bNewState = False Then ; Remove Excel instance from table (set value to zero)
  1168. $bState[$i] = 0
  1169. Return False
  1170. Else
  1171. Return True ; Excel instance found. Will be closed on _Excel_Close
  1172. EndIf
  1173. EndIf
  1174. Next
  1175. EndIf
  1176. Return False ; Excel instance not found. Will not be closed by _Excel_Close
  1177. EndFunc ;==>__Excel_CloseOnQuit
  1178. ; #INTERNAL_USE_ONLY#============================================================================================================
  1179. ; Name...........: __Excel_COMErrFunc
  1180. ; Description ...: Dummy function for silently handling COM errors.
  1181. ; Syntax.........:
  1182. ; Parameters ....:
  1183. ; Return values .:
  1184. ;
  1185. ; Author ........:
  1186. ; Modified ......:
  1187. ; Remarks .......:
  1188. ; Related .......:
  1189. ; Link ..........:
  1190. ; Example .......:
  1191. ; ===============================================================================================================================
  1192. Func __Excel_COMErrFunc()
  1193. ; Do nothing special, just check @error after suspect functions.
  1194. EndFunc ;==>__Excel_COMErrFunc