Office OCX & Office Component

Embedding MS Word, Excel and PowerPoint with Edraw Office Viewer Component

Office Viewer OCX Code Guide

Office Viewer ActiveX Control acts as an ActiveX document container for hosting Office documents (including Microsoft Word, Microsoft Excel, Microsoft PowerPoint, Microsoft Project, and Microsoft Visio documents) in a custom form or Web page. The OCX is lightweight and flexible, and gives developers new possibilities for using Office in a custom solution.

Download link: http://www.ocxt.com/download/OfficeOCXSetup.exe

Sales online: https://www.regnow.com/softsell/nph-softsell.cgi?item=14621-2

Support Office 97, Office 2000, Office XP, or Office 2003 in Windows OS.

Embedding Word, Excel, PowerPoint with Office Viewer Component

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

1.       From the Project Menu select Components…

2.       Select control “Office Viewer ActiveX Control”

3.       Click the OK Button

4.       The control will now appear in your toolbox

5.       Drag and drop the control on your form

6.       You can set all properties such as Name, Border, Color, Font, Print, hide/show toolbar, hide/show caption, etc.

7.       There are some methods and properties to operator the office control.

 

Code a solution using the control

The control is very customizable. You can change the color scheme of any of the control elements, as well as determine the border type and a custom caption. These can be set at run time or design time as needed.

1.        Create new documents

HRESULT CreateNew([in] BSTR ProgId);

Description: Creates a new document based on the ProgID.

Office Document Type:

Excel Spreadsheet: progID is “Excel.Sheet”;

Excel Chart: ProgID is “Excel.Chart”;

PowerPoint Presentation: ProgID is “PowerPoint.Show”;

Project: ProgID is “MSProject.Project”;

Visio Drawing: ProgID is “Visio.Drawing”;

Word Document: ProgID is “Word.Document”.

The component allows you to create new documents for any ActiveX document type that is registered on the system. A user can use the built-in functionality of the New dialog box from the menu, or use a custom function that you provide to create new documents.

The CreateNew method on the control allows you to build your own method to start new documents. The method takes either a Programmatic Identifier (ProgId) of the ActiveX document type that you want to start, or the path to an Office template file.

If the ProgId or template is not recognized or the server cannot be started, you receive one of the standard error messages that are discussed in the "Custom Error Messages" section.

2.        Open documents

Function: HRESULT Open([in] VARIANT Document, [in, optional] VARIANT ReadOnly, [in, optional] VARIANT ProgId, [in, optional] VARIANT WebUsername, [in, optional] VARIANT WebPassword);

Description: Opens a document from a file, URL, or Automation object.

You can also open and edit Office documents that exist on a local drive, Universal Naming Convention (UNC) share, or Web folder. A standard Open dialog box that can be displayed by the user or called by code has been provided, and allows the user to find and select a file to open. You can also call the Open method directly and give the control a specific file to open.

Open takes either a qualified file path or a URL to a file on a remote Web server. The control attempts to gain write access to the file and keep it locked for editing unless you pass True for the ReadOnly parameter. For example, the following code opens a local file and keeps a lock on it for editing:

EOffice.Open "C:\TestBook.xls"

If you want to open a file that is not associated with an Office application but that can be loaded with Office, you can specify an alternate ProgId in the Open method. For example, to open a plain text file in Word, you can use code that resembles the following:

EOffice.Open "C:\Plain.txt", , "Word.Document"

If you combine this ability with a URL, you can use code that resembles the following to open the resulting HTML streamed back from an ASP file and have it display as data inside of Excel:

EOffice.Open "https://secureserver/test/mytest.asp?id=123", True, "Excel.Sheet", "MyUserAccount", "MyPassword"

The user can then edit the results and save the file as a local file on disk, or save the file to the server as a new file in a Web folder.

In addition you can use the Open method to create and display a copy of a document you created via Automation. For example, the following code would create a new Word document, and then display it.

Dim oWordApp As Object

Dim oWordDoc As Object

Set oWordApp = CreateObject("Word.Application")

Set oWordDoc = oWordApp.Documents.Add

oWordDoc.Content.Text = "Hello World"

EOffice.Open oWordDoc

   3.        Save documents

HRESULT Save([in, optional] VARIANT SaveAsDocument, [in, optional] VARIANT OverwriteExisting, [in, optional] VARIANT WebUsername, [in, optional] VARIANT WebPassword);

Description: Saves the document to specified location or its original location.

To save a document, you can use the menu or call the Save method. The Save method acts both as a simple Save command and as a SaveAs command, depending on whether you pass a file location for the first parameter. If the current file was opened read-only and you do not specify a save location, a read-only error occurs. For more information, see the "Custom Error Messages" section.

You can also save to a Web folder on a remote server if that server supports either Microsoft FrontPage Server Extensions (FPSE) or the Web Distributing Authoring and Versioning (WebDAV) protocol extension for HTTP. The following code shows a new file that is saved to a remote file server:

EOffice.CreateNew "PowerPoint.Show"

