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