首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从word对象模型使用C++接口在word中添加CustomXMLParts?

如何从word对象模型使用C++接口在word中添加CustomXMLParts?
EN

Stack Overflow用户
提问于 2019-07-18 10:04:14
回答 1查看 66关注 0票数 0

我正在尝试在MS word文档的customXMLParts中插入一些文本。有很多使用C#或VBA的示例,但我找不到任何使用C++接口的示例用法。

因此,通常在VBA中您只需执行以下操作:

代码语言:javascript
复制
ActiveDocument.CustomXMLParts.Add "<dataOrigin>abcdef</dataOrigin>"

但是,如果我想使用MSO14中的C++接口做同样的事情。“Add”函数接口略有不同:

代码语言:javascript
复制
_CustomXMLParts : _IMsoDispObj 
{
 ...
 virtual HRESULT __stdcall Add (
        /*[in]*/ BSTR XML,
        /*[in]*/ VARIANT SchemaCollection,
        /*[out,retval]*/ struct _CustomXMLPart * * ppPart ) = 0;
}

所以我的问题是这个SchemaCollection应该是什么?存在_CustomXMLSchemaCollection类型,但它似乎与VARIANT类型不兼容。或者我应该只传递xml模式的stringfy版本?

我试着用下面的方法调用函数,但它似乎不起作用,只是出错了:

代码语言:javascript
复制
// ... some initial code to get customXMLParts

BSTR xmlContent = SysAllocString(L"<dataOrigin>abcdef</dataOrigin>");
const char * xmlSchema = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
"<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
"<xs:element name=\"dataOrigin\" type=\"string\"></xs:element>"
"</xs:schema>";

Office14::_CustomXMLPart * outXML (nullptr);
customXMLParts->Add(xmlContent, xmlSchema, &outXML);

大多数的例子我谷歌似乎是为C#或VBA,谁知道schemaCollection应该是什么,或有一个例子如何正确调用CustomXMLParts.Add使用c++接口?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-21 01:02:45

实际上,我想通了。它只是customXMLParts自己的模式集合。

代码语言:javascript
复制
BSTR xmlContent = SysAllocString(L"<dataOrigin>abcdef</dataOrigin>");

Office14:_CustomXMLSchemaCollection * schemaCollection ( nullptr );
customXMLParts->get_SchemaCollection( &schemaCollection );

Office14::_CustomXMLPart * outXMLPart (nullptr);
customXMLParts->Add( xmlContent, _variant_t(schemaCollection), &outXMLPart);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57086112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档