How To embed and automate Office documents with Visual Basic

When developing an application that uses data in an Office document, it might be beneficial to have that data presented directly within your Visual Basic application so that the user can see and edit the data without having to switch to the Office application. You can accomplish this in Visual Basic using the Edraw Office Viewer Component.

This article demonstrates how to dynamically create and Automate the Word, Excel or PowerPoint document using the Office Viwer Component.

1. Create a new Visual Baisc application. 

2. From the Project Menu select Components…

2. Select control “Edraw Office Viewer Component” in the controls table

3. Click the OK Button

4.  The control will now appear in your toolbox

5.   Drag and drop the control on your form

The control also supports a property called Dispatch that allows you to obtain a reference to the IDispatch interface of the embedded object. From this interface you can automate the object to perform tasks, edit parts of the document, or gather information about what a user has added or removed. For example, if you have a Word document open, you can use code that resembles the following to add a line of text.

OfficeViewer1.CreateNew “Word.Document”

Set objWord = OfficeViewer1.ActiveDocument

objWord.Content.Text = “This was added by Automation”

System Requirement for EDraw Office Viewer Component

The client needs Microsoft Office installed. If you want to view the Microsoft Word document, you need Microsoft Word installed in your cient.

If you are doing the web application with the EDraw office Viewer Component, you can choose the any type server such as Linux, Unix or Windows.

  • Support Office 2000/XP/2003/2007
  • Support Windows 2000/XP/2003/Vista

Note: Please test our product before you buy our product firstly.

How to add EDraw Office Viewer Component to your Access Form

How to add EDraw Office Viewer Component to your Access Form
1.      Open Access
2.      Create a new blank database
3.      Create a new form in Design View. You should now be in design mode
4.      From the Insert Menu select “ActiveX Control…”
5.      Select WordViewer.WordView and click the OK Button
6.      The control should appear on the form
7.      Resize and move the control on the form as desired
8.      Add a button to the form. If the wizard appears cancel it
9.      Right-click on the button to invoke the popup menu
10.    Select “Build Event…” then select Code Builder and click OK Button
11.    You should be inside the Visual Basic Development Environment
12.    Within the Command_Click enter the following code:
‘Note: Modify code to point to an existing Word Document file on your web server
EDrawOfficeViewer1.Open “http://www.ocxt.com/demo/sample.doc”
13.    Save your changes and close Visual Basic
14.    Save the form and close it
15.    Double-click on the form to run it.
16.    Click the button. The Word Document should appear!
17.    Experiment by changing the file path to a local file such as:
EDrawOfficeViewer1.Open “c:\test.xls”

How to add Office ActiveX Control to your Visual Basic 6.0 project

EDraw Office Viewer Component can be integrated in the Visual Basic 6.0 project. It will help the developers to open office document in a form with the customize size. You can call the open method to open the word, excel, powerpoint, visio, project document from the local disk or a remote server.

1. From the Project Menu select Components…
2. Select control “EDraw Office Viewer Component” in the controls table
3. Click the OK Button
4. The control will now appear in your toolbox
5. Drag and drop the control on your form

Then you can call its methods and properties. You can also active the event in the component.

View Office Files in your .NET project

How to add EDraw Office Viewer Component to your .NET project
1.      Open .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 “http://www.ocxt.com/demo/sample.doc”
10.    Change the anchor property of EDrawOfficeViewer1 to Top, Bottom, Left, Right

11.    Run the application and click on the button. The Word Document should appear!

The EDraw Office Viewer Componet allows the developers to view office files (Microsoft Word, Excel, PowerPoint, Visio, Project) in .NET project.

Solved Boundary Errors When Upload a File with Office Viewer OCX

It’s important to solve the boundary errors when uploading a file to a server via HTTP. Now EDraw Office Viewer Component has changed the upload method and added a boundary process for the uploaded file and parameter.

The boundary error could be exploited by remote attackers to cause a denial.  EDraw suggests all the user updated to the latest version if they are developping a web application with the office viewer ocx but they have the vulnerable server.

Insert the picture into the MSWord document

Q: Can I use the AxOfficeViewer1.WordInsertPicture in web application.I want to insert a picture from the server dynamically to the opened word document(using openfromstream). Please let me know how do I achieve this.

A: The method AxOfficeViewer1.WordInsertPicture can insert an image from the local disk. You can’t insert an image from the remote server. So you need to download the image from remote server then insert it again. You can use the HttpDownloadFileToTempDir method in the EDraw Office Viewer Component to download the picture file.

Creating the CAB files for Office Viewer Component

If your control will be used on web pages, it is very convenient to distribute it as a .cab (cabinet file)This is a compressed archive – the classic use is to put the .cab file on a web server, and refer to it from web pages that host the control. Internet Explorer is smart enough to download and install the control if it is not already installed on the client machine, and to selectively update any files that are newer in the .cab file. Microsoft provides Makecab.exe for creating and editing .cab files.

In the install directory you can find the CAB file. We have created it for you. But if you want to create a new CAB file with the Office Viewer Component, you can download the CabArc.Exe from Microsoft official site.

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

How to insert an Office document directly into my Visual Basic form

Microsoft Excel, Microsoft PowerPoint and Microsoft Word support both Object Linking and Embedding (OLE) and ActiveX Document containment. If you want to “host” an Office document in Visual Basic, you can use the OLE Container control and OLE. You can also use the WebBrowser control included with Microsoft Internet Explorer to provide basic ActiveX Document containment. But the simplest and effective method is using the Edraw office Viewer Component. It is a script safe office viewer component. Support more customize methods and events.