summaryrefslogtreecommitdiff
path: root/wizards/source/template/Correspondence.xba
blob: 03c7ef77fbe8cd483b0c1f3abd7a4bb0fe9362b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Correspondence" script:language="StarBasic">Option Explicit

Public msgNoTextmark$, msgError$
Public sAddressbook$
Public Table
Public sCompany$, sFirstName$, sLastName$, sStreet$, sPostalCode$, sCity$, sState$, sInitials$, sPosition$
Public DialogExited
Public oDocument, oText, oBookMarks, oBookMark, oBookMarkCursor, oBookText as Object
Public bTemplate, bDBFields as Boolean

Sub Main
	bTemplate = true
    BasicLibraries.LoadLibrary(&quot;Tools&quot;)
	TemplateDialog = LoadDialog(&quot;Template&quot;, &quot;TemplateDialog&quot;)
	DialogModel = TemplateDialog.Model
	DialogModel.Step = 2
	DialogModel.Optmerge.State = True
	LoadLanguageCorrespondence()	
	TemplateDialog.Execute
	TemplateDialog.Dispose()
End Sub


Sub Placeholder
	bTemplate = false
	BasicLibraries.LoadLibrary(&quot;Tools&quot;)
	LoadLanguageCorrespondence()
	bDBFields = false
	OK()
End Sub


Sub Database
	bTemplate = false
	BasicLibraries.LoadLibrary(&quot;Tools&quot;)
	LoadLanguageCorrespondence()
	bDBFields = true
	OK()
End Sub


Function LoadLanguageCorrespondence() as Boolean
	If InitResources(&quot;&apos;Template&apos;&quot;, &quot;tpl&quot;) Then
		msgNoTextmark$ = GetResText(1303) &amp; Chr(13) &amp; Chr(10) &amp; GetResText(1301)
		msgError$ = GetResText(1302)
		If bTemplate Then
			DialogModel.Title = GetResText(1303+3)
			DialogModel.CmdCancel.Label = GetResText(1102)
			DialogModel.CmdCorrGoOn.Label = GetResText(1103)
			DialogModel.OptSingle.Label = GetResText(1303 + 1)
			DialogModel.Optmerge.Label = GetResText(1303 + 2)
			DialogModel.FrmLetter.Label = GetResText(1303)
		End If
		LoadLanguageCorrespondence() = True
	Else
		msgbox(&quot;Warning: Resource could not be loaded!&quot;)
	End If
End Function


Function GetFieldName(oFieldKnot as Object, GeneralFieldName as String)
	If oFieldKnot.HasByName(GeneralFieldName) Then
    GetFieldName = oFieldKnot.GetByName(GeneralFieldName).AssignedFieldName
	Else
		GetFieldName = &quot;&quot;
	End If
End Function


