Misc.xba 26 KB

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