首页 > 解决方案 > Libreoffice API (UNO):需要更改用户的 xTextField 文本

问题描述

是否有任何适当的方法可以使用 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++libreofficeunolibreoffice-writer

解决方案


您是否按照我的其他答案中的建议阅读了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);
}

如果此代码在空白文档上运行,则可以通过转到Insert -> Fields -> More Fields, Variables, User Field看到新创建的字段。


推荐阅读