问题
Is there any proper way to change text in the user's created xTextField using C++ UNO? These fields names are com.sun.star.text.fieldmaster.User.[FIELD NAME]
I tried this before, but it didn't help: Libreoffice API (UNO) : text and data from xTextField
Also I've tried something like this but still didn't help:
// current_field - xTextField I got before
Reference<XText> xText = Reference<XText>(current_field, UNO_QUERY);
if (!xText.is())
{
qDebug() << "XText FROM xTextField IS NULL!";
return;
}
OUStringBuffer bufText;
bufText.append( new_value.utf16() );
std::stringstream textStr;
textStr << bufText.toString();
xText->setString( bufText.toString() );
Any suggestions?
回答1:
Did you read section 5.18 of Andrew's Macro Document as recommended in my other answer? Here is Listing 5.49 translated into C++. It seems there is a bug in that listing because I had to add "."
to make it work.
OUString sName = OUString::createFromAscii("Author Name");
OUString sServ = OUString::createFromAscii("com.sun.star.text.FieldMaster.User");
OUString sFieldName = sServ + OUString::createFromAscii(".") + sName;
Reference< XMultiServiceFactory > xDocFactory (xTextDocument, UNO_QUERY);
if (xNamedFieldMasters->hasByName(sFieldName))
{
fieldMaster = xNamedFieldMasters->getByName(sFieldName);
Reference< XPropertySet> xProps (fieldMaster, UNO_QUERY);
Any aContent;
aContent <<= OUString::createFromAscii("Andrew Pitonyak");
xProps->setPropertyValue(OUString::createFromAscii("Content"), aContent);
}
else
{
fieldMaster <<= xDocFactory->createInstance(sServ);
Reference< XPropertySet> xProps (fieldMaster, UNO_QUERY);
Any aName;
aName <<= sName;
xProps->setPropertyValue(OUString::createFromAscii("Name"), aName);
Any aContent;
aContent <<= OUString::createFromAscii("Andrew Pitonyak");
xProps->setPropertyValue(OUString::createFromAscii("Content"), aContent);
}
If this code is run on a blank document, the newly created field can be seen by going to Insert -> Fields -> More Fields, Variables, User Field.
来源:https://stackoverflow.com/questions/62769483/libreoffice-api-uno-need-to-change-users-xtextfield-text