【问题标题】:Libreoffice API (UNO): need to change user's xTextField textLibreoffice API (UNO):需要更改用户的 xTextField 文本
【发布时间】:2020-07-07 06:43:50
【问题描述】:
是否有任何适当的方法可以使用 C++ UNO 更改用户创建的 xTextField 中的文本?
这些字段名称是 com.sun.star.text.fieldmaster.User.[FIELD NAME]
我之前尝试过,但没有帮助:
Libreoffice API (UNO) : text and data from xTextField
我也尝试过类似的方法,但仍然没有帮助:
// 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() );
有什么建议吗?
【问题讨论】:
标签:
c++
libreoffice
uno
libreoffice-writer
【解决方案1】:
您是否按照我的其他答案中的建议阅读了Andrew's Macro Document 的第 5.18 节?这是清单 5.49 翻译成 C++。该列表中似乎存在错误,因为我必须添加 "." 才能使其正常工作。
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);
}
如果此代码在空白文档上运行,则可以通过转到 插入 -> 字段 -> 更多字段、变量、用户字段看到新创建的字段。