Jump to content

Macro per creare lettere in serie con i dati di un database SQL

From The Document Foundation Wiki
This page is a translated version of the page Macros/Writer/010 and the translation is 100% complete.

La stampa in serie è una funzionalità di LibreOffice che consente di scrivere un modello di lettera contenente dei campi predefiniti e di popolarlo con i dati estratti da un database. Questo è molto utile se avete la necessità di inviare la stessa lettera a numerose persone apportando solo delle piccole modifiche, come il nome e l'indirizzo della persona alla quale la state inviando.

Come si usa?

Dopo aver scaricato e aperto il file mailmerge_by-lanedoCOM.odt, potete usare le macro contenute in questo documento. Modificate il modello per adattarlo alle vostre esisgente e poi eseguite le macro usando il menu:

  • Strumenti ▸ Macro ▸ Esegui macro
  • mailmerge_by-lanedoCOM.odt → Standard → Mail → DoManualMailMerge

NOTA: La macro si collega a un database e non eseguirà nulla se il database non è disponibile. Perciò non potete aspettarvi che funzioni semplicemente scaricando il file.

Download

   Media:mailmerge_by-lanedoCOM.odt


Come funziona

La macro si collega a un database SQL e popola i campi del modello. Il risultato è memorizzato in un unico file PDF in modo da rendere più semplice la stampa.

Le impostazioni del modello e del database sono specifiche per l'attività di Lanedo, ma possono essere facilmente personalizzate.


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

Sub DoManualMerge

Dim args(1) As New com.sun.star.beans.PropertyValue

doc = ThisComponent 
fm = doc.getTextFieldMasters()

'names = fm.getElementNames ()
'For idx = 0 To UBound(names())
'	print names(idx)
'Next idx

'https://wiki.openoffice.org/wiki/Documentation/DevGuide/Text/Text_Fields

res = GetResultSetFromMDB ()

Do While res.next ()
	meta = res.getMetaData()
	cur_row = res.getRow ()
	
	field = fm.getByName(TFName("Title"))
	field.Content = res.getString (9)
	
	field = fm.getByName(TFName("FirstName"))
	firstname = res.getString (2)
	field.Content = firstname

	field = fm.getByName(TFName("Surname"))
	surname = res.getString (3)
	field.Content = surname
	
	field = fm.getByName(TFName("Street"))
	field.Content = res.getString (4)
	
	field = fm.getByName(TFName("Housenumber"))
	field.Content = res.getString (5)
	
	field = fm.getByName(TFName("ZIP"))
	field.Content = res.getString (6)
	
	field = fm.getByName(TFName("City"))
	field.Content = res.getString (7)
	
	field = fm.getByName(TFName("Country"))
	field.Content = res.getString (8)
	
	doc.TextFields.refresh()
	args(0).Name = "FilterName"
	args(0).Value = "writer_pdf_Export"
	url = "file:///Users/gicmo/Coding/Lanedo/MM_" & firstname & "_" & surname & ".pdf"
	
	doc.storeToURL(url, args())

Loop	


End Sub

Function TFName (str As String) As String
 TFName = "com.sun.star.text.fieldmaster.User.Recp" & str
End Function

Sub MailMergeService

Dim MM as Object

MM = CreateUnoService("com.sun.star.text.MailMerge")
MM.DocumentURL = "file:///Users/gicmo/Coding/Lanedo/MailMerge.odt" 'sourceDocName
MM.OutputType = com.sun.star.text.MailMergeType.FILE
MM.OutputUrl = "file:///Users/gicmo/Coding/Lanedo/"
MM.FileNamePrefix = "MM_"
MM.SaveAsSingleFile=True
MM.DataSourceName =  "testdb_marvin"
MM.CommandType = 1 '1 = predefined query, 2 SQL
MM.Command = "all_data"
MM.execute(Array())
MM.dispose()

End Sub


Function db_escape (str As String, connection) As String
 Dim delim As String
 meta = connection.getMetaData ()
 delim = meta.getIdentifierQuoteString ()
 db_escape = delim & str & delim
End Function


Sub GetResultSetFromMDB as Object

Dim dm
Dim driver
Dim params(1) As New com.sun.star.beans.PropertyValue

REM Get Current Spreadsheet
REM http://api.openoffice.org/docs/common/ref/com/sun/star/sheet/module-ix.html
REM  http://api.openoffice.org/docs/common/ref/com/sun/star/sheet/SpreadsheetDocument.html (getSheets -> XSpreadsheets)
REM http://api.openoffice.org/docs/common/ref/com/sun/star/table/module-ix.html
doc = ThisComponent

url = "jdbc:mysql://marvin.local/testdb?user=gicmo&password=XXXXX"

' Database Setup
dm = CreateUnoService ("com.sun.star.sdbc.DriverManager")

params(0).Name = "JavaDriverClass"
params(0).Value = "com.mysql.jdbc.Driver"

con = dm.getConnectionWithInfo (url, params())

if IsNull (con) Then
	MsgBox "Could not connect to DB"
	Exit Sub
End if 

stmt = con.CreateStatement()
sqltxt = "SELECT * FROM " & db_escape("addressbook", con)
GetResultSetFromMDB = stmt.executeQuery (sqltxt)
End Sub


Macros