|
Word Viewer OCX v3.0 Reference 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 Function void CreateNew(); Description: Creates a new word document.
2. Open Local documents Function: boolean Open(BSTR Path); Description: Opens a document from a local file. You can also open and edit Office documents that exist on a local drive. You can call the Open method directly and give the control a specific file to open.
3. Open Web folder Function: boolean OpenWebFile(BSTR FileUrl); Description: Opens a document from a web folder. You can also open and edit Office documents that exist on web folder. Open takes either a qualified file path or a URL to a file on a remote Web server. For example, the following code opens a web file. OA1. OpenWebFile "http://www.officeocx.com/demo/sample.doc"
4. Show Open File Dialog Function: boolean OpenLocalDialog (); Description: Opens a document from a local file with the standard open file dialog. 5. Save Documents Function: boolean Save(BSTR strPath); Description: Save the opened file to specify a save location.
6. Save As Function: boolean SaveAs(BSTR strPath, long nFileType); Description: Save the opened file to common word document formats. Office Document Type: enum WdSaveFormat { wdFormatDocument = 0, wdFormatTemplate = 1, wdFormatText = 2, wdFormatTextLineBreaks = 3, wdFormatDOSText = 4, wdFormatDOSTextLineBreaks = 5, wdFormatRTF = 6, wdFormatUnicodeText = 7, wdFormatEncodedText = 7, wdFormatHTML = 8, wdFormatWebArchive = 9, wdFormatFilteredHTML = 10, wdFormatXML = 11 }; If you want to save the opened word document into the html format, you can write codes as follow: OA1. SaveAs “c:\1.html”, 10
7. Show Save File Dialog Function: void SaveLocalDialog(); Description: Saves a document to a local file with the standard save file dialog.
8. Save Web Folder Function: void SaveWebFile(BSTR ServerUrl); Description: Saves a document to a web folder. Note: you must write a Stream receive method in the server. For Example with ASP code: (Please review the full sample codes in the install package) private void Save() { Stream stream = Request.InputStream; StreamReader sr = new StreamReader(stream); string uploadString = sr.ReadToEnd(); sr.Close(); stream.Close(); try { FileStream fs = new FileStream(@"c:\Test1\test.doc", FileMode.OpenOrCreate ,FileAccess.Write,FileShare.None); byte[] _fs = Request.ContentEncoding.GetBytes(uploadString); fs.Write(_fs,0,_fs.Length); fs.Flush(); fs.Close(); } catch(Exception ex) { Response.Write(ex.ToString()); } } You can also write the stream receive method with other language in the server. Review the PHP codes in the follow note.
9. Close Document Function: boolean Close(); Description: Closes the currently open document.
10. Read-Only Function: void SetReadOnly(boolean bReadOnly); Description: Set the read-only state It is possible to change the viewer in a way it can only view the word documents. OA1.SetReadOnly true It is possible to change the viewer in a way it can edit the word documents. OA1.SetReadOnly false
11. Print Out Function: void Print(); Description: Print out the document
12. Show or hide the toolbars Function: void ShowToolbars(boolean bShow); Description: Show/Hide whether toolbars should be displayed. You can open an office document without toolbars as follow: OA1. Open “c:\\sample.xls” OA1.ShowToolbars False
13. Is Modified Function boo IsDirty(); Description: Returns True/False if file has been altered or needs save. It returns true when the document has been altered or needs save. BOOL bDirty = OA1.IsDirty
14. Upload and Download file Function: 1) boolean HttpUploadFile(BSTR ServerUrl, BSTR LocalFilePath); 2) boolean HttpDownloadFile(BSTR ServerUrl, BSTR LocalFileUrl); 3) boolean FTPUploadFile(BSTR ServerUrl, BSTR LocalFilePath, BSTR UserName, BSTR Password); 4) boolean FTPDownloadFile(BSTR ServerUrl, BSTR LocalFilePath, BSTR UserName, BSTR Password); Description: Upload and download the file with Http or FTP method. The ActiveX Control supports upload and download file with HTTP or FTP method. Download file with HTTP method: OA1.HttpDownloadFile “http://www.officeocx.com/demo/Samples.doc” Download file with FTP method: OA1.FTPDownloadFile “http://www.officeocx.com/demo/Samples.doc, “c:\Samples.doc”, “”, “” Upload file with HTTP method: OA1.HttpUploadFile “http://www.officeocx.com/demo/SaveFile.php”, “c:\Samples.doc” Note: you must write a Stream receive method in the server. For Example with PHP code: <?php /* PUT data comes in on the stdin stream */ $putdata = fopen("php://stdin", "r"); /* Open a file for writing */ $fp = fopen("test.edd", "w"); /* Read the data 1 KB at a time and write to the file */ while ($data = fread($putdata, 1024)) fwrite($fp, $data); /* Close the streams */ fclose($fp); fclose($putdata); ?> Download file with HTTP method: OA1.HTTPDownloadFile “http://www.officeocx.com/demo/Samples.doc”, “c:\Samples.doc”
15. Automating Office Document Function: IDispatch* GetIDispatch(); Description: Returns the Automation interface of the document object. The control also supports a property called GetIDispatch 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 = OA1. GetIDispatch oDoc.Content.Text = "This was added by Automation" The ability to control the object while the object is embedded is very powerful. |