Autotext.xba 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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="Autotext" script:language="StarBasic">Option Explicit
  21. Public UserfieldDataType(14) as String
  22. Public oDocAuto as Object
  23. Public BulletList(7) as Integer
  24. Public sTextFieldNotDefined as String
  25. Public sGeneralError as String
  26. Sub Main()
  27. Dim oCursor as Object
  28. Dim oStyles as Object
  29. Dim oSearchDesc as Object
  30. Dim oFoundall as Object
  31. Dim oFound as Object
  32. Dim i as Integer
  33. Dim sFoundString as String
  34. Dim sFoundContent as String
  35. Dim FieldStringThere as String
  36. Dim ULStringThere as String
  37. Dim PHStringThere as String
  38. On Local Error Goto GENERALERROR
  39. &apos; Initialization...
  40. BasicLibraries.LoadLibrary(&quot;Tools&quot;)
  41. If InitResources(&quot;&apos;Template&apos;&quot;) Then
  42. sGeneralError = GetResText(&quot;CorrespondenceMsgError&quot;)
  43. sTextFieldNotDefined = GetResText(&quot;TextField&quot;)
  44. End If
  45. UserfieldDatatype(0) = &quot;COMPANY&quot;
  46. UserfieldDatatype(1) = &quot;FIRSTNAME&quot;
  47. UserfieldDatatype(2) = &quot;NAME&quot;
  48. UserfieldDatatype(3) = &quot;SHORTCUT&quot;
  49. UserfieldDatatype(4) = &quot;STREET&quot;
  50. UserfieldDatatype(5) = &quot;COUNTRY&quot;
  51. UserfieldDatatype(6) = &quot;ZIP&quot;
  52. UserfieldDatatype(7) = &quot;CITY&quot;
  53. UserfieldDatatype(8) = &quot;TITLE&quot;
  54. UserfieldDatatype(9) = &quot;POSITION&quot;
  55. UserfieldDatatype(10) = &quot;PHONE_PRIVATE&quot;
  56. UserfieldDatatype(11) = &quot;PHONE_COMPANY&quot;
  57. UserfieldDatatype(12) = &quot;FAX&quot;
  58. UserfieldDatatype(13) = &quot;EMAIL&quot;
  59. UserfieldDatatype(14) = &quot;STATE&quot;
  60. BulletList(0) = 149
  61. BulletList(1) = 34
  62. BulletList(2) = 65
  63. BulletList(3) = 61
  64. BulletList(4) = 49
  65. BulletList(5) = 47
  66. BulletList(6) = 79
  67. BulletList(7) = 58
  68. oDocAuto = ThisComponent
  69. oStyles = oDocAuto.Stylefamilies.GetByName(&quot;NumberingStyles&quot;)
  70. &apos; Prepare the Search-Descriptor
  71. oSearchDesc = oDocAuto.createsearchDescriptor()
  72. oSearchDesc.SearchRegularExpression = True
  73. oSearchDesc.SearchWords = True
  74. oSearchDesc.SearchString = &quot;&lt;[^&gt;]+&gt;&quot;
  75. oFoundall = oDocAuto.FindAll(oSearchDesc)
  76. &apos;Loop over the foundings
  77. For i = 0 To oFoundAll.Count - 1
  78. oFound = oFoundAll.GetByIndex(i)
  79. sFoundString = oFound.String
  80. &apos;Extract the string inside the brackets
  81. sFoundContent = FindPartString(sFoundString,&quot;&lt;&quot;,&quot;&gt;&quot;,1)
  82. sFoundContent = LTrim(sFoundContent)
  83. &apos; Define the Cursor and place it on the founding
  84. oCursor = oFound.Text.CreateTextCursorbyRange(oFound)
  85. &apos; Find out, which object is to be created...
  86. FieldStringThere = Instr(1,sFoundContent,&quot;Field&quot;)
  87. ULStringThere = Instr(1,sFoundContent,&quot;UL&quot;)
  88. PHStringThere = Instr(1,sFoundContent,&quot;Placeholder&quot;)
  89. If FieldStringThere = 1 Then
  90. CreateUserDatafield(oCursor, sFoundContent)
  91. ElseIf ULStringThere = 1 Then
  92. CreateBullet(oCursor, oStyles)
  93. ElseIf PHStringThere = 1 Then
  94. CreatePlaceholder(oCursor, sFoundContent)
  95. End If
  96. Next i
  97. GENERALERROR:
  98. If Err &lt;&gt; 0 Then
  99. Msgbox(sGeneralError,16, GetProductName())
  100. Resume LETSGO
  101. End If
  102. LETSGO:
  103. End Sub
  104. &apos; creates a User - datafield out of a string with the following structure
  105. &apos; &quot;&lt;field:Company&gt;&quot;
  106. Sub CreateUserDatafield(oCursor, sFoundContent as String)
  107. Dim MaxIndex as Integer
  108. Dim sFoundList(3)
  109. Dim oUserfield as Object
  110. Dim UserInfo as String
  111. Dim UserIndex as Integer
  112. oUserfield = oDocAuto.CreateInstance(&quot;com.sun.star.text.TextField.ExtendedUser&quot;)
  113. sFoundList() = ArrayoutofString(sFoundContent,&quot;:&quot;,MaxIndex)
  114. UserInfo = UCase(LTrim(sFoundList(1)))
  115. UserIndex = IndexInArray(UserInfo, UserfieldDatatype())
  116. If UserIndex &lt;&gt; -1 Then
  117. oUserField.UserDatatype = UserIndex
  118. oCursor.Text.InsertTextContent(oCursor,oUserField,True)
  119. oUserField.IsFixed = True
  120. Else
  121. Msgbox(UserInfo &amp;&quot;: &quot; &amp; sTextFieldNotDefined,16, GetProductName())
  122. End If
  123. End Sub
  124. &apos; Creates a Bullet by setting a soft Formatation on the first unsorted List-Templates with a defined
  125. &apos; Bullet Id
  126. Sub CreateBullet(oCursor, oStyles as Object)
  127. Dim n, m, s as Integer
  128. Dim StyleSet as Boolean
  129. Dim ostyle as Object
  130. Dim StyleName as String
  131. Dim alevel()
  132. StyleSet = False
  133. For s = 0 To Ubound(BulletList())
  134. For n = 0 To oStyles.Count - 1
  135. ostyle = oStyles.getbyindex(n)
  136. StyleName = oStyle.Name
  137. alevel() = ostyle.NumberingRules.getbyindex(0)
  138. &apos; The properties of the style are stored in a Name-Value-Array()
  139. For m = 0 to Ubound(alevel())
  140. &apos; Set the first Numbering template without a bulletID
  141. If (aLevel(m).Name = &quot;BulletId&quot;) Then
  142. If alevel(m).Value = BulletList(s) Then
  143. oCursor.NumberingStyle = StyleName
  144. oCursor.SetString(&quot;&quot;)
  145. exit Sub
  146. End if
  147. End If
  148. Next m
  149. Next n
  150. Next s
  151. If Not StyleSet Then
  152. &apos; The Template with the demanded BulletID is not available, so take the first style in the sequence
  153. &apos; that has a defined Bullet ID
  154. oCursor.NumberingStyleName = oStyles.GetByIndex(5).Name
  155. oCursor.SetString(&quot;&quot;)
  156. End If
  157. End Sub
  158. &apos; Creates a placeholder out of a string with the following structure:
  159. &apos;&lt;placeholder:Showtext:Helptext&gt;
  160. Sub CreatePlaceholder(oCursor as Object, sFoundContent as String)
  161. Dim oPlaceholder as Object
  162. Dim MaxIndex as Integer
  163. Dim sFoundList(3)
  164. oPlaceholder = oDocAuto.CreateInstance(&quot;com.sun.star.text.TextField.JumpEdit&quot;)
  165. sFoundList() = ArrayoutofString(sFoundContent, &quot;:&quot; &amp; chr(34),MaxIndex)
  166. &apos; Delete The Double-quotes
  167. oPlaceholder.Hint = DeleteStr(sFoundList(2),chr(34))
  168. oPlaceholder.placeholder = DeleteStr(sFoundList(1),chr(34))
  169. oCursor.Text.InsertTextContent(oCursor,oPlaceholder,True)
  170. End Sub
  171. </script:module>