| View previous topic :: View next topic |
| Author |
Message |
Lodahl General User


Joined: 20 Feb 2007 Posts: 5 Location: Denmark
|
Posted: Sun May 10, 2009 10:57 am Post subject: Problems with document properties in 3.1 |
|
|
Usually I read the document properties fine, but with 3.1 it gives me some problems. Legacy custom properties are ok, but I can only get the first four fields and only if they are text.
The following example should traverse all custom fields, but I don't get the right reply.
Can any one help with new syntax?
| Code: | Sub GetUserInfoFields
Dim vDocInfo as Variant
Dim s$
Dim i%
vDocInfo = ThisComponent.getDocumentInfo()
'vDocInfo.setUserFieldValue(0, "...der")
'vDocInfo.setUserFieldValue(1, "Værdi skrevet her")
For i = 0 To vDocInfo().getUserFieldCount()-1
s$ = s$ & i & " " & vDocInfo.getUserFieldName(i) & ": " & CStr(vDocInfo.getUserFieldValue(i)) & CHR$(10)
Next
MsgBox (vDocInfo().getUserFieldCount() & " fields" &CHR$(13) & "----------------------" & CHR$(13) & s$, 0, "Info fields")
End sub |
_________________ ------------------------
Leif Lodahl
DA.OpenOffice.org
http://openoffice.magenta-aps.dk/author/leif
------------------------ |
|
| Back to top |
|
 |
JohnV Administrator

Joined: 07 Mar 2003 Posts: 7664 Location: Kentucky, USA
|
Posted: Mon May 11, 2009 9:16 am Post subject: |
|
|
Agreed. I notice other strange things.
Sometimes a document will have Info 1-4 and other times it will have none and you have to Add them and fill in the name. If that happens and you latter change the name your macro shows the old name. And if you Add less than 4 your macro shows 4. |
|
| Back to top |
|
 |
B Marcelly Super User

Joined: 12 May 2004 Posts: 1145 Location: France
|
Posted: Tue May 12, 2009 5:13 am Post subject: |
|
|
Hi,
Service DocumentInfo is deprecated from version 3.1
http://api.openoffice.org/docs/common/ref/com/sun/star/document/DocumentInfo.html
It is replaced by service DocumentProperties, and interface XDocumentProperties.
Example of display: | Code: | Option Explicit
Sub DisplayUserFields
Dim udefProps As Object, allUProps As Object, oneUProp As Object
Dim UPlist As String
udefProps = ThisComponent.DocumentProperties.UserDefinedProperties
allUProps = udefProps.PropertyValues
UPlist = ""
For Each oneUProp in allUProps
if IsUnoStruct(oneUProp.Value) then
With oneUProp.Value
UPlist = UPlist & oneUProp.Name & " = " & _
DateSerial(.Year, .Month, .Day) & chr(13)
End With
else
UPlist = UPlist & oneUProp.Name & " = " & oneUProp.Value & chr(13)
end if
Next
MsgBox(UPlist, 0, "User Defined Properties")
End Sub |
Variable allUProps is an array of structures com.sun.star.beans.PropertyValue.
A user property of type date in the user interface is in fact a structure com.sun.star.util.DateTime.
Other data types are directly converted to string by the macro.
______
Bernard |
|
| Back to top |
|
 |
Lodahl General User


Joined: 20 Feb 2007 Posts: 5 Location: Denmark
|
Posted: Thu Jun 04, 2009 12:24 pm Post subject: |
|
|
Hi all,
Thanks for all the suggestions.
Here is the solution that we found:
This code sets user properties:
| Code: | Sub AddUserProperty(fieldname, fieldvalue)
Dim userdefinedproperties As Object
userdefinedproperties = ThisComponent.DocumentProperties.UserDefinedProperties
If ExistUserProperty(fieldname) Then
on error goto errorhandler
userdefinedproperties.setPropertyValue(fieldname, fieldvalue)
else
userdefinedproperties.addProperty(fieldname, 0, fieldvalue) ' REMOVEABLE=128
end if
exit sub
errorhandler:
print "Type of field " & fieldname & " is " & TypeName(GetUserProperty(fieldname)) & ". Value " & fieldvalue & " given has type " & TypeName(fieldvalue)
stop
End Sub
|
This code removes a property filed (by fieldname):
| Code: | Sub RemoveUserProperty(fieldname)
Dim userdefinedproperties As Object
If ExistUserProperty(fieldname) Then
userdefinedproperties = ThisComponent.DocumentProperties.UserDefinedProperties
userdefinedproperties.removeProperty(fieldname)
EndIf
End Sub
|
This code reads user property (by field name):
| Code: | Function GetUserProperty(FieldName as String)
on error goto ErrorHandler
dim res
res = ThisComponent.DocumentProperties.UserDefinedProperties.getPropertyValue(FieldName)
ErrorHandler:
GetUserProperty = res
end Function
|
This code checks if a field name exists:
| Code: | Function ExistUserProperty(FieldName as String) as Boolean
on error goto not_found
ThisComponent.DocumentProperties.UserDefinedProperties.getPropertyValue(FieldName)
ExistUserProperty = True
Exit Function
not_found:
ExistUserProperty = False
End Function |
I hope this can be of some value to others  _________________ ------------------------
Leif Lodahl
DA.OpenOffice.org
http://openoffice.magenta-aps.dk/author/leif
------------------------ |
|
| Back to top |
|
 |
|