'Let user edit the document, then save it.

EOffice.Save "http://myserver/mypresentations/test.ppt"

By default, if a file already exists at the given location, you receive an error message. However, by setting the OverwriteExisting parameter to True, you can explicitly tell the control to overwrite the file.

   4.        Close Document

HRESULT Close();

Description: Closes the currently open document.

   You can close the currently open document.

EOffice.Close

   5.        Returns/sets the titlebar caption

HRESULT Caption([in] BSTR bstr);

HRESULT Caption([out,retval] BSTR* pbstr);

Description: Returns/sets the titlebar caption.

   6.        Show or hide the titlebar

HRESULT Titlebar([in] boolean vbool);

HRESULT Titlebar([out,retval] boolean* pbool);

Description: Returns/sets whether the titlebar should be displayed.

   7.        Show or hide the toolbars

HRESULT Toolbars([in] boolean vbool);

HRESULT Toolbars([out,retval] boolean* pbool);

Description: Returns/sets whether toolbars should be displayed.

You can programmatically show or hide the title bar, menu bar, or toolbars by setting these parameters to True or False. This may be useful when you try to restrict user actions or control the appearance of the document while it is embedded.

   8.        PrintOut document

HRESULT PrintOut([in, optional] VARIANT PromptToSelectPrinter);

Description: Prints the currently open document.

  9.        Show Dialog

HRESULT ShowDialog([in] ShowDialogType DlgType);

Description: Displays a modal dialog of the given type for user action.

typedef enum ShowDialogType

{

        DialogNew = 0,

        DialogOpen,

        DialogSave,

        DialogSaveCopy,

        DialogPrint,

        DialogPageSetup,

        DialogProperties

} ShowDialogType;

  10.    Returns/sets the border style for the control

HRESULT BorderStyle([in] BorderStyle style);

HRESULT BorderStyle([out, retval] BorderStyle* pstyle);

typedef enum BorderStyle

{

        BorderNone = 0,

        BorderFlat,

        Border3D,

        Border3DThin

 } BorderStyle;

  11.    Returns/Sets the color of the control

Returns/sets the border color of the control.

HRESULT BorderColor([in] OLE_COLOR clr);

HRESULT BorderColor([out,retval] OLE_COLOR* pclr);

 

Returns/sets the background color of the control.

HRESULT BackColor([in] OLE_COLOR clr);

HRESULT BackColor([out,retval] OLE_COLOR* pclr);

 

Returns/sets the foreground color of the control.

HRESULT ForeColor([in]OLE_COLOR clr);

HRESULT ForeColor([out,retval]OLE_COLOR* pclr);

 

Returns/sets the color of the titlebar.

HRESULT TitlebarColor([in] OLE_COLOR clr);

HRESULT TitlebarColor([out,retval] OLE_COLOR* pclr);

 

Returns/sets the color of text for the titlebar.

HRESULT TitlebarTextColor([in] OLE_COLOR clr);

HRESULT TitlebarTextColor([out,retval] OLE_COLOR* pclr);

   12.    Active Document

HRESULT ActiveDocument([out,retval] IDispatch** ppdisp);

Description: Returns the Automation interface of the document object.

The control also supports a property called ActiveDocument 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:

Dim oDoc As Word.Document

Set oDoc = EOffice.ActiveDocument

oDoc.Content.Text = "This was added by Automation"

The ability to control the object while the object is embedded is very powerful.

NOTE: Automation of the control from HTML script over the Web can be very dangerous to a user, so the control has purposely been marked as NOT "Safe for Scripting". Users who use the control in Web pages may be prompted to enable scripts before they use this control. This is required for proper security.

   13.    Handle your own file commands

Function: HRESULT EnableFileCommand([in] FileCommandType Item, [in] boolean vbool);

HRESULT EnableFileCommand([in] FileCommandType Item, [out,retval] boolean* pbool);

Description: Returns/sets whether a specific action item is enabled.

typedef enum FileCommandType

{

        FileNew = 0,

        FileOpen,

        FileClose,

        FileSave,

        FileSaveAs,

        FilePrint,

        FilePageSetup,

        FileProperties

} FileCommandType;

Every time a user selects an item from the File menu or an item on a toolbar that is associated with a file command, the OnFileCommand event is raised. The event allows you to override the default behavior for the control and supply your own custom actions and dialog boxes to do normal file operations.

You can also enable or disable items on the File menu by using the EnableFileCommand property. For example, the following code disables the Print command, and then traps print calls to prevent a user from printing:

Private Sub Form_Load()

EOffice.EnableFileCommand(FilePrint) = False

End Sub

Private Sub EOffice_OnFileCommand(ByVal Item

As _Framer.FileCommandType, Cancel As Boolean)

If Item = FilePrint Then

MsgBox "You asked to print, but I won't allow it."

Cancel = True

End If

End Sub

 Download Office Viewer Reference Manual

Home | Products | Order | Download | Support | Exchange Link | Contact | Site Map

Copyright © 2004-2012 Office OCX. All rights reserved.