Correspondence.xba 10 KB

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