Create personalized Templates for Open Office for business letters in Windows

Business letters often need in addition to the company logos and information, some personalized information about the user which is writing and or sendig this letter. Such information would be, the name, telephone number, email adress and so on. There is a function in Microsoft Office which offers such a solution. It is possible to show 'document variables'. These variables can be filled with data take from the registration with VBA (Visual Basic Application). Unfortunatly there is no equal function available in Open Office.
1 answer

Creating macros by Open Office Basic

At first personalization data (The values you want to add to the template) has to be written to the registry.

Simply create a *.reg file and install it to the registry.

e.g.:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\EnvironmentVariables]
"Name"="Philipp Glavanovits"

Now that we have the values in the registry we need a routine to access this values. We use Open Office Basic Macros to read the data out of the registry and make it available in Open Office. The values are available as Open Office userfields.

Open Office Basic can be found under the tab Extras => Macros => Organize Makros => Open Office.org Basic

To add such a value open the tab Insert => Field => Others and switch to the Tab Variables. Under the value Userfields you find all loaded values.

The macro to access the values in the predefined registry file following:

REM ***** BASIC *****

Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" ( _
ByVal hKey As Long, _
ByVal lpSubKey As String, _
phkResult As Long) As Long

Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" ( _
ByVal hKey As Long, _
ByVal lpValueName As String, _
ByVal lpReserved As Long, _
lpType As Long, _
ByVal lpData As long, _
ByVal lpcbData As Long) As Long

Declare Function RegQueryValueExA Lib "advapi32.dll" ( _
ByVal hKey As Long, _
ByVal lpValueName As String, _
ByVal lpReserved As Long, _
lpType As Long, _
lpData As String, _
lpcbData As Long) As Long

Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002

Sub Main

Dim ret As Long
Dim hKey as long
dim sKeyName as string
dim sValueName as string
Dim dwValue As String
Dim dwLen As Long
dim pdwType as long

Dim EMailName As String
Dim Signature As String
Dim SignatureText As String
Dim OfficePhoneNumber As String
Dim MobilePhoneNumber As String
Dim FaxNumber As String
Dim Homepage As String

Dim sUrl As String

sKeyName = "EnvironmentVariables"

ret = RegOpenKey(HKEY_CURRENT_USER, sKeyName , hKey)

dwLen = 255
ret = RegQueryValueExA(hKey, "Name", 0, pdwtype, Name, dwLen)
SetDocumentvariable("Name", Name)

ret = RegCloseKey(hKey)

oDocumentInfo = thisComponent

End Sub

'=========================================================================================
' SetDocumentVariable - routine used to create/set value of a document variable into
' the document user's textfield list, without physically inserting its contents in
' the text of the ad.
' In - strVarName: string of the name of the variable to be set/created
' aValue: string with the value of the doc variable
' Out - boolean flag with the operation status: TUE=OK, FALSE=variable could not be
' set/create
'=========================================================================================
Function SetDocumentVariable(ByVal strVarName As String, ByVal aValue As String) As Boolean
Dim bFound as Boolean

On Error Goto ErrorHandler
oActiveDocument = thisComponent
oTextmaster = oActiveDocument.getTextFieldMasters()
sName = "com.sun.star.text.FieldMaster.User."+ strVarName
bFound = oActiveDocument.getTextFieldMasters.hasbyname(sName)

if bFound Then
xMaster = oActiveDocument.getTextFieldMasters.getByName(sName) 'check if variable exists
'value MEMBER used for decimal values, CONTENT member for Strings
'xMaster.value 0 aValue
xMaster.Content = aValue
Else 'Document variable doesn't exist yet
sService = "com.sun.star.text.FieldMaster.User"
xMaster = oActiveDocument.createInstance(sService)
xMaster.Name = strVarName
xmaster.Content = aValue
End If
SetDocumentVariable = True 'Success
Exit Function

ErrorHandler:
SetDocumentVariable = False
end function