Word Shape ConvertToShape 异常,返回E_FAIL,解决办法

本文介绍了一种在Word中插入图片时遇到的问题及解决方案。当使用InlineShape的AddPicture方法时,ConvertToShape会抛出异常,解决方案是采用Shapes的AddPicture方法并通过Find定位插入点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

源代码:

_variant_t varLinkToFile=VARIANT_FALSE;

_variant_t varSaveWithDoc=VARIANT_TRUE;

try

                   {
                       Word::InlineShapePtr lpILShate=pSel->InlineShapes->AddPicture(_bstr_t(sValue),&varLinkToFile,&varSaveWithDoc);
                           if(pObj->m_nCtrlType-100!=7)
                           {
                               Word::ShapePtr lpShape=NULL;
                               if(lpILShate!=NULL)
                                   lpShape=lpILShate->ConvertToShape();
                               if(lpShape!=NULL)
                               {
                                   lpShape->WrapFormat->AllowOverlap=1;
                                   lpShape->WrapFormat->put_Type(Word::WdWrapType(pObj->m_nCtrlType-100));
                               }
                           }
                   }
                   catch(com_error e){}

不知什么原因,ConvertToShape老报异常,返回E_FAIL

解决办法:使用Shapes的AddPicture,但要通过find方法定位插入点


_variant_t FindText=(LPCTSTR)sField;
_variant_t ReplaceWith=vtMissing ;
_variant_t Forward=VARIANT_TRUE;
_variant_t Wrap=(_variant_t)(Word::wdFindContinue);
_variant_t Format=VARIANT_FALSE;
_variant_t MatchCase=VARIANT_FALSE;
_variant_t MatchWholeWord=VARIANT_FALSE;

_variant_t MatchWildcards=VARIANT_FALSE;
_variant_t MatchSoundsLike=VARIANT_FALSE;
_variant_t MatchAllWordForms=VARIANT_FALSE;

CString sNull(_T(""));

ReplaceWith = (LPCTSTR)sNull;

CString sValue = _T("the pos you wanted");

pSel->Find->Execute(
                           &FindText, &MatchCase, &MatchWholeWord, &MatchWildcards, &MatchSoundsLike,
                           &MatchAllWordForms, &Forward, &Wrap, &Format, &ReplaceWith,&vtMissing);


Word::RangePtr lpRange=pSel->Range;
                       _variant_t vtAnchor(lpRange,true);
                       Word::ShapesPtr shapes = m_lpWord->GetShapes();
                       Word::ShapePtr shape = shapes->AddPicture(_bstr_t(sValue), &varLinkToFile, &varSaveWithDoc, &vtMissing, &vtMissing, &vtMissing, &vtMissing,&vtAnchor);


还望与大家多多讨论


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值