简体   繁体   中英

C++ Word Automation problem with _variant_t range pointer (AddPicture() method)

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

You cannot construct a _variant_t from a _com_ptr_t<T> (where T = Word::Range in your case), only from IUnknown* or IDispatch* .

Your only option is to explicitly access the C pointer:

&_variant_t( isRangePtr.GetInterfacePtr() )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM