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