Sub OK
Dim ParaBreak
Dim sDocLang as String
Dim oSearchDesc as Object
Dim oFoundAll as Object
Dim oFound as Object
Dim sFoundContent as String
Dim sFoundString as String
Dim sDBField as String
Dim i as Integer
Dim oDBAccess as Object
Dim oAddressDialog as Object
Dim oAddressPilot as Object
Dim oFields as Object
Dim oDocSettings as Object
Dim oContext as Object
Dim bDBvalid as Boolean
	&apos;On Local Error Goto GENERALERROR
	
	If bTemplate Then
		bDBFields = DialogModel.Optmerge.State              &apos;database or placeholder
		TemplateDialog.EndExecute()
		DialogExited = TRUE
	End If
	
	If bDBFields Then
		oDBAccess = GetRegistryKeyContent(&quot;org.openoffice.Office.DataAccess/AddressBook/&quot;)
		sAddressbook = oDBAccess.DataSourceName

		bDBvalid = false
		oContext = createUnoService( &quot;com.sun.star.sdb.DatabaseContext&quot; )		

		If (not isNull(oContext)) Then 
			&apos;Is the previously assigned address data source still valid?
			bDBvalid = oContext.hasByName(sAddressbook)
		end if
				
		If (bDBvalid = false) Then			
			oAddressPilot = createUnoService(&quot;com.sun.star.ui.dialogs.AddressBookSourcePilot&quot;)
			oAddressPilot.execute
			
			oDBAccess = GetRegistryKeyContent(&quot;org.openoffice.Office.DataAccess/AddressBook/&quot;)
			sAddressbook = oDBAccess.DataSourceName
			If sAddressbook = &quot;&quot; Then
				MsgBox(GetResText(1301))
				Exit Sub
			End If
		End If
		oFields = oDBAccess.GetByName(&quot;Fields&quot;)
		Table = oDBAccess.GetByName(&quot;Command&quot;)
	End If

	ParaBreak = com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK
  	oDocument = ThisComponent
	If bDBFields Then
		&apos;set the address db as current db at the document
    	oDocSettings = oDocument.createInstance(&quot;com.sun.star.document.Settings&quot;)
		oDocSettings.CurrentDatabaseDataSource = sAddressbook
		oDocSettings.CurrentDatabaseCommand = Table
		oDocSettings.CurrentDatabaseCommandType = 0
	End If
	oBookmarks = oDocument.Bookmarks
	oText = oDocument.Text

	oSearchDesc = oDocument.createsearchDescriptor()
	oSearchDesc.SearchRegularExpression = True
	oSearchDesc.SearchWords = True
	oSearchDesc.SearchString  = &quot;&lt;[^&gt;]+&gt;&quot;
	oFoundall = oDocument.FindAll(oSearchDesc)

	&apos;Loop over the foundings
  	For i = oFoundAll.Count -1 To 0 Step -1
		oFound = oFoundAll.GetByIndex(i)
		sFoundString = oFound.String
		&apos;Extract the string inside the brackets
		sFoundContent = FindPartString(sFoundString,&quot;&lt;&quot;,&quot;&gt;&quot;,1)
		sFoundContent = LTrim(sFoundContent)
		&apos; Define the Cursor and place it on the founding
		oBookmarkCursor = oFound.Text.CreateTextCursorbyRange(oFound)
		oBookText = oFound.Text
		If bDBFields Then
			sDBField = GetFieldname(oFields, sFoundContent)
			If sDBField &lt;&gt; &quot;&quot; Then
				InsertDBField(sAddressbook, Table, sDBField)
			Else
				InsertPlaceholder(sFoundContent)
			End If
		Else
			InsertPlaceholder(sFoundContent)
		End If
	Next i
	If bDBFields Then
		&apos;Open the DB beamer with the right DB
		Dim oDisp as Object
		Dim oTransformer
		Dim aURL as new com.sun.star.util.URL
		aURL.complete = &quot;.component:DB/DataSourceBrowser&quot;
		oTransformer = createUnoService(&quot;com.sun.star.util.URLTransformer&quot;)
		oTransformer.parseStrict(aURL)
		oDisp = oDocument.getCurrentController.getFrame.queryDispatch(aURL, &quot;_beamer&quot;, com.sun.star.frame.FrameSearchFlag.CHILDREN + com.sun.star.frame.FrameSearchFlag.CREATE)
		Dim aArgs(3) as new com.sun.star.beans.PropertyValue
		aArgs(1).Name = &quot;DataSourceName&quot;
		aArgs(1).Value = sAddressbook
		aArgs(2).Name = &quot;CommandType&quot;
		aArgs(2).Value = com.sun.star.sdb.CommandType.TABLE
		aArgs(3).Name = &quot;Command&quot;
		aArgs(3).Value = Table
		oDisp.dispatch(aURL, aArgs())
	End If
	
	GENERALERROR:
	If Err &lt;&gt; 0 Then
		Msgbox(msgError$,16, GetProductName())
		Resume LETSGO
	End If
	LETSGO:

End Sub


Sub InsertDBField(sDBName as String, sTableName as String, sColName as String)
Dim oFieldMaster, oField as Object
	If sColname &lt;&gt; &quot;&quot; Then
		oFieldMaster = oDocument.createInstance(&quot;com.sun.star.text.FieldMaster.Database&quot;)
		oField = oDocument.createInstance(&quot;com.sun.star.text.TextField.Database&quot;)
		oFieldMaster.DataBaseName = sDBName
		oFieldMaster.DataBaseName = sDBName
		oFieldMaster.DataTableName = sTableName
		oFieldMaster.DataColumnName = sColName
		oField.AttachTextfieldmaster (oFieldMaster)
		oBookText.InsertTextContent(oBookMarkCursor, oField, True)
		oField.Content = &quot;&lt;&quot; &amp; sColName &amp; &quot;&gt;&quot;
	End If
