Python.xba 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="Python" script:language="StarBasic">
  4. REM =======================================================================================================================
  5. REM === The Access2Base library is a part of the LibreOffice project. ===
  6. REM === Full documentation is available on http://www.access2base.com ===
  7. REM =======================================================================================================================
  8. Option Compatible
  9. Option Explicit
  10. REM -----------------------------------------------------------------------------------------------------------------------
  11. Public Sub DebugPrint(ParamArray pvArgs() As Variant)
  12. &apos;Print arguments unconditionally in console
  13. &apos;Arguments are separated by a TAB (simulated by spaces)
  14. &apos;Some pvArgs might be missing: a TAB is still generated
  15. Dim vVarTypes() As Variant, i As Integer
  16. Const cstTab = 5
  17. On Local Error Goto Exit_Sub &apos; Never interrupt processing
  18. Utils._SetCalledSub(&quot;DebugPrint&quot;)
  19. vVarTypes = Utils._AddNumeric(Array(vbEmpty, vbNull, vbDate, vbString, vbBoolean, vbObject, vbVariant, vbByte, vbArray + vbByte))
  20. If UBound(pvArgs) &gt;= 0 Then
  21. For i = 0 To UBound(pvArgs)
  22. If Not Utils._CheckArgument(pvArgs(i), i + 1, vVarTypes(), , False) Then pvArgs(i) = &quot;[TYPE?]&quot;
  23. Next i
  24. End If
  25. Dim sOutput As String, sArg As String
  26. sOutput = &quot;&quot;
  27. For i = 0 To UBound(pvArgs)
  28. sArg = Replace(Utils._CStr(pvArgs(i), _A2B_.DebugPrintShort), &quot;\;&quot;, &quot;;&quot;)
  29. &apos; Add argument to output
  30. If i = 0 Then
  31. sOutput = sArg
  32. Else
  33. sOutput = sOutput &amp; Space(cstTab - (Len(sOutput) Mod cstTab)) &amp; sArg
  34. End If
  35. Next i
  36. TraceLog(TRACEANY, sOutput, False)
  37. Exit_Sub:
  38. Utils._ResetCalledSub(&quot;DebugPrint&quot;)
  39. Exit Sub
  40. End Sub &apos; DebugPrint V0.9.5
  41. REM -----------------------------------------------------------------------------------------------------------------------
  42. REM --- PYTHON WRAPPERS ---
  43. REM -----------------------------------------------------------------------------------------------------------------------
  44. REM -----------------------------------------------------------------------------------------------------------------------
  45. Public Function PythonEventsWrapper(Optional poEvent As Variant) As Variant
  46. &apos; Python wrapper when Application.Events() method is invoked
  47. &apos; The ParamArray mechanism empties UNO objects when they are member of the arguments list
  48. &apos; As a workaround, the Application.Events function is executed directly
  49. If _ErrorHandler() Then On Local Error GoTo Exit_Function &apos; Do never interrupt
  50. PythonEventsWrapper = Null
  51. Dim vReturn As Variant, vArray As Variant
  52. Const cstObject = 1
  53. vReturn = Application.Events(poEvent)
  54. vArray = Array(cstObject, _A2B_.AddPython(vReturn), vReturn._Type)
  55. PythonEventsWrapper = vArray
  56. Exit_Function:
  57. Exit Function
  58. End Function &apos; PythonEventsWrapper V6.4
  59. REM -----------------------------------------------------------------------------------------------------------------------
  60. Public Function PythonWrapper(ByVal pvCallType As Variant _
  61. , ByVal pvObject As Variant _
  62. , ByVal pvScript As Variant _
  63. , ParamArray pvArgs() As Variant _
  64. ) As Variant
  65. &apos; Called from Python to apply
  66. &apos; - on object with entry pvObject in PythonCache
  67. &apos; Conventionally: -1 = Application
  68. &apos; -2 = DoCmd
  69. &apos; - a script pvScript which type is described by pvCallType
  70. &apos; - with arguments pvArgs(0)... (max. 8 for object methods)
  71. &apos; The value returned by the method/property is encapsulated in an array
  72. &apos; [0] =&gt; 0 = scalar or array returned by the method
  73. &apos; =&gt; 1 = basic object returned by the method
  74. &apos; =&gt; 2 = a null value
  75. &apos; [1] =&gt; the object reference or the returned value (complemented with arguments passed by reference, if any) or Null
  76. &apos; [2] =&gt; the object type or Null
  77. &apos; [3] =&gt; the object name, if any
  78. &apos; or, when pvCallType == vbUNO, as the UNO object returned by the property
  79. Dim vReturn As Variant, vArray As Variant
  80. Dim vObject As Variant, sScript As String, sModule As String
  81. Dim i As Integer, iNbArgs As Integer, vArg As Variant, vArgs() As Variant
  82. Const cstApplication = -1, cstDoCmd = -2
  83. Const cstScalar = 0, cstObject = 1, cstNull = 2, cstUNO = 3
  84. &apos;Conventional special values
  85. Const cstNoArgs = &quot;+++NOARGS+++&quot;, cstSymEmpty = &quot;+++EMPTY+++&quot;, cstSymNull = &quot;+++NULL+++&quot;, cstSymMissing = &quot;+++MISSING+++&quot;
  86. &apos;https://support.office.com/en-us/article/CallByName-fonction-49ce9475-c315-4f13-8d35-e98cfe98729a
  87. &apos;Determines the pvCallType
  88. Const vbGet = 2, vbLet = 4, vbMethod = 1, vbSet = 8, vbUNO = 16
  89. If _ErrorHandler() Then On Local Error GoTo Error_Function
  90. PythonWrapper = Null
  91. &apos;Reinterpret arguments one by one into vArgs, examine iso-dates and conventional NoArgs/Empty/Null values
  92. iNbArgs = -1
  93. vArgs = Array()
  94. If UBound(pvArgs) &gt;= 0 Then
  95. For i = 0 To UBound(pvArgs)
  96. vArg = pvArgs(i)
  97. If i = 0 And VarType(vArg) = vbString Then
  98. If vArg = cstNoArgs Then Exit For
  99. End If
  100. If VarType(vArg) = vbString Then
  101. If vArg = cstSymEmpty Then
  102. vArg = Empty
  103. ElseIf vArg = cstSymNull Then
  104. vArg = Null
  105. ElseIf vArg = cstSymMissing Then
  106. Exit For &apos; Next arguments must be missing also
  107. Else
  108. vArg = _CDate(vArg)
  109. End If
  110. End If
  111. iNbArgs = iNbArgs + 1
  112. ReDim Preserve vArgs(iNbArgs)
  113. vArgs(iNbArgs) = vArg
  114. Next i
  115. End If
  116. &apos;Check pvObject
  117. Select Case pvObject &apos; Always numeric
  118. Case cstApplication
  119. sModule = &quot;Application&quot;
  120. Select Case pvScript
  121. Case &quot;AllDialogs&quot; : If iNbArgs &lt; 0 Then vReturn = Application.AllDialogs() Else vReturn = Application.AllDialogs(vArgs(0))
  122. Case &quot;AllForms&quot; : If iNbArgs &lt; 0 Then vReturn = Application.AllForms() Else vReturn = Application.AllForms(vArgs(0))
  123. Case &quot;AllModules&quot; : If iNbArgs &lt; 0 Then vReturn = Application.AllModules() Else vReturn = Application.AllModules(vArgs(0))
  124. Case &quot;CloseConnection&quot;
  125. vReturn = Application.CloseConnection()
  126. Case &quot;CommandBars&quot; : If iNbArgs &lt; 0 Then vReturn = Application.CommandBars() Else vReturn = Application.CommandBars(vArgs(0))
  127. Case &quot;CurrentDb&quot; : vReturn = Application.CurrentDb()
  128. Case &quot;CurrentUser&quot; : vReturn = Application.CurrentUser()
  129. Case &quot;DAvg&quot; : vReturn = Application.DAvg(vArgs(0), vArgs(1), vArgs(2))
  130. Case &quot;DCount&quot; : vReturn = Application.DCount(vArgs(0), vArgs(1), vArgs(2))
  131. Case &quot;DLookup&quot; : vReturn = Application.DLookup(vArgs(0), vArgs(1), vArgs(2), vArgs(3))
  132. Case &quot;DMax&quot; : vReturn = Application.DMax(vArgs(0), vArgs(1), vArgs(2))
  133. Case &quot;DMin&quot; : vReturn = Application.DMin(vArgs(0), vArgs(1), vArgs(2))
  134. Case &quot;DStDev&quot; : vReturn = Application.DStDev(vArgs(0), vArgs(1), vArgs(2))
  135. Case &quot;DStDevP&quot; : vReturn = Application.DStDevP(vArgs(0), vArgs(1), vArgs(2))
  136. Case &quot;DSum&quot; : vReturn = Application.DSum(vArgs(0), vArgs(1), vArgs(2))
  137. Case &quot;DVar&quot; : vReturn = Application.DVar(vArgs(0), vArgs(1), vArgs(2))
  138. Case &quot;DVarP&quot; : vReturn = Application.DVarP(vArgs(0), vArgs(1), vArgs(2))
  139. Case &quot;Forms&quot; : If iNbArgs &lt; 0 Then vReturn = Application.Forms() Else vReturn = Application.Forms(vArgs(0))
  140. Case &quot;getObject&quot; : vReturn = Application.getObject(vArgs(0))
  141. Case &quot;getValue&quot; : vReturn = Application.getValue(vArgs(0))
  142. Case &quot;HtmlEncode&quot; : vReturn = Application.HtmlEncode(vArgs(0), vArgs(1))
  143. Case &quot;OpenDatabase&quot; : vReturn = Application.OpenDatabase(vArgs(0), vArgs(1), vArgs(2), vArgs(3))
  144. Case &quot;ProductCode&quot; : vReturn = Application.ProductCode()
  145. Case &quot;setValue&quot; : vReturn = Application.setValue(vArgs(0), vArgs(1))
  146. Case &quot;SysCmd&quot; : vReturn = Application.SysCmd(vArgs(0), vArgs(1), vARgs(2))
  147. Case &quot;TempVars&quot; : If iNbArgs &lt; 0 Then vReturn = Application.TempVars() Else vReturn = Application.TempVars(vArgs(0))
  148. Case &quot;Version&quot; : vReturn = Application.Version()
  149. Case Else
  150. GoTo Error_Proc
  151. End Select
  152. Case cstDoCmd
  153. sModule = &quot;DoCmd&quot;
  154. Select Case pvScript
  155. Case &quot;ApplyFilter&quot; : vReturn = DoCmd.ApplyFilter(vArgs(0), vArgs(1), vArgs(2))
  156. Case &quot;Close&quot; : vReturn = DoCmd.mClose(vArgs(0), vArgs(1), vArgs(2))
  157. Case &quot;CopyObject&quot; : vReturn = DoCmd.CopyObject(vArgs(0), vArgs(1), vArgs(2), vArgs(3))
  158. Case &quot;FindNext&quot; : vReturn = DoCmd.FindNext()
  159. Case &quot;FindRecord&quot; : vReturn = DoCmd.FindRecord(vArgs(0), vArgs(1), vArgs(2), vArgs(3), vArgs(4), vArgs(5), vArgs(6))
  160. Case &quot;GetHiddenAttribute&quot;
  161. vReturn = DoCmd.GetHiddenAttribute(vArgs(0), vArgs(1))
  162. Case &quot;GoToControl&quot; : vReturn = DoCmd.GoToControl(vArgs(0))
  163. Case &quot;GoToRecord&quot; : vReturn = DoCmd.GoToRecord(vArgs(0), vArgs(1), vArgs(2), vArgs(3))
  164. Case &quot;Maximize&quot; : vReturn = DoCmd.Maximize()
  165. Case &quot;Minimize&quot; : vReturn = DoCmd.Minimize()
  166. Case &quot;MoveSize&quot; : vReturn = DoCmd.MoveSize(vArgs(0), vArgs(1), vArgs(2), vArgs(3))
  167. Case &quot;OpenForm&quot; : vReturn = DoCmd.OpenForm(vArgs(0), vArgs(1), vArgs(2), vArgs(3), vArgs(4), vArgs(5), vArgs(6))
  168. Case &quot;OpenQuery&quot; : vReturn = DoCmd.OpenQuery(vArgs(0), vArgs(1), vArgs(2))
  169. Case &quot;OpenReport&quot; : vReturn = DoCmd.OpenReport(vArgs(0), vArgs(1))
  170. Case &quot;OpenSQL&quot; : vReturn = DoCmd.OpenSQL(vArgs(0), vArgs(1))
  171. Case &quot;OpenTable&quot; : vReturn = DoCmd.OpenTable(vArgs(0), vArgs(1), vArgs(2))
  172. Case &quot;OutputTo&quot; : vReturn = DoCmd.OutputTo(vArgs(0), vArgs(1), vArgs(2), vArgs(3), vArgs(4), vArgs(5), vArgs(6), vArgs(7))
  173. Case &quot;Quit&quot; : _A2B_.CalledSub = &quot;Quit&quot; : GoTo Error_Action
  174. Case &quot;RunApp&quot; : vReturn = DoCmd.RunApp(vArgs(0))
  175. Case &quot;RunCommand&quot; : vReturn = DoCmd.RunCommand(vArgs(0))
  176. Case &quot;RunSQL&quot; : vReturn = DoCmd.RunSQL(vArgs(0), vArgs(1))
  177. Case &quot;SelectObject&quot; : vReturn = DoCmd.SelectObject(vArgs(0), vArgs(1), vArgs(2))
  178. Case &quot;SendObject&quot; : vReturn = DoCmd.SendObject(vArgs(0), vArgs(1), vArgs(2), vArgs(3), vArgs(4), vArgs(5), vArgs(6), vArgs(7), vArgs(8), vArgs(9))
  179. Case &quot;SetHiddenAttribute&quot;
  180. vReturn = DoCmd.SetHiddenAttribute(vArgs(0), vArgs(1), vArgs(2))
  181. Case &quot;SetOrderBy&quot; : vReturn = DoCmd.SetOrderBy(vArgs(0), vArgs(1))
  182. Case &quot;ShowAllRecords&quot;
  183. vReturn = DoCmd.ShowAllRecords()
  184. Case Else
  185. GoTo Error_Proc
  186. End Select
  187. Case Else
  188. &apos; Locate targeted object
  189. If pvObject &gt; UBound(_A2B_.PythonCache) Or pvObject &lt; 0 Then GoTo Error_Object
  190. Set vObject = _A2B_.PythonCache(pvObject)
  191. If IsNull(vObject) Then
  192. If pvScript = &quot;Dispose&quot; Then GoTo Exit_Function Else GoTo Error_Object
  193. End If
  194. &apos; Preprocessing
  195. sScript = pvScript
  196. sModule = vObject._Type
  197. Select Case sScript
  198. Case &quot;Add&quot;
  199. If vObject._Type = &quot;COLLECTION&quot; And vObject._CollType = COLLTABLEDEFS Then vArgs = Array(_A2B_.PythonCache(vArgs(0)))
  200. Case &quot;Close&quot;
  201. sSCript = &quot;mClose&quot;
  202. Case &quot;Type&quot;
  203. sScript = &quot;pType&quot;
  204. Case Else
  205. End Select
  206. &apos; Execute method
  207. Select Case UBound(vArgs) &apos; Dirty but ... CallByName does not support an array of arguments or return values
  208. Case -1
  209. If pvCallType = vbUNO Then
  210. With vObject
  211. Select Case sScript &apos; List all properties that should be called directly (UNO)
  212. Case &quot;BoundField&quot; : vReturn = .BoundField
  213. Case &quot;Column&quot; : vReturn = .Column
  214. Case &quot;Connection&quot; : vReturn = .Connection
  215. case &quot;ContainerWindow&quot; : vReturn = .ContainerWindow
  216. Case &quot;ControlModel&quot; : vReturn = .ControlModel
  217. Case &quot;ControlView&quot; : vReturn = .ControlView
  218. Case &quot;DatabaseForm&quot; : vReturn = .DatabaseForm
  219. Case &quot;Document&quot; : vReturn = .Document
  220. Case &quot;FormsCollection&quot; : vReturn = .FormsCollection
  221. Case &quot;LabelControl&quot; : vReturn = .LabelControl
  222. Case &quot;MetaData&quot; : vReturn = .MetaData
  223. Case &quot;ParentComponent&quot; : vReturn = .ParentComponent
  224. Case &quot;Query&quot; : vReturn = .Query
  225. Case &quot;RowSet&quot; : vReturn = .RowSet
  226. Case &quot;Table&quot; : vReturn = .Table
  227. Case &quot;UnoDialog&quot; : vReturn = .UnoDialog
  228. Case Else
  229. End Select
  230. End With
  231. ElseIf sScript = &quot;ItemData&quot; Then &apos; List all properties that should be called directly (arrays not supported by CallByName)
  232. vReturn = vObject.ItemData
  233. ElseIf sScript = &quot;LinkChildFields&quot; Then
  234. vReturn = vObject.LinkChildFields
  235. ElseIf sScript = &quot;LinkMasterFields&quot; Then
  236. vReturn = vObject.LinkMasterFields
  237. ElseIf sScript = &quot;OpenArgs&quot; Then
  238. vReturn = vObject.OpenArgs
  239. ElseIf sScript = &quot;Selected&quot; Then
  240. vReturn = vObject.Selected
  241. ElseIf sScript = &quot;Value&quot; Then
  242. vReturn = vObject.Value
  243. Else
  244. vReturn = CallByName(vObject, sScript, pvCallType)
  245. End If
  246. Case 0
  247. Select Case sScript
  248. Case &quot;AppendChunk&quot; &apos; Arg is a vector, not supported by CallByName
  249. vReturn = vObject.GetChunk(vArgs(0), vArgs(1))
  250. Case &quot;GetRows&quot; &apos; Returns an array, not supported by CallByName
  251. vReturn = vObject.GetRows(vArgs(0), True) &apos; Force iso dates
  252. Case Else
  253. vReturn = CallByName(vObject, sScript, pvCallType, vArgs(0))
  254. End Select
  255. Case 1
  256. Select Case sScript
  257. Case &quot;GetChunk&quot; &apos; Returns a vector, not supported by CallByName
  258. vReturn = vObject.GetChunk(vArgs(0), vArgs(1))
  259. Case Else
  260. vReturn = CallByName(vObject, sScript, pvCallType, vArgs(0), vArgs(1))
  261. End Select
  262. Case 2 : vReturn = CallByName(vObject, sScript, pvCallType, vArgs(0), vArgs(1), vArgs(2))
  263. Case 3 : vReturn = CallByName(vObject, sScript, pvCallType, vArgs(0), vArgs(1), vArgs(2), vArgs(3))
  264. Case 4 : vReturn = CallByName(vObject, sScript, pvCallType, vArgs(0), vArgs(1), vArgs(2), vArgs(3), vArgs(4))
  265. Case 5 : vReturn = CallByName(vObject, sScript, pvCallType, vArgs(0), vArgs(1), vArgs(2), vArgs(3), vArgs(4), vArgs(5))
  266. Case 6 : vReturn = CallByName(vObject, sScript, pvCallType, vArgs(0), vArgs(1), vArgs(2), vArgs(3), vArgs(4), vArgs(5), vArgs(6))
  267. Case 7 : vReturn = CallByName(vObject, sScript, pvCallType, vArgs(0), vArgs(1), vArgs(2), vArgs(3), vArgs(4), vArgs(5), vArgs(6), vArgs(7))
  268. End Select
  269. &apos; Postprocessing
  270. Select Case pvScript
  271. Case &quot;Close&quot;, &quot;Dispose&quot;, &quot;Terminate&quot;
  272. Set _A2B_.PythonCache(pvObject) = Nothing
  273. Case &quot;Move&quot;, &quot;MoveFirst&quot;, &quot;MoveLast&quot;, &quot;MoveNext&quot;, &quot;MovePrevious&quot; &apos; Pass the new BOF, EOF values (binary format)
  274. If vObject._Type = &quot;RECORDSET&quot; Then
  275. vReturn = (Iif(vObject.BOF, 1, 0) * 2 + Iif(vObject.EOF, 1, 0)) * Iif(vReturn, 1, -1)
  276. End If
  277. Case &quot;Find&quot; &apos; Store in array the arguments passed by reference
  278. If vObject._Type = &quot;MODULE&quot; And vReturn = True Then
  279. vReturn = Array(vReturn, vArgs(1), vArgs(2), vArgs(3), vArgs(4))
  280. End If
  281. Case &quot;ProcOfLine&quot; &apos; Store in array the arguments passed by reference
  282. vReturn = Array(vReturn, vArgs(1))
  283. Case Else
  284. End Select
  285. End Select
  286. &apos; Structure the returned array
  287. If pvCallType = vbUNO Then
  288. vArray = vReturn
  289. Else
  290. If IsNull(vReturn) Then
  291. vArray = Array(cstNull, Null, Null)
  292. ElseIf IsObject(vReturn) Then
  293. Select Case vReturn._Type
  294. Case &quot;COLLECTION&quot;, &quot;COMMANDBARCONTROL&quot;, &quot;EVENT&quot;
  295. vArray = Array(cstObject, _A2B_.AddPython(vReturn), vReturn._Type)
  296. Case Else
  297. vArray = Array(cstObject, _A2B_.AddPython(vReturn), vReturn._Type, vReturn.Name)
  298. End Select
  299. Else
  300. If VarType(vReturn) = vbDate Then
  301. vArray = Array(cstScalar, _CStr(vReturn), Null)
  302. ElseIf VarType(vReturn) = vbBigint Then &apos; Could happen for big integer database fields
  303. vArray = Array(cstScalar, CLng(vReturn), Null)
  304. Else
  305. vArray = Array(cstScalar, vReturn, Null)
  306. End If
  307. End If
  308. End If
  309. PythonWrapper = vArray
  310. Exit_Function:
  311. Exit Function
  312. Error_Function:
  313. TraceError(TRACEABORT, Err, &quot;PythonWrapper&quot;, Erl)
  314. GoTo Exit_Function
  315. Error_Object:
  316. TraceError(TRACEFATAL, ERROBJECTNOTFOUND, &quot;Python Wrapper (&quot; &amp; pvScript &amp; &quot;)&quot;, 0, , Array(_GetLabel(&quot;OBJECT&quot;), &quot;#&quot; &amp; pvObject))
  317. GoTo Exit_Function
  318. Error_Action:
  319. TraceError(TRACEFATAL, ERRACTION, Utils._CalledSub(), 0)
  320. GoTo Exit_Function
  321. Error_Proc:
  322. TraceError(TRACEFATAL, ERRPROCEDURENOTFOUND, &quot;Python Wrapper&quot;, 0, , Array(pvScript, sModule))
  323. GoTo Exit_Function
  324. End Function &apos; PythonWrapper V6.4
  325. REM -----------------------------------------------------------------------------------------------------------------------
  326. REM --- PYTHON HELPER FUNCTIONS ---
  327. REM -----------------------------------------------------------------------------------------------------------------------
  328. REM -----------------------------------------------------------------------------------------------------------------------
  329. Public Function PyConvertFromUrl(ByVal pvFile As Variant) As String
  330. &apos; Convenient function to have common conversions of filenames from/to url notations both in Python and Basic
  331. On Local Error GoTo Exit_Function
  332. PyConvertFromUrl = &quot;&quot;
  333. If Not Utils._CheckArgument(pvFile, 1, vbString) Then Goto Exit_Function
  334. PyConvertFromUrl = ConvertFromUrl(pvFile)
  335. Exit_Function:
  336. Exit Function
  337. End Function &apos; PyConvertFromUrl V6.4
  338. REM -----------------------------------------------------------------------------------------------------------------------
  339. Public Function PyConvertToUrl(ByVal pvFile As Variant) As String
  340. &apos; Convenient function to have common conversions of filenames from/to url notations both in Python and Basic
  341. On Local Error GoTo Exit_Function
  342. PyConvertToUrl = &quot;&quot;
  343. If Not Utils._CheckArgument(pvFile, 1, vbString) Then Goto Exit_Function
  344. PyConvertToUrl = ConvertToUrl(pvFile)
  345. Exit_Function:
  346. Exit Function
  347. End Function &apos; PyConvertToUrl V6.4
  348. REM -----------------------------------------------------------------------------------------------------------------------
  349. Public Function PyCreateUnoService(ByVal pvService As Variant) As Variant
  350. &apos; Convenient function to create a UNO service in Python
  351. On Local Error GoTo Exit_Function
  352. Set PyCreateUnoService = Nothing
  353. If Not Utils._CheckArgument(pvService, 1, vbString) Then Goto Exit_Function
  354. Set PyCreateUnoService = CreateUnoService(pvService)
  355. Exit_Function:
  356. Exit Function
  357. End Function &apos; PyCreateUnoService V6.4
  358. REM -----------------------------------------------------------------------------------------------------------------------
  359. Public Function PyDateAdd(ByVal pvAdd As Variant _
  360. , ByVal pvCount As Variant _
  361. , ByVal pvDate As Variant _
  362. ) As Variant
  363. &apos; Convenient shortcut to useful and easy-to-use Basic date functions
  364. Dim vDate As Variant, vNewDate As Variant
  365. On Local Error GoTo Exit_Function
  366. PyDateAdd = Null
  367. If Not Utils._CheckArgument(pvAdd, 1, vbString) Then Goto Exit_Function
  368. If Not Utils._CheckArgument(pvCount, 2, Utils._AddNumeric()) Then Goto Exit_Function
  369. If Not Utils._CheckArgument(pvDate, 3, vbString) Then Goto Exit_Function
  370. vDate = _CDate(pvDate)
  371. vNewDate = DateAdd(pvAdd, pvCount, vDate)
  372. If VarType(vNewDate) = vbDate Then PyDateAdd = _CStr(vNewDate) Else PyDateAdd = vNewDate
  373. Exit_Function:
  374. Exit Function
  375. End Function &apos; PyDateAdd V6.4
  376. REM -----------------------------------------------------------------------------------------------------------------------
  377. Public Function PyDateDiff(ByVal pvAdd As Variant _
  378. , ByVal pvDate1 As Variant _
  379. , ByVal pvDate2 As Variant _
  380. , ByVal pvWeekStart As Variant _
  381. , ByVal pvYearStart As Variant _
  382. ) As Variant
  383. &apos; Convenient shortcut to useful and easy-to-use Basic date functions
  384. Dim vDate1 As Variant, vDate2 As Variant
  385. On Local Error GoTo Exit_Function
  386. PyDateDiff = Null
  387. If Not Utils._CheckArgument(pvAdd, 1, vbString) Then Goto Exit_Function
  388. If Not Utils._CheckArgument(pvDate1, 2, vbString) Then Goto Exit_Function
  389. If Not Utils._CheckArgument(pvDate2, 3, vbString) Then Goto Exit_Function
  390. If Not Utils._CheckArgument(pvWeekStart, 4, Utils._AddNumeric()) Then Goto Exit_Function
  391. If Not Utils._CheckArgument(pvWeekStart, 5, Utils._AddNumeric()) Then Goto Exit_Function
  392. vDate1 = _CDate(pvDate1)
  393. vDate2 = _CDate(pvDate2)
  394. PyDateDiff = DateDiff(pvAdd, vDate1, vDate2, pvWeekStart, pvYearStart)
  395. Exit_Function:
  396. Exit Function
  397. End Function &apos; PyDateDiff V6.4
  398. REM -----------------------------------------------------------------------------------------------------------------------
  399. Public Function PyDatePart(ByVal pvAdd As Variant _
  400. , ByVal pvDate As Variant _
  401. , ByVal pvWeekStart As Variant _
  402. , ByVal pvYearStart As Variant _
  403. ) As Variant
  404. &apos; Convenient shortcut to useful and easy-to-use Basic date functions
  405. Dim vDate As Variant
  406. On Local Error GoTo Exit_Function
  407. PyDatePart = Null
  408. If Not Utils._CheckArgument(pvAdd, 1, vbString) Then Goto Exit_Function
  409. If Not Utils._CheckArgument(pvDate, 2, vbString) Then Goto Exit_Function
  410. If Not Utils._CheckArgument(pvWeekStart, 3, Utils._AddNumeric()) Then Goto Exit_Function
  411. If Not Utils._CheckArgument(pvWeekStart, 4, Utils._AddNumeric()) Then Goto Exit_Function
  412. vDate = _CDate(pvDate)
  413. PyDatePart = DatePart(pvAdd, vDate, pvWeekStart, pvYearStart)
  414. Exit_Function:
  415. Exit Function
  416. End Function &apos; PyDatePart V6.4
  417. REM -----------------------------------------------------------------------------------------------------------------------
  418. Public Function PyDateValue(ByVal pvDate As Variant) As Variant
  419. &apos; Convenient shortcut to useful and easy-to-use Basic date functions
  420. Dim vDate As Variant
  421. On Local Error GoTo Exit_Function
  422. PyDateValue = Null
  423. If Not Utils._CheckArgument(pvDate, 1, vbString) Then Goto Exit_Function
  424. vDate = DateValue(pvDate)
  425. If VarType(vDate) = vbDate Then PyDateValue = _CStr(vDate) Else PyDateValue = vDate
  426. Exit_Function:
  427. Exit Function
  428. End Function &apos; PyDateValue V6.4
  429. REM -----------------------------------------------------------------------------------------------------------------------
  430. Public Function PyFormat(ByVal pvValue As Variant, pvFormat As Variant) As String
  431. &apos; Convenient function to format numbers or dates
  432. On Local Error GoTo Exit_Function
  433. PyFormat = &quot;&quot;
  434. If Not Utils._CheckArgument(pvValue, 1, Utils._AddNumeric(vbString)) Then Goto Exit_Function
  435. pvValue = _CDate(pvValue)
  436. If IsEmpty(pvFormat) Then
  437. PyFormat = Str(pvValue)
  438. Else
  439. If Not Utils._CheckArgument(pvFormat, 2, vbString) Then Goto Exit_Function
  440. PyFormat = Format(pvValue, pvFormat)
  441. End If
  442. Exit_Function:
  443. Exit Function
  444. End Function &apos; PyFormat V6.4
  445. REM -----------------------------------------------------------------------------------------------------------------------
  446. Public Function PyGetGUIType() As Variant
  447. PyGetGUIType = GetGUIType()
  448. End Function &apos; PyGetGUIType V6.4
  449. REM -----------------------------------------------------------------------------------------------------------------------
  450. Public Function PyGetSystemTicks() As Variant
  451. PyGetSystemTicks = GetSystemTicks()
  452. End Function &apos; PyGetSystemTicks V6.4
  453. REM -----------------------------------------------------------------------------------------------------------------------
  454. Public Function PyGlobalScope(ByVal pvLib As Variant) As Variant
  455. Select Case pvLib
  456. Case &quot;Basic&quot;
  457. PyGlobalScope = GlobalScope.BasicLibraries()
  458. Case &quot;Dialog&quot;
  459. PyGlobalScope = GlobalScope.DialogLibraries()
  460. Case Else
  461. End Select
  462. End Function &apos; PyGlobalScope V6.4
  463. REM -----------------------------------------------------------------------------------------------------------------------
  464. Public Function PyInputBox(ByVal pvText As Variant _
  465. , ByVal pvTitle As Variant _
  466. , ByVal pvDefault As Variant _
  467. , ByVal pvXPos As Variant _
  468. , ByVal pvYPos As Variant _
  469. ) As Variant
  470. &apos; Convenient function to open input box from Python
  471. On Local Error GoTo Exit_Function
  472. PyInputBox = Null
  473. If Not Utils._CheckArgument(pvText, 1, vbString) Then Goto Exit_Function
  474. If IsEmpty(pvTitle) Then pvTitle = &quot;&quot;
  475. If Not Utils._CheckArgument(pvTitle, 2, vbString) Then Goto Exit_Function
  476. If IsEmpty(pvDefault) Then pvDefault = &quot;&quot;
  477. If Not Utils._CheckArgument(pvDefault, 3, vbString) Then Goto Exit_Function
  478. If IsEmpty(pvXPos) Or IsEmpty(pvYPos) Then
  479. PyInputBox = InputBox(pvText, pvTitle, pvDefault)
  480. Else
  481. If Not Utils._CheckArgument(pvXPos, 4, Utils._AddNumeric()) Then Goto Exit_Function
  482. If Not Utils._CheckArgument(pvYPos, 5, Utils._AddNumeric()) Then Goto Exit_Function
  483. PyInputBox = InputBox(pvText, pvTitle, pvDefault, pvXPos, pvYPos)
  484. End If
  485. Exit_Function:
  486. Exit Function
  487. End Function &apos; PyInputBox V6.4.0
  488. REM -----------------------------------------------------------------------------------------------------------------------
  489. Public Function PyMsgBox(ByVal pvText As Variant _
  490. , ByVal pvType As Variant _
  491. , ByVal pvDialogTitle As Variant _
  492. ) As Variant
  493. &apos; Convenient function to open message box from Python
  494. On Local Error GoTo Exit_Function
  495. PyMsgBox = Null
  496. If Not Utils._CheckArgument(pvText, 1, vbString) Then Goto Exit_Function
  497. If IsEmpty(pvType) Then pvType = 0
  498. If Not Utils._CheckArgument(pvType, 2, Utils._AddNumeric()) Then Goto Exit_Function
  499. If IsEmpty(pvDialogTitle) Then
  500. PyMsgBox = MsgBox(pvText, pvType)
  501. Else
  502. If Not Utils._CheckArgument(pvDialogTitle, 3, vbString) Then Goto Exit_Function
  503. PyMsgBox = MsgBox(pvText, pvType, pvDialogTitle)
  504. End If
  505. Exit_Function:
  506. Exit Function
  507. End Function &apos; PyMsgBox V6.4.0
  508. REM -----------------------------------------------------------------------------------------------------------------------
  509. Public Function PyTimer() As Long
  510. &apos; Convenient function to call Timer from Python
  511. PyTimer = Timer
  512. End Function &apos; PyTimer V6.4
  513. REM -----------------------------------------------------------------------------------------------------------------------
  514. REM --- PRIVATE FUNCTIONS ---
  515. REM -----------------------------------------------------------------------------------------------------------------------
  516. REM -----------------------------------------------------------------------------------------------------------------------
  517. Private Function _CDate(ByVal pvValue As Variant) As Variant
  518. &apos; Return a Date type if iso date, otherwise return input
  519. Dim vValue As Variant
  520. vValue = pvValue
  521. If VarType(pvValue) = vbString Then
  522. If pvValue &lt;&gt; &quot;&quot; And IsDate(pvValue) Then vValue = CDate(pvValue) &apos; IsDate(&quot;&quot;) gives True !?
  523. End If
  524. _CDate = vValue
  525. End Function
  526. </script:module>