Office Viewer ActiveX Control 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.
Support Office 97, Office 2000, Office 2003, or Office 2007 in Windows OS.
How to add Edraw 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
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
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:
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"
3. Save documents
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:
'Let user edit the document, then save it.
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
You can close the currently open document.
5. Returns/sets the titlebar caption
6. Show or hide the titlebar
7. Show or hide the toolbars
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
9. Show Dialog
10. Returns/sets the border style for the control
11. Returns/Sets the color of the control
12. Get Dispatch
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:
Dim oDoc As Word.Document
Set oDoc = EOffice.Dispatch
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
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
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