Change variable
From NOA Documentation Wiki
Source: Markus Krüger (NOA developer)
The following code shows how to change the value of OpenOffice.org variables added via 'Insert -> Field -> Others... -> Variables-> Set variable (Text)':
...
ITextDocument textDocument = ...;
ITextFieldService textFieldService = textDocument.getTextFieldService();
ITextField[] variables = textFieldService.getVariableFields("");
for (int i = 0; i < variables.length; i++) {
XPropertySet xPropertySetField = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, variables[i].getXTextContent());
xPropertySetField.setPropertyValue("Content", "New text for " + xPropertySetField.getPropertyValue("VariableName")
+ " where old text was "
+ xPropertySetField.getPropertyValue("CurrentPresentation"));
}
textDocument.getTextFieldService().refresh();
It is important to know, that the array of placeholders is not in any order, so you have to check the property "VariableName" for the variable you want to replace.
Another option is to exclicitly only get fields for a specific variable name like this:
...
ITextDocument textDocument = ...;
ITextFieldService textFieldService = textDocument.getTextFieldService();
ITextField[] variables = textFieldService.getVariableTextFieldMaster("myVariableName").getVariableTextFields();
for (int i = 0; i < variables.length; i++) {
XPropertySet xPropertySetField = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, variables[i].getXTextContent());
xPropertySetField.setPropertyValue("Content", "New text for " + xPropertySetField.getPropertyValue("VariableName")
+ " where old text was "
+ xPropertySetField.getPropertyValue("CurrentPresentation"));
}
textDocument.getTextFieldService().refresh();

