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