Insert an image into Word with the Edraw Office Viewer Component
Posted by office viewer on April 13th, 2007With the Edraw office Viewer Component, you can do the office automation in a customize form or web page. It provides the “GetActiveDocument” method to return the document’s dispath interface.
The follow example written with Visual C++ 6.0 demo how to insert an image into the opened microsoft word document.
void COfficeViewerDlg::OnWordautomation()
{
CString strFilter = L”Microsoft Word Document|*.doc;*.docx;*.dot;*.rft|All Files (*.*)|*.*||”;
CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST|OFN_HIDEREADONLY, strFilter);
dlg.m_ofn.nFilterIndex = 1;
if (dlg.DoModal() == IDOK)
{
CString pathname = dlg.GetPathName();
VARIANT varPath;
varPath.vt = VT_BSTR; varPath.bstrVal = pathname.AllocSysString();
VARIANT varOpt;
varOpt.vt = VT_ERROR; varOpt.scode = DISP_E_PARAMNOTFOUND;
if(m_oEdrawOfficeViewer.Open(varPath, varOpt)==FALSE){
AfxMessageBox(L”Failed to open the document!”);
return;
}
LPDISPATCH lpDisp;
lpDisp = m_oEdrawOfficeViewer.GetActiveDocument();
//Add text to the first line of the document
_Document m_WordDoc;
//set _Document doc to use lpDisp, the IDispatch* of the actual document.
m_WordDoc.AttachDispatch(lpDisp);
Shape m_wordShape;
Shapes m_wordShapes;
COleVariant vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
COleVariant vTrue((short)TRUE),vFalse((short)FALSE);
CString m_strPath;
TCHAR exeFullPath[MAX_PATH];
GetModuleFileName(NULL,exeFullPath,MAX_PATH);
m_strPath.Format(L”%s”,exeFullPath);
int ndx = m_strPath.ReverseFind (‘\\’);
m_strPath = m_strPath.Left (ndx);
CString filename = m_strPath + L”\\seal.bmp”;
_Application m_WordApp;
m_wordShapes = m_WordDoc.GetShapes();
CString FileName =filename;
m_WordApp=m_WordDoc.GetApplication();
InlineShapes m_WordInlineShapes;
InlineShape m_WordInlineShape;
Selection m_WordSelection;
m_WordSelection=m_WordApp.GetSelection();
m_WordInlineShapes=m_WordSelection.GetInlineShapes();
m_WordInlineShape = m_WordInlineShapes.AddPicture(FileName,vFalse,vTrue,vOpt);
m_wordShape = m_WordInlineShape.ConvertToShape();
Shapes shs;
Shape shp;
shs=m_WordDoc.GetShapes();
VARIANT var;
var.vt=VT_I4;
var.lVal=shs.GetCount();
shp=shs.Item(&var);
shp.Select(&var);
ShapeRange m_wordShapeRange;
WrapFormat m_wordWrapFormant;
m_WordApp = m_WordDoc.GetApplication();
m_wordShape = m_WordSelection.GetShapeRange();
m_wordWrapFormant = m_wordShape.GetWrapFormat();
m_wordWrapFormant.SetType(3);
m_wordShapeRange.ZOrder(4);
PictureFormat m_wordPictureFormat = m_wordShape.GetPictureFormat();
m_wordPictureFormat.SetTransparentBackground(TRUE);
m_wordPictureFormat.SetTransparencyColor(0xFFFFFF);
FillFormat m_wordFillFormat = m_wordShape.GetFill();
m_wordFillFormat.SetVisible(FALSE);
shs.ReleaseDispatch();
shp.ReleaseDispatch();
m_wordPictureFormat.ReleaseDispatch();
m_wordFillFormat.ReleaseDispatch();
m_wordWrapFormant.ReleaseDispatch();
m_wordShapeRange.ReleaseDispatch();
m_wordShape.ReleaseDispatch();
m_wordShapes.ReleaseDispatch();
m_WordSelection.ReleaseDispatch();
m_WordDoc.ReleaseDispatch();
m_WordApp.ReleaseDispatch();
}
}
Recent Comments