Insert an image into Word with the Edraw Office Viewer Component

With 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 (*.*)|*.*||”;
 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!”);

  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.
  Shape m_wordShape;
  Shapes m_wordShapes;
  COleVariant vTrue((short)TRUE),vFalse((short)FALSE);
  CString m_strPath;
  TCHAR exeFullPath[MAX_PATH];
  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;
  InlineShapes m_WordInlineShapes;
  InlineShape m_WordInlineShape;
  Selection m_WordSelection;
  m_WordInlineShape = m_WordInlineShapes.AddPicture(FileName,vFalse,vTrue,vOpt);
  m_wordShape = m_WordInlineShape.ConvertToShape();
  Shapes shs;
  Shape shp;
  VARIANT var;
  ShapeRange m_wordShapeRange;
  WrapFormat m_wordWrapFormant;
  m_WordApp = m_WordDoc.GetApplication();
  m_wordShape = m_WordSelection.GetShapeRange();
  m_wordWrapFormant = m_wordShape.GetWrapFormat();
  PictureFormat m_wordPictureFormat = m_wordShape.GetPictureFormat();
  FillFormat m_wordFillFormat  = m_wordShape.GetFill();

Failed to get the Excel Worksheets or WorkBooks object

When you develop a worksheet with the macro in the activex control,  you can use the thisworkbook.sheets(“”) or thisapplication.workbook(“”) to replace the workbook.sheets(“”) or application.sheets(“”).

What are the differences between early binding and late binding?

Binding refers to how your application connects to an Automation server. There are two common methods: early binding and late binding. The method you should use depends on your application design and objectives.

Which form of binding should I use?

The answer to this question depends as much on the design of your project as anything else. Microsoft recommends early binding in almost all cases. However, there may be reasons for choosing late binding.

Early binding is the preferred method. It is the best performer because your application binds directly to the address of the function being called and there is no extra overhead in doing a run-time lookup. In terms of overall execution speed, it is at least twice as fast as late binding.

Early binding also provides type safety. When you have a reference set to the component’s type library, Visual Basic provides IntelliSense support to help you code each function correctly. Visual Basic also warns you if the data type of a parameter or return value is incorrect, saving a lot of time when writing and debugging code.

Late binding is still useful in situations where the exact interface of an object is not known at design-time. If your application seeks to talk with multiple unknown servers or needs to invoke functions by name (using the Visual Basic 6.0 CallByName function for example) then you need to use late binding. Late binding is also useful to work around compatibility problems between multiple versions of a component that has improperly modified or adapted its interface between versions.

The advantages given to early binding make it the best choice whenever possible.

Is there are any library needed for Office Automation? If yes what is it?

Only install MS Office in the computer.

Automate Excel using Excel OCX and worksheet functions

This article describes how to automate Microsoft Excel 97, Microsoft Excel 2000, Microsoft Excel 2002, or Microsoft Excel 2003 by using the Edraw Office Viewer Component. Specifically, this article illustrates how to get the worksheet dispatch interface.

1. Open Visual Studio .NET

2.  Right-click on the toolbox and select “Choose Items…”

3. Select the COM Components Tab

4. Check Edraw Office Viewer Component and click OK Button

5.  The control should appear in the toolbox as “Edraw Office Viewer Component”

6.  Double-click on the control to add to your form

7.  Resize and move the control on the form as desired

8.  Add a button to the form

9.  Double-click on the button to access code editor and enter the following code within the Click event:

‘Note: Modify code to point to an existing office document file on your web server

EdrawOfficeViewer1.Open “

10. Change the anchor property of EdrawOfficeViewer1 to Top, Bottom, Left, Right

11. Run the application and click on the button. The Excel program should appear!

For this automation process we need to follow the below steps

1. Add a referrence to the Microsoft Excel object library COM component.

2. Add the namespace Excel

3. Instantiate the class Excel.ApplicationClass as below
Excel.Application xl=EdrawOfficeViewer1.Application

4. To read cell(s) in the worksheet,

Excel.Sheets xlsheets = wb.Sheets; //Get the sheets from workbook
Excel.Worksheet excelWorksheet = (Excel.Worksheet)xlsheets[1]; //Select the first sheet
Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range(“B4:FZ4”, Type.Missing); //Select a range of cells
Excel.Range excelCell2 = (Excel.Range)excelWorksheet.get_Range(“A5:A5″, Type.Missing); //Select a single cell
Console.WriteLine(excelCell2.Cells.Value2.ToString()); //Print the value of the cell for a single cell selection
System.Array myvalues = (System.Array)excelCell.Cells.Value2; //Assign it to an array
string[] strArray = ConvertToStringArray(myvalues); //Convert array into String array
foreach (string str in strArray)
Console.WriteLine(” Text in Cell ” + str); //Loop through the array to print the values in the cell


Excel is a great tool to work with. When it comes to automating, we need to consider many things. Always remember to quit the excel application in code before exiting. If not, the memory consumed by the excel application will not be freed up.

What is the Office Automation

Productivity is the biggest challenge for any company or small business. Increasing productivity is Microsoft Office’s goal. In that spirit, Microsoft has sought to include features that allow corporate and small business developers to produce quality in-house applications that harness the power and versatility of Office in order to quickly build customized solutions. Automation is the key to this strategy.

Automation (formerly known as OLE Automation) is the programmatic manipulation of any program or component based on certain rules following the Component Object Model (COM). Automation was first pioneered and developed by Visual Basic and Office as a way to allow developers to extend and control the Office environment both internally and externally.

While it is not necessary to know all the details about how Automation works, you should familiarize yourself with some of the key terms and concepts. Namely, an Automation client is any piece of code that creates and calls an instance of a COM server that exposes an “object model.” Object models are an arrangement of classes that expose functionality through various properties and methods and enable programmers to control a product. A particular instance of one of these classes is an object and the properties and methods comprise its interface. The object model for each Office application is different, and must be learned before Automation code can be written.

The Edraw Office Viewer Component provides some simple automation functions. You can call them directly. Or you can get the dispatch interface then do the automation by yourself. The component exposes the Application and ActiveDocument interfaces.