Basic Wrapper Method for Automating Office

> Edraw Tip > Basic Wrapper Method for Automating Office
author
Posted by James Freeman |
The office control is designed to handle specific issues that make using ActiveX documents from a non-top-level host window difficult, and serves as a starting place for constructing your own embedded object file viewer or editor as an ActiveX control.

Edraw Office Viewer Component contains a standard ActiveX control that acts as an ActiveX document container for hosting MS Word, MS Excel and MS PowerPoint in a custom form or Web page. The control is lightweight and flexible, and gives developers new possibilities to use Office program in a custom solution.

Download Office Viewer Component and View Sample Projects

Boolean CreateNew(BSTR ProgID)

Creates a new, empty document.
ProgID: Word.Application, Excel.Application, PowerPoint.Application.

Example
The following VB Script shows how to open Word.
Sub NewDoc_Example()
EDOffice.CreateNew "Word.Application"
End Sub

Boolean Open(BSTR FileName, [in, optional] VARIANT ProgID)

Opens the specified document.
FileName: The name of the document (needs the full paths or url).
ProgID: Word.Application, Excel.Application, PowerPoint.Application.

Example
The following VB Script shows how to open Word file.
Sub LoadFile_Example()
EDOffice.Open "c:\test.xlsx", "Excel.Application"
End Sub

Sub LoadURL_Example()
EDOffice.Open "http://www.ocxt.com/demo/samples/sample.xls", "Excel.Application"
End Sub

Boolean Save()

Saves the specified document. If the document hasn't been saved before, the Save As dialog box prompts the user with a file name.
boolean SaveAs([in] BSTR FilePath, [in, optional] VARIANT FileFormat)

Saves the document to specified location with the specified format.
FilePath: The name for the document. If a document with the specified file name already exists, the document is overwritten without the user being prompted first.
FileFormat: The format in which the document is saved. Can be any WdSaveFormat constant.

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
}WdSaveFormat;

enum XlFileFormat
{
xlAddIn = 18,
xlCSV = 6,
xlCSVMac = 22,
xlCSVMSDOS = 24,
xlCSVWindows = 23,
xlDBF2 = 7,
xlDBF3 = 8,
xlDBF4 = 11,
xlDIF = 9,
xlExcel2 = 16,
xlExcel2FarEast = 27,
xlExcel3 = 29,
xlExcel4 = 33,
xlExcel5 = 39,
xlExcel7 = 39,
xlExcel9795 = 43,
xlExcel4Workbook = 35,
xlIntlAddIn = 26,
xlIntlMacro = 25,
xlWorkbookNormal = -4143,
xlSYLK = 2,
xlTemplate = 17,
xlCurrentPlatformText = -4158,
xlTextMac = 19,
xlTextMSDOS = 21,
xlTextPrinter = 36,
xlTextWindows = 20,
xlWJ2WD1 = 14,
xlWK1 = 5,
xlWK1ALL = 31,
xlWK1FMT = 30,
xlWK3 = 15,
xlWK4 = 38,
xlWK3FM3 = 32,
xlWKS = 4,
xlWorks2FarEast = 28,
xlWQ1 = 34,
xlWJ3 = 40,
xlWJ3FJ3 = 41,
xlUnicodeText = 42,
xlHtml = 44
}XlFileFormat;

enum PpSaveAsFileType
{
ppSaveAsPresentation = 1,
ppSaveAsPowerPoint7 = 2,
ppSaveAsPowerPoint4 = 3,
ppSaveAsPowerPoint3 = 4,
ppSaveAsTemplate = 5,
ppSaveAsRTF = 6,
ppSaveAsShow = 7,
ppSaveAsAddIn = 8,
ppSaveAsPowerPoint4FarEast = 10,
ppSaveAsDefault = 11,
ppSaveAsHTML = 12,
ppSaveAsHTMLv3 = 13,
ppSaveAsHTMLDual = 14,
ppSaveAsMetaFile = 15,
ppSaveAsGIF = 16,
ppSaveAsJPG = 17,
ppSaveAsPNG = 18,
ppSaveAsBMP = 19,
ppSaveAsOpenXMLPresentation = 24,
ppSaveAsOpenXMLPresentationMacroEnabled = 25,
ppSaveAsOpenXMLShow = 28,
ppSaveAsOpenXMLShowMacroEnabled = 29,
ppSaveAsOpenXMLTemplate = 26,
ppSaveAsOpenXMLTemplateMacroEnabled = 27,
}PpSaveAsFileType;

Example

The following VB Script shows how to save as a Word document.
Sub SaveAs_Example()
EDOffice.SaveAs "c:\test.xls"
End Sub

Boolean CloseDoc([in, optional] VARIANT SaveChanges)

Closes the specified document or documents.
SaveChanges: Specifies the save action for the document.

Boolean IsDirty()

Returns True/False if file has been altered or needs to save.

Boolean IsOpened()

Returns True/Fase if file has been opened.

Boolean OpenFileDialog([in, optional] VARIANT Filter)

Calls the standard file dialog to open the office document.
Filter: The file filter string.

Example
The following VB Script shows how to open only the docx file dialog.
Sub OpenFileDialog_Example()
EDOffice.OpenFileDialog "Microsoft Excel Files(*.xl;*.xlsx;*.xlsb;*.xlam;*.xltx;*.xltm;*.xls;*.xlt;*.xla;*.xlm;*.xlw)|*.xl;*.xlsx;*.xlsb;*.xlam;*.xltx;*.xltm;*.xls;*.xlt;*.xla;*.xlm;*.xlw||"
End Sub

Boolean SaveFileDialog([in, optional] VARIANT Filter)

Calls the standard file dialog to save the office document.
Filter: The file filter string.

Boolean PrintDialog()

Calls the print dialog.

Boolean PrintOut(WdPrintOutRange PrintRange, [in, optional] VARIANT FromPage, [in, optional] VARIANT ToPage, [in, optional] VARIANT Pages, [in, optional] VARIANT Copies)

Prints all or a part of the specified document with settings.
PrintRange: Optional Object. The page range. Can be any WdPrintOutRange constant.
FromPage: Optional Object. The starting page number when Range is set to wdPrintFromTo.
ToPage: Optional Object. The ending page number when Range is set to wdPrintFromTo.
Pages: Optional Object. The page numbers and page ranges to be printed, separated by commas. For example, "2, 6-10" prints page 2 and pages 6 through 10.
Copies: Optional Object. The number of copies to be printed.

enum WdPrintOutRange
{
wdPrintAllDocument = 0,
wdPrintSelection = 1,
wdPrintCurrentPage = 2,
wdPrintFromTo = 3,
wdPrintRangeOfPages = 4
};

Example
The following VB Script shows how to print the 3-6 page in a document.
Sub PrintOut_Example()
EDOffice.PrintOut 3, 3, 6
End Sub

Boolean PrintPreview()

Starts a print preview.

Boolean PrintPreviewExit()

Exits a current print preview.

Do Office Automation with C#

Word Automation

Disables MS Word Standard Command

Disable Office Ribbon Button

Show/Hide Office Menu Bar

download EdrawMind
main page

Get Started! You Will Love This Easy-To-Use Diagram Software

EdrawMax is an advanced all-in-one diagramming tool for creating professional flowcharts, org charts, mind maps, network diagrams, UML diagrams, floor plans, electrical diagrams, science illustrations, and more. Just try it, you will love it!