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