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