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