使用msword.olb操作word(3)

来源:互联网 发布:临沂软件开发吧 编辑:程序博客网 时间:2024/05/30 23:01

摘要:实现在word中插入图片,并实现图片插入类型:嵌入型、图片位于文字上方、图片位于文字下方、上下型、穿越型、紧密型、四周型

例子代码如下:

_Application oWordApp;Documents oWordDocs;_Document    oWordDoc;Selection oWordSel;Find oWordFind;Replacement  oWordRep;InlineShapes oWordShapes;InlineShape oWordShape;Options option;Shapes shapes;Shape shape;WrapFormat oWordWrpFormat;COleVariant vTrue((short)TRUE),vFalse((short)FALSE),vOpt((long)DISP_E_PARAMNOTFOUND,VT_ERROR);//创建WORD实例if (!oWordApp.CreateDispatch(_T("Word.Application"))){AfxMessageBox(_T("Word CreateDispatch Failed!"));return ;}try{oWordApp.SetVisible(TRUE);oWordApp.Activate();oWordDocs = oWordApp.GetDocuments();CString sFileName(_T("C:\\发文办文单.doc"));CString sString(_T("X380X"));CString sPicture(_T("C:\\380-1-1.png"));oWordDoc= oWordDocs.Open(COleVariant(sFileName),vFalse,vFalse,vFalse,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt);oWordSel= oWordApp.GetSelection();oWordFind= oWordSel.GetFind();oWordRep= oWordFind.GetReplacement();oWordFind.ClearFormatting();oWordFind.SetText(sString);oWordRep.SetText(_T(""));oWordFind.SetForward(TRUE);oWordFind.SetWrap(1);oWordFind.SetFormat(FALSE);oWordFind.SetMatchCase(FALSE);oWordFind.SetMatchWholeWord(FALSE);oWordFind.SetMatchByte(TRUE);oWordFind.SetMatchWildcards(FALSE);oWordFind.SetMatchSoundsLike(FALSE);oWordFind.SetMatchAllWordForms(FALSE);oWordDoc.Save();BOOL bFlag = oWordFind.Execute(COleVariant(sString),vFalse,vFalse,vFalse,vFalse,vFalse,vTrue,vTrue,vFalse,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt);oWordShapes = oWordSel.GetInlineShapes();option= oWordApp.GetOptions();option.SetPictureWrapType((long)3);oWordShape = oWordShapes.AddPicture(sPicture,vFalse,vTrue,vOpt);shape= oWordShape.ConvertToShape();oWordWrpFormat = shape.GetWrapFormat();long lType = oWordWrpFormat.GetType();oWordWrpFormat.SetType((long)7);// 嵌入型//oWordWrpFormat.put_Type((long)6);// 图片位于文字上方//oWordWrpFormat.put_Type((long)5); // 图片位于文字下方//oWordWrpFormat.put_Type((long)4); // 上下型//oWordWrpFormat.put_Type((long)3); // 穿越型//oWordWrpFormat.put_Type((long)2); // 紧密型//oWordWrpFormat.put_Type((long)1); // 四周型oWordDoc.Save();oWordShapes.ReleaseDispatch();oWordRep.ReleaseDispatch();oWordFind.ReleaseDispatch();oWordSel.ReleaseDispatch();oWordDoc.Close(vOpt,vOpt,vOpt);oWordDoc.ReleaseDispatch();oWordDocs.ReleaseDispatch();}catch(...){}try{oWordApp.Quit(vOpt,vOpt,vOpt);oWordApp.ReleaseDispatch();}catch(...){}

0 0
原创粉丝点击