End Sub


Sub InsertPlaceholder(sColName as String)
Dim oFieldMaster as Object
Dim bCorrectField as Boolean
	If sColname &lt;&gt; &quot;&quot; Then
		bCorrectField = True
		oFieldMaster = oDocument.createInstance(&quot;com.sun.star.text.TextField.JumpEdit&quot;)
		Select Case sColName
			Case &quot;Company&quot;
				oFieldMaster.PlaceHolder = getResText(1350+1)
			Case &quot;Department&quot;
				oFieldMaster.PlaceHolder = getResText(1350+2)
			Case &quot;FirstName&quot;
				oFieldMaster.PlaceHolder = getResText(1350+3)
			Case &quot;LastName&quot;
				oFieldMaster.PlaceHolder = getResText(1350+4)
			Case &quot;Street&quot;
				oFieldMaster.PlaceHolder = getResText(1350+5)
			Case &quot;Country&quot;
				oFieldMaster.PlaceHolder = getResText(1350+6)
			Case &quot;Zip&quot;
				oFieldMaster.PlaceHolder = getResText(1350+7)
			Case &quot;City&quot;
				oFieldMaster.PlaceHolder = getResText(1350+8)
			Case &quot;Title&quot;
				oFieldMaster.PlaceHolder = getResText(1350+9)
			Case &quot;Position&quot;
				oFieldMaster.PlaceHolder = getResText(1350+10)
			Case &quot;AddrForm&quot;
				oFieldMaster.PlaceHolder = getResText(1350+11)
			Case &quot;Code&quot;
				oFieldMaster.PlaceHolder = getResText(1350+12)
			Case &quot;AddrFormMail&quot;
				oFieldMaster.PlaceHolder = getResText(1350+13)
			Case &quot;PhonePriv&quot;
				oFieldMaster.PlaceHolder = getResText(1350+14)
			Case &quot;PhoneComp&quot;
				oFieldMaster.PlaceHolder = getResText(1350+15)
			Case &quot;Fax&quot;
				oFieldMaster.PlaceHolder = getResText(1350+16)
			Case &quot;EMail&quot;
				oFieldMaster.PlaceHolder = getResText(1350+17)
			Case &quot;URL&quot;
				oFieldMaster.PlaceHolder = getResText(1350+18)
			Case &quot;Note&quot;
				oFieldMaster.PlaceHolder = getResText(1350+19)
			Case &quot;Altfield1&quot;
				oFieldMaster.PlaceHolder = getResText(1350+20)
			Case &quot;Altfield2&quot;
				oFieldMaster.PlaceHolder = getResText(1350+21)
			Case &quot;Altfield3&quot;
				oFieldMaster.PlaceHolder = getResText(1350+22)
			Case &quot;Altfield4&quot;
				oFieldMaster.PlaceHolder = getResText(1350+23)
			Case &quot;Id&quot;
				oFieldMaster.PlaceHolder = getResText(1350+24)
			Case &quot;State&quot;
				oFieldMaster.PlaceHolder = getResText(1350+25)
			Case &quot;PhoneOffice&quot;
				oFieldMaster.PlaceHolder = getResText(1350+26)
			Case &quot;Pager&quot;
				oFieldMaster.PlaceHolder = getResText(1350+27)
			Case &quot;PhoneCell&quot;
				oFieldMaster.PlaceHolder = getResText(1350+28)
			Case &quot;PhoneOther&quot;
				oFieldMaster.PlaceHolder = getResText(1350+29)
			Case &quot;CalendarURL&quot;
				oFieldMaster.PlaceHolder = getResText(1350+30)
			Case &quot;InviteParticipant&quot;
				oFieldMaster.PlaceHolder = getResText(1350+31)
			Case Else
				bCorrectField = False
		End Select
		If bCorrectField Then
			oFieldMaster.Hint = getResText(1350)
			oBookText.InsertTextContent(oBookMarkCursor, oFieldMaster, True)
		End If
	End If
End Sub
</script:module>