Bump version to 4.1-6
[LibreOffice.git] / wizards / source / template / Correspondence.xba
blob0d98846436a1139b773a93bf7fd36a1458c3324c
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="Correspondence" script:language="StarBasic">Option Explicit
22 Public msgNoTextmark$, msgError$
23 Public sAddressbook$
24 Public Table
25 Public sCompany$, sFirstName$, sLastName$, sStreet$, sPostalCode$, sCity$, sState$, sInitials$, sPosition$
26 Public DialogExited
27 Public oDocument, oText, oBookMarks, oBookMark, oBookMarkCursor, oBookText as Object
28 Public bTemplate, bDBFields as Boolean
30 Sub Main
31 bTemplate = true
32 BasicLibraries.LoadLibrary(&quot;Tools&quot;)
33 TemplateDialog = LoadDialog(&quot;Template&quot;, &quot;TemplateDialog&quot;)
34 DialogModel = TemplateDialog.Model
35 DialogModel.Step = 2
36 DialogModel.Optmerge.State = True
37 LoadLanguageCorrespondence()
38 TemplateDialog.Execute
39 TemplateDialog.Dispose()
40 End Sub
43 Sub Placeholder
44 bTemplate = false
45 BasicLibraries.LoadLibrary(&quot;Tools&quot;)
46 LoadLanguageCorrespondence()
47 bDBFields = false
48 OK()
49 End Sub
52 Sub Database
53 bTemplate = false
54 BasicLibraries.LoadLibrary(&quot;Tools&quot;)
55 LoadLanguageCorrespondence()
56 bDBFields = true
57 OK()
58 End Sub
61 Function LoadLanguageCorrespondence() as Boolean
62 If InitResources(&quot;&apos;Template&apos;&quot;, &quot;tpl&quot;) Then
63 msgNoTextmark$ = GetResText(1303) &amp; Chr(13) &amp; Chr(10) &amp; GetResText(1301)
64 msgError$ = GetResText(1302)
65 If bTemplate Then
66 DialogModel.Title = GetResText(1303+3)
67 DialogModel.CmdCancel.Label = GetResText(1102)
68 DialogModel.CmdCorrGoOn.Label = GetResText(1103)
69 DialogModel.OptSingle.Label = GetResText(1303 + 1)
70 DialogModel.Optmerge.Label = GetResText(1303 + 2)
71 DialogModel.FrmLetter.Label = GetResText(1303)
72 End If
73 LoadLanguageCorrespondence() = True
74 Else
75 msgbox(&quot;Warning: Resource could not be loaded!&quot;)
76 End If
77 End Function
80 Function GetFieldName(oFieldKnot as Object, GeneralFieldName as String)
81 If oFieldKnot.HasByName(GeneralFieldName) Then
82 GetFieldName = oFieldKnot.GetByName(GeneralFieldName).AssignedFieldName
83 Else
84 GetFieldName = &quot;&quot;
85 End If
86 End Function
89 Sub OK
90 Dim ParaBreak
91 Dim sDocLang as String
92 Dim oSearchDesc as Object
93 Dim oFoundAll as Object
94 Dim oFound as Object
95 Dim sFoundContent as String
96 Dim sFoundString as String
97 Dim sDBField as String
98 Dim i as Integer
99 Dim oDBAccess as Object
100 Dim oAddressDialog as Object
101 Dim oAddressPilot as Object
102 Dim oFields as Object
103 Dim oDocSettings as Object
104 Dim oContext as Object
105 Dim bDBvalid as Boolean
106 &apos;On Local Error Goto GENERALERROR
108 If bTemplate Then
109 bDBFields = DialogModel.Optmerge.State &apos;database or placeholder
110 TemplateDialog.EndExecute()
111 DialogExited = TRUE
112 End If
114 If bDBFields Then
115 oDBAccess = GetRegistryKeyContent(&quot;org.openoffice.Office.DataAccess/AddressBook/&quot;)
116 sAddressbook = oDBAccess.DataSourceName
118 bDBvalid = false
119 oContext = createUnoService( &quot;com.sun.star.sdb.DatabaseContext&quot; )
121 If (not isNull(oContext)) Then
122 &apos;Is the previously assigned address data source still valid?
123 bDBvalid = oContext.hasByName(sAddressbook)
124 end if
126 If (bDBvalid = false) Then
127 oAddressPilot = createUnoService(&quot;com.sun.star.ui.dialogs.AddressBookSourcePilot&quot;)
128 oAddressPilot.execute
130 oDBAccess = GetRegistryKeyContent(&quot;org.openoffice.Office.DataAccess/AddressBook/&quot;)
131 sAddressbook = oDBAccess.DataSourceName
132 If sAddressbook = &quot;&quot; Then
133 MsgBox(GetResText(1301))
134 Exit Sub
135 End If
136 End If
137 oFields = oDBAccess.GetByName(&quot;Fields&quot;)
138 Table = oDBAccess.GetByName(&quot;Command&quot;)
139 End If
141 ParaBreak = com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK
142 oDocument = ThisComponent
143 If bDBFields Then
144 &apos;set the address db as current db at the document
145 oDocSettings = oDocument.createInstance(&quot;com.sun.star.document.Settings&quot;)
146 oDocSettings.CurrentDatabaseDataSource = sAddressbook
147 oDocSettings.CurrentDatabaseCommand = Table
148 oDocSettings.CurrentDatabaseCommandType = 0
149 End If
150 oBookmarks = oDocument.Bookmarks
151 oText = oDocument.Text
153 oSearchDesc = oDocument.createsearchDescriptor()
154 oSearchDesc.SearchRegularExpression = True
155 oSearchDesc.SearchWords = True
156 oSearchDesc.SearchString = &quot;&lt;[^&gt;]+&gt;&quot;
157 oFoundall = oDocument.FindAll(oSearchDesc)
159 &apos;Loop over the foundings
160 For i = oFoundAll.Count -1 To 0 Step -1
161 oFound = oFoundAll.GetByIndex(i)
162 sFoundString = oFound.String
163 &apos;Extract the string inside the brackets
164 sFoundContent = FindPartString(sFoundString,&quot;&lt;&quot;,&quot;&gt;&quot;,1)
165 sFoundContent = LTrim(sFoundContent)
166 &apos; Define the Cursor and place it on the founding
167 oBookmarkCursor = oFound.Text.CreateTextCursorbyRange(oFound)
168 oBookText = oFound.Text
169 If bDBFields Then
170 sDBField = GetFieldname(oFields, sFoundContent)
171 If sDBField &lt;&gt; &quot;&quot; Then
172 InsertDBField(sAddressbook, Table, sDBField)
173 Else
174 InsertPlaceholder(sFoundContent)
175 End If
176 Else
177 InsertPlaceholder(sFoundContent)
178 End If
179 Next i
180 If bDBFields Then
181 &apos;Open the DB beamer with the right DB
182 Dim oDisp as Object
183 Dim oTransformer
184 Dim aURL as new com.sun.star.util.URL
185 aURL.complete = &quot;.component:DB/DataSourceBrowser&quot;
186 oTransformer = createUnoService(&quot;com.sun.star.util.URLTransformer&quot;)
187 oTransformer.parseStrict(aURL)
188 oDisp = oDocument.getCurrentController.getFrame.queryDispatch(aURL, &quot;_beamer&quot;, com.sun.star.frame.FrameSearchFlag.CHILDREN + com.sun.star.frame.FrameSearchFlag.CREATE)
189 Dim aArgs(3) as new com.sun.star.beans.PropertyValue
190 aArgs(1).Name = &quot;DataSourceName&quot;
191 aArgs(1).Value = sAddressbook
192 aArgs(2).Name = &quot;CommandType&quot;
193 aArgs(2).Value = com.sun.star.sdb.CommandType.TABLE
194 aArgs(3).Name = &quot;Command&quot;
195 aArgs(3).Value = Table
196 oDisp.dispatch(aURL, aArgs())
197 End If
199 GENERALERROR:
200 If Err &lt;&gt; 0 Then
201 Msgbox(msgError$,16, GetProductName())
202 Resume LETSGO
203 End If
204 LETSGO:
206 End Sub
209 Sub InsertDBField(sDBName as String, sTableName as String, sColName as String)
210 Dim oFieldMaster, oField as Object
211 If sColname &lt;&gt; &quot;&quot; Then
212 oFieldMaster = oDocument.createInstance(&quot;com.sun.star.text.FieldMaster.Database&quot;)
213 oField = oDocument.createInstance(&quot;com.sun.star.text.TextField.Database&quot;)
214 oFieldMaster.DataBaseName = sDBName
215 oFieldMaster.DataBaseName = sDBName
216 oFieldMaster.DataTableName = sTableName
217 oFieldMaster.DataColumnName = sColName
218 oField.AttachTextfieldmaster (oFieldMaster)
219 oBookText.InsertTextContent(oBookMarkCursor, oField, True)
220 oField.Content = &quot;&lt;&quot; &amp; sColName &amp; &quot;&gt;&quot;
221 End If
222 End Sub
225 Sub InsertPlaceholder(sColName as String)
226 Dim oFieldMaster as Object
227 Dim bCorrectField as Boolean
228 If sColname &lt;&gt; &quot;&quot; Then
229 bCorrectField = True
230 oFieldMaster = oDocument.createInstance(&quot;com.sun.star.text.TextField.JumpEdit&quot;)
231 Select Case sColName
232 Case &quot;Company&quot;
233 oFieldMaster.PlaceHolder = getResText(1350+1)
234 Case &quot;Department&quot;
235 oFieldMaster.PlaceHolder = getResText(1350+2)
236 Case &quot;FirstName&quot;
237 oFieldMaster.PlaceHolder = getResText(1350+3)
238 Case &quot;LastName&quot;
239 oFieldMaster.PlaceHolder = getResText(1350+4)
240 Case &quot;Street&quot;
241 oFieldMaster.PlaceHolder = getResText(1350+5)
242 Case &quot;Country&quot;
243 oFieldMaster.PlaceHolder = getResText(1350+6)
244 Case &quot;Zip&quot;
245 oFieldMaster.PlaceHolder = getResText(1350+7)
246 Case &quot;City&quot;
247 oFieldMaster.PlaceHolder = getResText(1350+8)
248 Case &quot;Title&quot;
249 oFieldMaster.PlaceHolder = getResText(1350+9)
250 Case &quot;Position&quot;
251 oFieldMaster.PlaceHolder = getResText(1350+10)
252 Case &quot;AddrForm&quot;
253 oFieldMaster.PlaceHolder = getResText(1350+11)
254 Case &quot;Code&quot;
255 oFieldMaster.PlaceHolder = getResText(1350+12)
256 Case &quot;AddrFormMail&quot;
257 oFieldMaster.PlaceHolder = getResText(1350+13)
258 Case &quot;PhonePriv&quot;
259 oFieldMaster.PlaceHolder = getResText(1350+14)
260 Case &quot;PhoneComp&quot;
261 oFieldMaster.PlaceHolder = getResText(1350+15)
262 Case &quot;Fax&quot;
263 oFieldMaster.PlaceHolder = getResText(1350+16)
264 Case &quot;EMail&quot;
265 oFieldMaster.PlaceHolder = getResText(1350+17)
266 Case &quot;URL&quot;
267 oFieldMaster.PlaceHolder = getResText(1350+18)
268 Case &quot;Note&quot;
269 oFieldMaster.PlaceHolder = getResText(1350+19)
270 Case &quot;Altfield1&quot;
271 oFieldMaster.PlaceHolder = getResText(1350+20)
272 Case &quot;Altfield2&quot;
273 oFieldMaster.PlaceHolder = getResText(1350+21)
274 Case &quot;Altfield3&quot;
275 oFieldMaster.PlaceHolder = getResText(1350+22)
276 Case &quot;Altfield4&quot;
277 oFieldMaster.PlaceHolder = getResText(1350+23)
278 Case &quot;Id&quot;
279 oFieldMaster.PlaceHolder = getResText(1350+24)
280 Case &quot;State&quot;
281 oFieldMaster.PlaceHolder = getResText(1350+25)
282 Case &quot;PhoneOffice&quot;
283 oFieldMaster.PlaceHolder = getResText(1350+26)
284 Case &quot;Pager&quot;
285 oFieldMaster.PlaceHolder = getResText(1350+27)
286 Case &quot;PhoneCell&quot;
287 oFieldMaster.PlaceHolder = getResText(1350+28)
288 Case &quot;PhoneOther&quot;
289 oFieldMaster.PlaceHolder = getResText(1350+29)
290 Case &quot;CalendarURL&quot;
291 oFieldMaster.PlaceHolder = getResText(1350+30)
292 Case &quot;InviteParticipant&quot;
293 oFieldMaster.PlaceHolder = getResText(1350+31)
294 Case Else
295 bCorrectField = False
296 End Select
297 If bCorrectField Then
298 oFieldMaster.Hint = getResText(1350)
299 oBookText.InsertTextContent(oBookMarkCursor, oFieldMaster, True)
300 End If
301 End If
302 End Sub
303 </script:module>