Misc.xba 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <!--
  4. * This file is part of the LibreOffice project.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. *
  10. * This file incorporates work covered by the following license notice:
  11. *
  12. * Licensed to the Apache Software Foundation (ASF) under one or more
  13. * contributor license agreements. See the NOTICE file distributed
  14. * with this work for additional information regarding copyright
  15. * ownership. The ASF licenses this file to you under the Apache
  16. * License, Version 2.0 (the "License"); you may not use this file
  17. * except in compliance with the License. You may obtain a copy of
  18. * the License at http://www.apache.org/licenses/LICENSE-2.0 .
  19. -->
  20. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="Misc" script:language="StarBasic">REM ***** BASIC *****
  21. Const SBSHARE = 0
  22. Const SBUSER = 1
  23. Dim Taskindex as Integer
  24. Dim oResSrv as Object
  25. Sub Main()
  26. Dim PropList(3,1)&apos; as String
  27. PropList(0,0) = &quot;URL&quot;
  28. PropList(0,1) = &quot;sdbc:odbc:Erica_Test_Unicode&quot;
  29. PropList(1,0) = &quot;User&quot;
  30. PropList(1,1) = &quot;extra&quot;
  31. PropList(2,0) = &quot;Password&quot;
  32. PropList(2,1) = &quot;extra&quot;
  33. PropList(3,0) = &quot;IsPasswordRequired&quot;
  34. PropList(3,1) = True
  35. End Sub
  36. Function RegisterNewDataSource(DSName as String, PropertyList(), Optional DriverProperties() as New com.sun.star.beans.PropertyValue)
  37. Dim oDataSource as Object
  38. Dim oDBContext as Object
  39. Dim oPropInfo as Object
  40. Dim i as Integer
  41. oDBContext = createUnoService(&quot;com.sun.star.sdb.DatabaseContext&quot;)
  42. oDataSource = createUnoService(&quot;com.sun.star.sdb.DataSource&quot;)
  43. For i = 0 To Ubound(PropertyList(), 1)
  44. sPropName = PropertyList(i,0)
  45. sPropValue = PropertyList(i,1)
  46. oDataSource.SetPropertyValue(sPropName,sPropValue)
  47. Next i
  48. If Not IsMissing(DriverProperties()) Then
  49. oDataSource.Info() = DriverProperties()
  50. End If
  51. oDBContext.RegisterObject(DSName, oDataSource)
  52. RegisterNewDataSource () = oDataSource
  53. End Function
  54. &apos; Connects to a registered Database
  55. Function ConnecttoDatabase(DSName as String, UserID as String, Password as String, Optional Propertylist(), Optional DriverProperties() as New com.sun.star.beans.PropertyValue)
  56. Dim oDBContext as Object
  57. Dim oDBSource as Object
  58. &apos; On Local Error Goto NOCONNECTION
  59. oDBContext = CreateUnoService(&quot;com.sun.star.sdb.DatabaseContext&quot;)
  60. If oDBContext.HasbyName(DSName) Then
  61. oDBSource = oDBContext.GetByName(DSName)
  62. ConnectToDatabase = oDBSource.GetConnection(UserID, Password)
  63. Else
  64. If Not IsMissing(Namelist()) Then
  65. If Not IsMissing(DriverProperties()) Then
  66. RegisterNewDataSource(DSName, PropertyList(), DriverProperties())
  67. Else
  68. RegisterNewDataSource(DSName, PropertyList())
  69. End If
  70. oDBSource = oDBContext.GetByName(DSName)
  71. ConnectToDatabase = oDBSource.GetConnection(UserID, Password)
  72. Else
  73. Msgbox(&quot;DataSource &quot; &amp; DSName &amp; &quot; is not registered&quot; , 16, GetProductname())
  74. ConnectToDatabase() = NULL
  75. End If
  76. End If
  77. NOCONNECTION:
  78. If Err &lt;&gt; 0 Then
  79. Msgbox(Error$, 16, GetProductName())
  80. Resume LEAVESUB
  81. LEAVESUB:
  82. End If
  83. End Function
  84. Function GetStarOfficeLocale() as New com.sun.star.lang.Locale
  85. Dim aLocLocale As New com.sun.star.lang.Locale
  86. Dim sLocale as String
  87. Dim sLocaleList(1)
  88. Dim oMasterKey
  89. oMasterKey = GetRegistryKeyContent(&quot;org.openoffice.Setup/L10N/&quot;)
  90. sLocale = oMasterKey.getByName(&quot;ooLocale&quot;)
  91. sLocaleList() = ArrayoutofString(sLocale, &quot;-&quot;)
  92. aLocLocale.Language = sLocaleList(0)
  93. If Ubound(sLocaleList()) &gt; 0 Then
  94. aLocLocale.Country = sLocaleList(1)
  95. End If
  96. If Ubound(sLocaleList()) &gt; 1 Then
  97. aLocLocale.Variant = sLocaleList(2)
  98. End If
  99. GetStarOfficeLocale() = aLocLocale
  100. End Function
  101. Function GetRegistryKeyContent(sKeyName as string, Optional bforUpdate as Boolean)
  102. Dim oConfigProvider as Object
  103. Dim aNodePath(0) as new com.sun.star.beans.PropertyValue
  104. oConfigProvider = createUnoService(&quot;com.sun.star.configuration.ConfigurationProvider&quot;)
  105. aNodePath(0).Name = &quot;nodepath&quot;
  106. aNodePath(0).Value = sKeyName
  107. If IsMissing(bForUpdate) Then
  108. GetRegistryKeyContent() = oConfigProvider.createInstanceWithArguments(&quot;com.sun.star.configuration.ConfigurationAccess&quot;, aNodePath())
  109. Else
  110. If bForUpdate Then
  111. GetRegistryKeyContent() = oConfigProvider.createInstanceWithArguments(&quot;com.sun.star.configuration.ConfigurationUpdateAccess&quot;, aNodePath())
  112. Else
  113. GetRegistryKeyContent() = oConfigProvider.createInstanceWithArguments(&quot;com.sun.star.configuration.ConfigurationAccess&quot;, aNodePath())
  114. End If
  115. End If
  116. End Function
  117. Function GetProductname() as String
  118. Dim oProdNameAccess as Object
  119. Dim sVersion as String
  120. Dim sProdName as String
  121. oProdNameAccess = GetRegistryKeyContent(&quot;org.openoffice.Setup/Product&quot;)
  122. sProdName = oProdNameAccess.getByName(&quot;ooName&quot;)
  123. sVersion = oProdNameAccess.getByName(&quot;ooSetupVersion&quot;)
  124. GetProductName = sProdName &amp; sVersion
  125. End Function
  126. &apos; Opens a Document, checks beforehand, whether it has to be loaded
  127. &apos; or whether it is already on the desktop.
  128. &apos; If the parameter bDisposable is set to False then the returned document
  129. &apos; should not be disposed afterwards, because it is already opened.
  130. Function OpenDocument(DocPath as String, Args(), Optional bDisposable as Boolean)
  131. Dim oComponents as Object
  132. Dim oComponent as Object
  133. &apos; Search if one of the active Components is the one that you search for
  134. oComponents = StarDesktop.Components.CreateEnumeration
  135. While oComponents.HasmoreElements
  136. oComponent = oComponents.NextElement
  137. If hasUnoInterfaces(oComponent,&quot;com.sun.star.frame.XModel&quot;) then
  138. If UCase(oComponent.URL) = UCase(DocPath) then
  139. OpenDocument() = oComponent
  140. If Not IsMissing(bDisposable) Then
  141. bDisposable = False
  142. End If
  143. Exit Function
  144. End If
  145. End If
  146. Wend
  147. If Not IsMissing(bDisposable) Then
  148. bDisposable = True
  149. End If
  150. OpenDocument() = StarDesktop.LoadComponentFromURL(DocPath,&quot;_default&quot;,0,Args())
  151. End Function
  152. Function TaskonDesktop(DocPath as String) as Boolean
  153. Dim oComponents as Object
  154. Dim oComponent as Object
  155. &apos; Search if one of the active Components is the one that you search for
  156. oComponents = StarDesktop.Components.CreateEnumeration
  157. While oComponents.HasmoreElements
  158. oComponent = oComponents.NextElement
  159. If hasUnoInterfaces(oComponent,&quot;com.sun.star.frame.XModel&quot;) then
  160. If UCase(oComponent.URL) = UCase(DocPath) then
  161. TaskonDesktop = True
  162. Exit Function
  163. End If
  164. End If
  165. Wend
  166. TaskonDesktop = False
  167. End Function
  168. &apos; Retrieves a FileName out of a StarOffice-Document
  169. Function RetrieveFileName(LocDoc as Object)
  170. Dim LocURL as String
  171. Dim LocURLArray() as String
  172. Dim MaxArrIndex as integer
  173. LocURL = LocDoc.Url
  174. LocURLArray() = ArrayoutofString(LocURL,&quot;/&quot;,MaxArrIndex)
  175. RetrieveFileName = LocURLArray(MaxArrIndex)
  176. End Function
  177. &apos; Gets a special configured PathSetting
  178. Function GetPathSettings(sPathType as String, Optional bshowall as Boolean, Optional ListIndex as integer) as String
  179. Dim oSettings, oPathSettings as Object
  180. Dim sPath as String
  181. Dim PathList() as String
  182. Dim MaxIndex as Integer
  183. Dim oPS as Object
  184. oPS = createUnoService(&quot;com.sun.star.util.PathSettings&quot;)
  185. If Not IsMissing(bShowall) Then
  186. If bShowAll Then
  187. ShowPropertyValues(oPS)
  188. Exit Function
  189. End If
  190. End If
  191. sPath = oPS.getPropertyValue(sPathType)
  192. If Not IsMissing(ListIndex) Then
  193. &apos; Share and User-Directory
  194. If Instr(1,sPath,&quot;;&quot;) &lt;&gt; 0 Then
  195. PathList = ArrayoutofString(sPath,&quot;;&quot;, MaxIndex)
  196. If ListIndex &lt;= MaxIndex Then
  197. sPath = PathList(ListIndex)
  198. Else
  199. Msgbox(&quot;String Cannot be analyzed!&quot; &amp; sPath , 16, GetProductName())
  200. End If
  201. End If
  202. End If
  203. If Instr(1, sPath, &quot;;&quot;) = 0 Then
  204. GetPathSettings = ConvertToUrl(sPath)
  205. Else
  206. GetPathSettings = sPath
  207. End If
  208. End Function
  209. &apos; Gets the fully qualified path to a subdirectory of the
  210. &apos; Template Directory, e. g. with the parameter &quot;wizard/bitmap&quot;
  211. &apos; The parameter must be passed in Url notation
  212. &apos; The return-Value is in Url notation
  213. Function GetOfficeSubPath(sOfficePath as String, ByVal sSubDir as String)
  214. Dim sOfficeString as String
  215. Dim sOfficeList() as String
  216. Dim sOfficeDir as String
  217. Dim sBigDir as String
  218. Dim i as Integer
  219. Dim MaxIndex as Integer
  220. Dim oUcb as Object
  221. oUcb = createUnoService(&quot;com.sun.star.ucb.SimpleFileAccess&quot;)
  222. sOfficeString = GetPathSettings(sOfficePath)
  223. If Right(sSubDir,1) &lt;&gt; &quot;/&quot; Then
  224. sSubDir = sSubDir &amp; &quot;/&quot;
  225. End If
  226. sOfficeList() = ArrayoutofString(sOfficeString,&quot;;&quot;, MaxIndex)
  227. For i = 0 To MaxIndex
  228. sOfficeDir = ConvertToUrl(sOfficeList(i))
  229. If Right(sOfficeDir,1) &lt;&gt; &quot;/&quot; Then
  230. sOfficeDir = sOfficeDir &amp; &quot;/&quot;
  231. End If
  232. sBigDir = sOfficeDir &amp; sSubDir
  233. If oUcb.Exists(sBigDir) Then
  234. GetOfficeSubPath() = sBigDir
  235. Exit Function
  236. End If
  237. Next i
  238. ShowNoOfficePathError()
  239. GetOfficeSubPath = &quot;&quot;
  240. End Function
  241. Sub ShowNoOfficePathError()
  242. Dim ProductName as String
  243. Dim sError as String
  244. Dim bResObjectexists as Boolean
  245. Dim oLocResSrv as Object
  246. bResObjectexists = not IsNull(oResSrv)
  247. If bResObjectexists Then
  248. oLocResSrv = oResSrv
  249. End If
  250. If InitResources(&quot;Tools&quot;) Then
  251. ProductName = GetProductName()
  252. sError = GetResText(&quot;RID_COMMON_6&quot;)
  253. sError = ReplaceString(sError, ProductName, &quot;%PRODUCTNAME&quot;)
  254. sError = ReplaceString(sError, chr(13), &quot;&lt;BR&gt;&quot;)
  255. MsgBox(sError, 16, ProductName)
  256. End If
  257. If bResObjectexists Then
  258. oResSrv = oLocResSrv
  259. End If
  260. End Sub
  261. Function InitResources(Description) as boolean
  262. Dim xResource as Object
  263. Dim sOfficeDir as String
  264. Dim aArgs(5) as Any
  265. On Error Goto ErrorOccurred
  266. sOfficeDir = &quot;$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/wizards/&quot;
  267. sOfficeDir = GetDefaultContext.getByName(&quot;/singletons/com.sun.star.util.theMacroExpander&quot;).ExpandMacros(sOfficeDir)
  268. aArgs(0) = sOfficeDir
  269. aArgs(1) = true
  270. aArgs(2) = GetStarOfficeLocale()
  271. aArgs(3) = &quot;resources&quot;
  272. aArgs(4) = &quot;&quot;
  273. aArgs(5) = NULL
  274. oResSrv = getProcessServiceManager().createInstanceWithArguments( &quot;com.sun.star.resource.StringResourceWithLocation&quot;, aArgs() )
  275. If (IsNull(oResSrv)) then
  276. InitResources = FALSE
  277. MsgBox(&quot;could not initialize StringResourceWithLocation&quot;)
  278. Else
  279. InitResources = TRUE
  280. End If
  281. Exit Function
  282. ErrorOccurred:
  283. Dim nSolarVer
  284. InitResources = FALSE
  285. nSolarVer = GetSolarVersion()
  286. MsgBox(&quot;Resource file missing&quot;, 16, GetProductName())
  287. Resume CLERROR
  288. CLERROR:
  289. End Function
  290. Function GetResText( sID as String ) As string
  291. Dim sString as String
  292. On Error Goto ErrorOccurred
  293. If Not IsNull(oResSrv) Then
  294. sString = oResSrv.resolveString(sID)
  295. GetResText = ReplaceString(sString, GetProductname(), &quot;%PRODUCTNAME&quot;)
  296. Else
  297. GetResText = &quot;&quot;
  298. End If
  299. Exit Function
  300. ErrorOccurred:
  301. GetResText = &quot;&quot;
  302. MsgBox(&quot;Resource with ID =&quot; + sID + &quot; not found!&quot;, 16, GetProductName())
  303. Resume CLERROR
  304. CLERROR:
  305. End Function
  306. Function CutPathView(sDocUrl as String, Optional PathLen as Integer)
  307. Dim sViewPath as String
  308. Dim FileName as String
  309. Dim iFileLen as Integer
  310. sViewPath = ConvertfromURL(sDocURL)
  311. iViewPathLen = Len(sViewPath)
  312. If iViewPathLen &gt; 60 Then
  313. FileName = FileNameoutofPath(sViewPath, &quot;/&quot;)
  314. iFileLen = Len(FileName)
  315. If iFileLen &lt; 44 Then
  316. sViewPath = Left(sViewPath,57-iFileLen-10) &amp; &quot;...&quot; &amp; Right(sViewPath,iFileLen + 10)
  317. Else
  318. sViewPath = Left(sViewPath,27) &amp; &quot; ... &quot; &amp; Right(sViewPath,28)
  319. End If
  320. End If
  321. CutPathView = sViewPath
  322. End Function
  323. &apos; Deletes the content of all cells that are softformatted according
  324. &apos; to the &apos;InputStyleName&apos;
  325. Sub DeleteInputCells(oSheet as Object, InputStyleName as String)
  326. Dim oRanges as Object
  327. Dim oRange as Object
  328. oRanges = oSheet.CellFormatRanges.createEnumeration
  329. While oRanges.hasMoreElements
  330. oRange = oRanges.NextElement
  331. If Instr(1,oRange.CellStyle, InputStyleName) &lt;&gt; 0 Then
  332. Call ReplaceRangeValues(oRange, &quot;&quot;)
  333. End If
  334. Wend
  335. End Sub
  336. &apos; Inserts a certain string to all cells of a range that is passed
  337. &apos; either as an object or as the RangeName
  338. Sub ChangeValueofRange(oSheet as Object, Range, ReplaceValue, Optional StyleName as String)
  339. Dim oCellRange as Object
  340. If Vartype(Range) = 8 Then
  341. &apos; Get the Range out of the Rangename
  342. oCellRange = oSheet.GetCellRangeByName(Range)
  343. Else
  344. &apos; The range is passed as an object
  345. Set oCellRange = Range
  346. End If
  347. If IsMissing(StyleName) Then
  348. ReplaceRangeValues(oCellRange, ReplaceValue)
  349. Else
  350. If Instr(1,oCellRange.CellStyle,StyleName) Then
  351. ReplaceRangeValues(oCellRange, ReplaceValue)
  352. End If
  353. End If
  354. End Sub
  355. Sub ReplaceRangeValues(oRange as Object, ReplaceValue)
  356. Dim oRangeAddress as Object
  357. Dim ColCount as Integer
  358. Dim RowCount as Integer
  359. Dim i as Integer
  360. oRangeAddress = oRange.RangeAddress
  361. ColCount = oRangeAddress.EndColumn - oRangeAddress.StartColumn
  362. RowCount = oRangeAddress.EndRow - oRangeAddress.StartRow
  363. Dim FillArray(RowCount) as Variant
  364. Dim sLine(ColCount) as Variant
  365. For i = 0 To ColCount
  366. sLine(i) = ReplaceValue
  367. Next i
  368. For i = 0 To RowCount
  369. FillArray(i) = sLine()
  370. Next i
  371. oRange.DataArray = FillArray()
  372. End Sub
  373. &apos; Returns the Value of the first cell of a Range
  374. Function GetValueofCellbyName(oSheet as Object, sCellName as String)
  375. Dim oCell as Object
  376. oCell = GetCellByName(oSheet, sCellName)
  377. GetValueofCellbyName = oCell.Value
  378. End Function
  379. Function DuplicateRow(oSheet as Object, RangeName as String)
  380. Dim oRange as Object
  381. Dim oCell as Object
  382. Dim oCellAddress as New com.sun.star.table.CellAddress
  383. Dim oRangeAddress as New com.sun.star.table.CellRangeAddress
  384. oRange = oSheet.GetCellRangeByName(RangeName)
  385. oRangeAddress = oRange.RangeAddress
  386. oCell = oSheet.GetCellByPosition(oRangeAddress.StartColumn,oRangeAddress.StartRow)
  387. oCellAddress = oCell.CellAddress
  388. oSheet.Rows.InsertByIndex(oCellAddress.Row,1)
  389. oRangeAddress = oRange.RangeAddress
  390. oSheet.CopyRange(oCellAddress, oRangeAddress)
  391. DuplicateRow = oRangeAddress.StartRow-1
  392. End Function
  393. &apos; Returns the String of the first cell of a Range
  394. Function GetStringofCellbyName(oSheet as Object, sCellName as String)
  395. Dim oCell as Object
  396. oCell = GetCellByName(oSheet, sCellName)
  397. GetStringofCellbyName = oCell.String
  398. End Function
  399. &apos; Returns a named Cell
  400. Function GetCellByName(oSheet as Object, sCellName as String) as Object
  401. Dim oCellRange as Object
  402. Dim oCellAddress as Object
  403. oCellRange = oSheet.GetCellRangeByName(sCellName)
  404. oCellAddress = oCellRange.RangeAddress
  405. GetCellByName = oSheet.GetCellByPosition(oCellAddress.StartColumn,oCellAddress.StartRow)
  406. End Function
  407. &apos; Changes the numeric Value of a cell by transmitting the String of the numeric Value
  408. Sub ChangeCellValue(oCell as Object, ValueString as String)
  409. Dim CellValue
  410. oCell.Formula = &quot;=Value(&quot; &amp; &quot;&quot;&quot;&quot; &amp; ValueString &amp; &quot;&quot;&quot;&quot; &amp; &quot;)&quot;
  411. CellValue = oCell.Value
  412. oCell.Formula = &quot;&quot;
  413. oCell.Value = CellValue
  414. End Sub
  415. Function GetDocumentType(oDocument)
  416. On Local Error GoTo NODOCUMENTTYPE
  417. &apos; ShowSupportedServiceNames(oDocument)
  418. If oDocument.SupportsService(&quot;com.sun.star.sheet.SpreadsheetDocument&quot;) Then
  419. GetDocumentType() = &quot;scalc&quot;
  420. ElseIf oDocument.SupportsService(&quot;com.sun.star.text.TextDocument&quot;) Then
  421. GetDocumentType() = &quot;swriter&quot;
  422. ElseIf oDocument.SupportsService(&quot;com.sun.star.drawing.DrawingDocument&quot;) Then
  423. GetDocumentType() = &quot;sdraw&quot;
  424. ElseIf oDocument.SupportsService(&quot;com.sun.star.presentation.PresentationDocument&quot;) Then
  425. GetDocumentType() = &quot;simpress&quot;
  426. ElseIf oDocument.SupportsService(&quot;com.sun.star.formula.FormulaProperties&quot;) Then
  427. GetDocumentType() = &quot;smath&quot;
  428. End If
  429. NODOCUMENTTYPE:
  430. If Err &lt;&gt; 0 Then
  431. GetDocumentType = &quot;&quot;
  432. Resume GOON
  433. GOON:
  434. End If
  435. End Function
  436. Function GetNumberFormatType(oDocFormats, oFormatObject as Object) as Integer
  437. Dim ThisFormatKey as Long
  438. Dim oObjectFormat as Object
  439. On Local Error Goto NOFORMAT
  440. ThisFormatKey = oFormatObject.NumberFormat
  441. oObjectFormat = oDocFormats.GetByKey(ThisFormatKey)
  442. GetNumberFormatType = oObjectFormat.Type
  443. NOFORMAT:
  444. If Err &lt;&gt; 0 Then
  445. Msgbox(&quot;Numberformat of Object is not available!&quot;, 16, GetProductName())
  446. GetNumberFormatType = 0
  447. GOTO NOERROR
  448. End If
  449. NOERROR:
  450. On Local Error Goto 0
  451. End Function
  452. Sub ProtectSheets(Optional oSheets as Object)
  453. Dim i as Integer
  454. Dim oDocSheets as Object
  455. If IsMissing(oSheets) Then
  456. oDocSheets = StarDesktop.CurrentFrame.Controller.Model.Sheets
  457. Else
  458. Set oDocSheets = oSheets
  459. End If
  460. For i = 0 To oDocSheets.Count-1
  461. oDocSheets(i).Protect(&quot;&quot;)
  462. Next i
  463. End Sub
  464. Sub UnprotectSheets(Optional oSheets as Object)
  465. Dim i as Integer
  466. Dim oDocSheets as Object
  467. If IsMissing(oSheets) Then
  468. oDocSheets = StarDesktop.CurrentFrame.Controller.Model.Sheets
  469. Else
  470. Set oDocSheets = oSheets
  471. End If
  472. For i = 0 To oDocSheets.Count-1
  473. oDocSheets(i).Unprotect(&quot;&quot;)
  474. Next i
  475. End Sub
  476. Function GetRowIndex(oSheet as Object, RowName as String)
  477. Dim oRange as Object
  478. oRange = oSheet.GetCellRangeByName(RowName)
  479. GetRowIndex = oRange.RangeAddress.StartRow
  480. End Function
  481. Function GetColumnIndex(oSheet as Object, ColName as String)
  482. Dim oRange as Object
  483. oRange = oSheet.GetCellRangeByName(ColName)
  484. GetColumnIndex = oRange.RangeAddress.StartColumn
  485. End Function
  486. Function CopySheetbyName(oSheets as Object, OldName as String, NewName as String, DestPos as Integer) as Object
  487. Dim oSheet as Object
  488. Dim Count as Integer
  489. Dim BasicSheetName as String
  490. BasicSheetName = NewName
  491. &apos; Copy the last table. Assumption: The last table is the template
  492. On Local Error Goto RENAMESHEET
  493. oSheets.CopybyName(OldName, NewName, DestPos)
  494. RENAMESHEET:
  495. oSheet = oSheets(DestPos)
  496. If Err &lt;&gt; 0 Then
  497. &apos; Test if renaming failed
  498. Count = 2
  499. Do While oSheet.Name &lt;&gt; NewName
  500. NewName = BasicSheetName &amp; &quot;_&quot; &amp; Count
  501. oSheet.Name = NewName
  502. Count = Count + 1
  503. Loop
  504. Resume CL_ERROR
  505. CL_ERROR:
  506. End If
  507. CopySheetbyName = oSheet
  508. End Function
  509. &apos; Dis-or enables a Window and adjusts the mousepointer accordingly
  510. Sub ToggleWindow(bDoEnable as Boolean)
  511. Dim oWindow as Object
  512. oWindow = StarDesktop.CurrentFrame.ComponentWindow
  513. oWindow.Enable = bDoEnable
  514. End Sub
  515. Function CheckNewSheetname(oSheets as Object, Sheetname as String, Optional oLocale) as String
  516. Dim nStartFlags as Long
  517. Dim nContFlags as Long
  518. Dim oCharService as Object
  519. Dim iSheetNameLength as Integer
  520. Dim iResultPos as Integer
  521. Dim WrongChar as String
  522. Dim oResult as Object
  523. nStartFlags = com.sun.star.i18n.KParseTokens.ANY_LETTER_OR_NUMBER + com.sun.star.i18n.KParseTokens.ASC_UNDERSCORE
  524. nContFlags = nStartFlags
  525. oCharService = CreateUnoService(&quot;com.sun.star.i18n.CharacterClassification&quot;)
  526. iSheetNameLength = Len(SheetName)
  527. If IsMissing(oLocale) Then
  528. oLocale = ThisComponent.CharLocale
  529. End If
  530. Do
  531. oResult =oCharService.parsePredefinedToken(com.sun.star.i18n.KParseType.IDENTNAME, SheetName, 0, oLocale, nStartFlags, &quot;&quot;, nContFlags, &quot; &quot;)
  532. iResultPos = oResult.EndPos
  533. If iResultPos &lt; iSheetNameLength Then
  534. WrongChar = Mid(SheetName, iResultPos+1,1)
  535. SheetName = ReplaceString(SheetName,&quot;_&quot;, WrongChar)
  536. End If
  537. Loop Until iResultPos = iSheetNameLength
  538. CheckNewSheetname = SheetName
  539. End Function
  540. Sub AddNewSheetName(oSheets as Object, ByVal SheetName as String)
  541. Dim Count as Integer
  542. Dim bSheetIsThere as Boolean
  543. Dim iSheetNameLength as Integer
  544. iSheetNameLength = Len(SheetName)
  545. Count = 2
  546. Do
  547. bSheetIsThere = oSheets.HasByName(SheetName)
  548. If bSheetIsThere Then
  549. SheetName = Right(SheetName,iSheetNameLength) &amp; &quot;_&quot; &amp; Count
  550. Count = Count + 1
  551. End If
  552. Loop Until Not bSheetIsThere
  553. AddNewSheetname = SheetName
  554. End Sub
  555. Function GetSheetIndex(oSheets, sName) as Integer
  556. Dim i as Integer
  557. For i = 0 To oSheets.Count-1
  558. If oSheets(i).Name = sName Then
  559. GetSheetIndex = i
  560. exit Function
  561. End If
  562. Next i
  563. GetSheetIndex = -1
  564. End Function
  565. Function GetLastUsedRow(oSheet as Object) as Long
  566. Dim oCell As Object
  567. Dim oCursor As Object
  568. Dim aAddress As Variant
  569. oCell = oSheet.GetCellbyPosition(0, 0)
  570. oCursor = oSheet.createCursorByRange(oCell)
  571. oCursor.GotoEndOfUsedArea(True)
  572. aAddress = oCursor.RangeAddress
  573. GetLastUsedRow = aAddress.EndRow
  574. End Function
  575. &apos; Note To set a one lined frame you have to set the inner width to 0
  576. &apos; In the API all Units that refer to pt-Heights are &quot;1/100mm&quot;
  577. &apos; The convert factor from 1pt to 1/100 mm is approximately 35
  578. Function ModifyBorderLineWidth(ByVal oStyleBorder, iInnerLineWidth as Integer, iOuterLineWidth as Integer)
  579. Dim aBorder as New com.sun.star.table.BorderLine
  580. aBorder = oStyleBorder
  581. aBorder.InnerLineWidth = iInnerLineWidth
  582. aBorder.OuterLineWidth = iOuterLineWidth
  583. ModifyBorderLineWidth = aBorder
  584. End Function
  585. Sub AttachBasicMacroToEvent(oDocument as Object, EventName as String, SubPath as String)
  586. Dim PropValue(1) as new com.sun.star.beans.PropertyValue
  587. PropValue(0).Name = &quot;EventType&quot;
  588. PropValue(0).Value = &quot;StarBasic&quot;
  589. PropValue(1).Name = &quot;Script&quot;
  590. PropValue(1).Value = &quot;macro:///&quot; &amp; SubPath
  591. oDocument.Events.ReplaceByName(EventName, PropValue())
  592. End Sub
  593. Function ModifyPropertyValue(oContent() as New com.sun.star.beans.PropertyValue, TargetProperties() as New com.sun.star.beans.PropertyValue)
  594. Dim MaxIndex as Integer
  595. Dim i as Integer
  596. Dim a as Integer
  597. MaxIndex = Ubound(oContent())
  598. bDoReplace = False
  599. For i = 0 To MaxIndex
  600. a = GetPropertyValueIndex(oContent(i).Name, TargetProperties())
  601. If a &lt;&gt; -1 Then
  602. If Vartype(TargetProperties(a).Value) &lt;&gt; 9 Then
  603. If TargetProperties(a).Value &lt;&gt; oContent(i).Value Then
  604. oContent(i).Value = TargetProperties(a).Value
  605. bDoReplace = True
  606. End If
  607. Else
  608. If Not EqualUnoObjects(TargetProperties(a).Value, oContent(i).Value) Then
  609. oContent(i).Value = TargetProperties(a).Value
  610. bDoReplace = True
  611. End If
  612. End If
  613. End If
  614. Next i
  615. ModifyPropertyValue() = bDoReplace
  616. End Function
  617. Function GetPropertyValueIndex(SearchName as String, TargetProperties() as New com.sun.star.beans.PropertyValue ) as Integer
  618. Dim i as Integer
  619. For i = 0 To Ubound(TargetProperties())
  620. If Searchname = TargetProperties(i).Name Then
  621. GetPropertyValueIndex = i
  622. Exit Function
  623. End If
  624. Next i
  625. GetPropertyValueIndex() = -1
  626. End Function
  627. Sub DispatchSlot(SlotID as Integer)
  628. Dim oArg() as new com.sun.star.beans.PropertyValue
  629. Dim oUrl as new com.sun.star.util.URL
  630. Dim oTrans as Object
  631. Dim oDisp as Object
  632. oTrans = createUNOService(&quot;com.sun.star.util.URLTransformer&quot;)
  633. oUrl.Complete = &quot;slot:&quot; &amp; CStr(SlotID)
  634. oTrans.parsestrict(oUrl)
  635. oDisp = StarDesktop.ActiveFrame.queryDispatch(oUrl, &quot;_self&quot;, 0)
  636. oDisp.dispatch(oUrl, oArg())
  637. End Sub
  638. &apos;returns the type of the office application
  639. &apos;FatOffice = 0, WebTop = 1
  640. &apos;This routine has to be changed if the Product Name is being changed!
  641. Function IsFatOffice() As Boolean
  642. If sProductname = &quot;&quot; Then
  643. sProductname = GetProductname()
  644. End If
  645. IsFatOffice = TRUE
  646. &apos;The following line has to include the current productname
  647. If Instr(1,sProductname,&quot;WebTop&quot;,1) &lt;&gt; 0 Then
  648. IsFatOffice = FALSE
  649. End If
  650. End Function
  651. Sub ToggleDesignMode(oDocument as Object)
  652. Dim aSwitchMode as new com.sun.star.util.URL
  653. aSwitchMode.Complete = &quot;.uno:SwitchControlDesignMode&quot;
  654. aTransformer = createUnoService(&quot;com.sun.star.util.URLTransformer&quot;)
  655. aTransformer.parseStrict(aSwitchMode)
  656. oFrame = oDocument.currentController.Frame
  657. oDispatch = oFrame.queryDispatch(aSwitchMode, oFrame.Name, 63)
  658. Dim aEmptyArgs() as New com.sun.star.bean.PropertyValue
  659. oDispatch.dispatch(aSwitchMode, aEmptyArgs())
  660. Erase aSwitchMode
  661. End Sub
  662. Function isHighContrast(oPeer as Object)
  663. Dim UIColor as Long
  664. Dim myRed as Integer
  665. Dim myGreen as Integer
  666. Dim myBlue as Integer
  667. Dim myLuminance as Double
  668. UIColor = oPeer.getProperty( &quot;DisplayBackgroundColor&quot; )
  669. myRed = Red (UIColor)
  670. myGreen = Green (UIColor)
  671. myBlue = Blue (UIColor)
  672. myLuminance = (( myBlue*28 + myGreen*151 + myRed*77 ) / 256 )
  673. isHighContrast = false
  674. If myLuminance &lt;= 25 Then isHighContrast = true
  675. End Function
  676. Function CreateNewDocument(sType as String, Optional sAddMsg as String) as Object
  677. Dim NoArgs() as new com.sun.star.beans.PropertyValue
  678. Dim oDocument as Object
  679. Dim sUrl as String
  680. Dim ErrMsg as String
  681. On Local Error Goto NOMODULEINSTALLED
  682. sUrl = &quot;private:factory/&quot; &amp; sType
  683. oDocument = StarDesktop.LoadComponentFromURL(sUrl,&quot;_default&quot;,0, NoArgs())
  684. NOMODULEINSTALLED:
  685. If (Err &lt;&gt; 0) OR IsNull(oDocument) Then
  686. If InitResources(&quot;&quot;) Then
  687. Select Case sType
  688. Case &quot;swriter&quot;
  689. ErrMsg = GetResText(&quot;RID_COMMON_1&quot;)
  690. Case &quot;scalc&quot;
  691. ErrMsg = GetResText(&quot;RID_COMMON_2&quot;)
  692. Case &quot;simpress&quot;
  693. ErrMsg = GetResText(&quot;RID_COMMON_3&quot;)
  694. Case &quot;sdraw&quot;
  695. ErrMsg = GetResText(&quot;RID_COMMON_4&quot;)
  696. Case &quot;smath&quot;
  697. ErrMsg = GetResText(&quot;RID_COMMON_5&quot;)
  698. Case Else
  699. ErrMsg = &quot;Invalid Document Type!&quot;
  700. End Select
  701. ErrMsg = ReplaceString(ErrMsg, chr(13), &quot;&lt;BR&gt;&quot;)
  702. If Not IsMissing(sAddMsg) Then
  703. ErrMsg = ErrMsg &amp; chr(13) &amp; sAddMsg
  704. End If
  705. Msgbox(ErrMsg, 48, GetProductName())
  706. End If
  707. If Err &lt;&gt; 0 Then
  708. Resume GOON
  709. End If
  710. End If
  711. GOON:
  712. CreateNewDocument = oDocument
  713. End Function
  714. &apos; This Sub has been used in order to ensure that after disposing a document
  715. &apos; from the backing window it is returned to the backing window, so the
  716. &apos; office won&apos;t be closed
  717. Sub DisposeDocument(oDocument as Object)
  718. Dim dispatcher as Object
  719. Dim parser as Object
  720. Dim disp as Object
  721. Dim url as new com.sun.star.util.URL
  722. Dim NoArgs() as New com.sun.star.beans.PropertyValue
  723. Dim oFrame as Object
  724. If Not IsNull(oDocument) Then
  725. oDocument.setModified(false)
  726. parser = createUnoService(&quot;com.sun.star.util.URLTransformer&quot;)
  727. url.Complete = &quot;.uno:CloseDoc&quot;
  728. parser.parseStrict(url)
  729. oFrame = oDocument.CurrentController.Frame
  730. disp = oFrame.queryDispatch(url,&quot;_self&quot;, com.sun.star.util.SearchFlags.NORM_WORD_ONLY)
  731. disp.dispatch(url, NoArgs())
  732. End If
  733. End Sub
  734. &apos;Function to calculate if the year is a leap year
  735. Function CalIsLeapYear(ByVal iYear as Integer) as Boolean
  736. CalIsLeapYear = ((iYear Mod 4 = 0) And ((iYear Mod 100 &lt;&gt; 0) Or (iYear Mod 400 = 0)))
  737. End Function
  738. </script:module>