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