jopicach escribió:Hola de nuevo,
Podría hacer una pregunta. No se si es viable, pero ¿sería muy complicado abrir un documento que ya lleve texto y cambiar solo los campos que estén con los %? Me explico, el botón del formulario abriría un documento writer por su nombre, el cual llevaría ya el texto a modo de plantilla, estaría en la misma carpeta que la base de datos y al abrirse cambiase solo esos campos. Eso ya sería la solución ideal.
Muchas gracias.
Sub MakeDoc(event)
'Referencia al formulario
form = event.Source.Model.Parent
'Nombre de la plantilla
doc_name = event.Source.Model.Tag
'Ruta de esta base de datos
path = form.ActiveConnection.Parent.DatabaseDocument.URL
'Ruta a la plantilla
path_template = replace_name(path, doc_name)
'Abrimos la plantilla
doc = open_doc(path_template)
'Reemplazamos los valores
replace_fields(doc, form)
End Sub
Sub replace_fields(doc, form)
sd = doc.createSearchDescriptor()
sd.SearchCaseSensitive = False
sd.SearchRegularExpression = False
'Los campos del formulario
fields = form.Columns.getElementNames()
For i = LBound(fields) To UBound(fields)
sd.setSearchString("%" + fields(i) + "%")
found = doc.findAll(sd)
If found.Count > 0 Then
For f = 0 To found.Count - 1
t = found.getByIndex(f)
t.setString(form.getString(i+1))
Next f
End If
Next i
End Sub
Function replace_name(path, doc_name) As String
tmp = Split(path, "/")
tmp(UBound(tmp)) = doc_name
replace_name = Join(tmp, "/")
End Function
Function open_doc(path) As Object
Dim opt(0) As New "com.sun.star.beans.PropertyValue"
Dim doc As Object
opt(0).Name = "AsTemplate"
opt(0).Value = True
open_doc = StarDesktop.loadComponentFromURL(path, "_blank", 0, opt())
End Function
Sub replace_fields(doc, form)
sd = doc.createSearchDescriptor()
sd.SearchCaseSensitive = False
sd.SearchRegularExpression = False
'Los campos del formulario
fields = form.Columns.getElementNames()
For i = LBound(fields) To UBound(fields)
sd.setSearchString("%" + fields(i) + "%")
found = doc.findAll(sd)
If found.Count > 0 Then
field = form.Columns.getByName(fields(i))
value = form.getString(i+1)
If field.TypeName = "DATE" Then
value = Format(form.getLong(i+1), "dd/mm/yyyy")
End If
For f = 0 To found.Count - 1
t = found.getByIndex(f)
t.setString(value)
Next f
End If
Next i
End Sub
value = Format(form.getLong(i+1)+2, "DD/MM/YYYY")
Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 1 invitado