Correspondence.xba 11 KB

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