Libreoffice API (UNO): need to change user's xTextField text

白昼怎懂夜的黑 提交于 2020-07-20 03:54:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!