C++ _variant_t 范围指针的 Word Automation 问题(AddPicture() 方法)
我的任务是迭代 word xml 模式中的字段并替换图片。这些图片是 Excel 图表转换为图像。迭代模式中的字段也可以删除找到的图像。我的问题是在原始范围内重新插入内联形状。我将找到的 inlineshape 的范围保存在单独的 WordRangePointer 中,但 AddPicture() 和 AddOLEObject() 需要一个变体。仅通过插入 &vtMissing 我才设法将图片(或 OLE 对象)放入文档中,但因此图像会自动插入,而不是我想要的位置。
MSDN InlineShapes.AddOLEObject()
MSDN InlineShapes。 我的代码的AddPicture()
部分如下所示(如果找到 InlineShapes):
_variant_t vtTrue( true );
_variant_t vtFalse( false );
_variant_t vtTypeS( "Excel.Sheet.8" );
_variant_t vtTypeC( "Excel.Chart.8" );
Word::InlineShapesPtr ishps = spDoc->InlineShapes;
Word::InlineShapePtr is = field->InlineShape;
Word::RangePtr isRangePtr;
Word::DocumentsPtr spDocs = spWordApp->Documents;
Word::_DocumentPtr spDoc = spDocs->Open(&_variant_t( filename ));
if ( is && ( std::string( is->OLEFormat->ProgID ).find("Excel.Sheet.8") != std::string::npos ) )
{
std::cout << "Excel Sheet found" << std::endl;
height = (int)is->Height;
width = (int)is->Width;
isRangePtr= is->Range;
std::pair< long, long > range = std::make_pair( isRangePtr->Start, isRangePtr->End );
//Word::RangePtr r = field->Result;
is->Delete();
//sFullName = "//absolute path.......jpg" ;
// Now AddOleObject
//ishps->AddPicture( sFullName , &vtTrue, &vtTrue, &vtMissing );
sFullName = "//absolute path.......jpg" ;
is = ishps->AddPicture( sFullName , &vtTrue, &vtTrue, &vtMissing );
//is->Range->SetRange( range.first, range.second );
//ishps->AddOLEObject(&vtTypeS , &_variant_t( sFullName ), &vtTrue, &vtFalse, &vtMissing, &vtMissing, &vtMissing, &vtMissing );
如果我用 &_variant_t(isRangePtr) 替换最后一个参数range 我收到类型不匹配编译错误。我不明白为什么 COM 接口中现在有范围指针的 _variant_t 构造函数。也许有?
另一种选择是通过 TOP 和 LEFT 属性读取图片的绝对坐标并在没有范围的情况下插入它。但这个解决方案不优雅而且有点蹩脚。
我真的很感激任何帮助!
提前致谢!
克里斯
My task is to iterate over the fields in a word xml schema and to replace pictures. Those pictures are excel charts converted to images. Iterating over the fields in the schema is no problem also deleting the found images. My problem is to reinsert the inlineshapes at the original range. I am saving the range of a found inlineshape in a seperate WordRangePointer, but AddPicture() and AddOLEObject() expect a variant. Only by inserting &vtMissing I've managed to place a picture (or OLE object) in the document but therefore the images is inserted automatically and not where I want to.
MSDN InlineShapes.AddOLEObject()
MSDN InlineShapes.AddPicture()
part of my code looks like this (if a InlineShapes is found):
_variant_t vtTrue( true );
_variant_t vtFalse( false );
_variant_t vtTypeS( "Excel.Sheet.8" );
_variant_t vtTypeC( "Excel.Chart.8" );
Word::InlineShapesPtr ishps = spDoc->InlineShapes;
Word::InlineShapePtr is = field->InlineShape;
Word::RangePtr isRangePtr;
Word::DocumentsPtr spDocs = spWordApp->Documents;
Word::_DocumentPtr spDoc = spDocs->Open(&_variant_t( filename ));
if ( is && ( std::string( is->OLEFormat->ProgID ).find("Excel.Sheet.8") != std::string::npos ) )
{
std::cout << "Excel Sheet found" << std::endl;
height = (int)is->Height;
width = (int)is->Width;
isRangePtr= is->Range;
std::pair< long, long > range = std::make_pair( isRangePtr->Start, isRangePtr->End );
//Word::RangePtr r = field->Result;
is->Delete();
//sFullName = "//absolute path.......jpg" ;
// Now AddOleObject
//ishps->AddPicture( sFullName , &vtTrue, &vtTrue, &vtMissing );
sFullName = "//absolute path.......jpg" ;
is = ishps->AddPicture( sFullName , &vtTrue, &vtTrue, &vtMissing );
//is->Range->SetRange( range.first, range.second );
//ishps->AddOLEObject(&vtTypeS , &_variant_t( sFullName ), &vtTrue, &vtFalse, &vtMissing, &vtMissing, &vtMissing, &vtMissing );
If I replace the last argument with &_variant_t(isRangePtr) for the range I get a type mismatch compile error. I don't why there is now _variant_t constructor for range pointers in the COM interface. Maybe there is ?
Another option could be to read the absolute coordinates of the picture through th e TOP and LEFT property and to insert it without a range. But this solution would be inelegant and kind of crappy.
I would really appreciate any help!
Thanks in advance!
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能从
_com_ptr_t
构造_variant_t
(其中 T = Word::Range 在您的情况下),只能从IUnknown*
或IDispatch*
。您唯一的选择是显式访问 C 指针:
You cannot construct a
_variant_t
from a_com_ptr_t<T>
(where T = Word::Range in your case), only fromIUnknown*
orIDispatch*
.Your only option is to explicitly access the C pointer: