Is there are any library needed for Office Automation? If yes what is it?

Only install MS Office in the computer.

Embed WORD, EXCEL, POWERPOINT and PDF Easily

It is now possible to embed various Microsoft applications (WORD, EXCEL, POWERPOINT) files as well as PDF into a web page or custom form with Edraw Office Viewer Component. You can download the full install package from http://www.ocxt.com. Then review lot’s example projects in the install folder. The component also provides some office automation interfaces.

More Information

Edraw Office Viewer Component for Hosting Office Documents

Embedding ms office in C# Forms

Host Microsoft Office Application in .NET

 

 

Automate Excel using Excel OCX and worksheet functions

This article describes how to automate Microsoft Excel 97, Microsoft Excel 2000, Microsoft Excel 2002, or Microsoft Excel 2003 by using the Edraw Office Viewer Component. Specifically, this article illustrates how to get the worksheet dispatch interface.

1. Open Visual Studio .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.xls

10. Change the anchor property of EdrawOfficeViewer1 to Top, Bottom, Left, Right

11. Run the application and click on the button. The Excel program should appear!

For this automation process we need to follow the below steps

1. Add a referrence to the Microsoft Excel object library COM component.

2. Add the namespace Excel

3. Instantiate the class Excel.ApplicationClass as below
Excel.Application xl=EdrawOfficeViewer1.Application

4. To read cell(s) in the worksheet,

Excel.Sheets xlsheets = wb.Sheets; //Get the sheets from workbook
Excel.Worksheet excelWorksheet = (Excel.Worksheet)xlsheets[1]; //Select the first sheet
Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range(”B4:FZ4″, Type.Missing); //Select a range of cells
Excel.Range excelCell2 = (Excel.Range)excelWorksheet.get_Range(”A5:A5″, Type.Missing); //Select a single cell
Console.WriteLine(excelCell2.Cells.Value2.ToString()); //Print the value of the cell for a single cell selection
System.Array myvalues = (System.Array)excelCell.Cells.Value2; //Assign it to an array
string[] strArray = ConvertToStringArray(myvalues); //Convert array into String array
foreach (string str in strArray)
Console.WriteLine(” Text in Cell ” + str); //Loop through the array to print the values in the cell

Conclusion

Excel is a great tool to work with. When it comes to automating, we need to consider many things. Always remember to quit the excel application in code before exiting. If not, the memory consumed by the excel application will not be freed up.

What is the Office Automation

Productivity is the biggest challenge for any company or small business. Increasing productivity is Microsoft Office’s goal. In that spirit, Microsoft has sought to include features that allow corporate and small business developers to produce quality in-house applications that harness the power and versatility of Office in order to quickly build customized solutions. Automation is the key to this strategy.

Automation (formerly known as OLE Automation) is the programmatic manipulation of any program or component based on certain rules following the Component Object Model (COM). Automation was first pioneered and developed by Visual Basic and Office as a way to allow developers to extend and control the Office environment both internally and externally.

While it is not necessary to know all the details about how Automation works, you should familiarize yourself with some of the key terms and concepts. Namely, an Automation client is any piece of code that creates and calls an instance of a COM server that exposes an “object model.” Object models are an arrangement of classes that expose functionality through various properties and methods and enable programmers to control a product. A particular instance of one of these classes is an object and the properties and methods comprise its interface. The object model for each Office application is different, and must be learned before Automation code can be written.

The Edraw Office Viewer Component provides some simple automation functions. You can call them directly. Or you can get the dispatch interface then do the automation by yourself. The component exposes the Application and ActiveDocument interfaces.

Web Office Component + JavaScript + VBA

It’s easy to create a web office application with the EDraw Office Viewer Component. The component has the good support to do the VBA. You can write the Javascript or the vbscript to realize it. Follow the sample codes.

<script language=”JScript” for=WebOffice event=”OnDocumentOpened(vFileName,vObject)”>
OnDocumentOpened(vFileName,vObject); //opened document event
</script>

<script language=”JScript” for=WebOffice event=”OnDocumentClosed()”>
OnDocumentClosed(); //Closed document event
</script>
<script language=javascript>
function OnDocumentOpened(vFileName,vObject)
{…}

function Load(){
webform.OfficeViewer.Open=”<%=mFileName%>”;
}

function UnLoad(){
try{
webform.OfficeViewer.Close();
}catch(e){}
}

function LoadDocument(){
webform.OfficeViewer.OpenOfficeFileDialog();
}

function SaveDocument(){
webform.OfficeViewer.Save();
}
//Returns/sets whether toolbars should be displayed.
function WebToolBars(Visible){
try{
webform.OfficeViewer.ToolBars=Visible;
}catch(e){}
}
//Returns/sets whether a menu bar should be displayed.
function WebMenuBar(Visible){
try{
webform.OfficeViewer.MenuBar=Visible;
}catch(e){}
}

//Shows/Hides the specified toolbar.
function WebToolsVisible(ToolName,Visible){
try{
webform.OfficeViewer.ActiveDocument.CommandBars(ToolName).Visible = Visible;
}catch(e){}
}

Enable ActiveX controls marked safe for scripting

If you can’t open the word document or excel workbook in your internet explore, you need check your IE setting. The simple method is to restore the IE security as Default setting.

Click on the “Tools” menu and select “Internet Options”. Click on the “Security” tab.

Click on the globe marked “Internet”. This will display a slider which, by default, is set to “Medium”. Click on the “custom” button and scroll down until you find the setting “Script ActiveX controls marked safe for scripting”. Enable this setting. Click OK as needed to save the setting.

Note: You computer needs MS office installed.

Signing EDraw Office Viewer ActiveX control

Downloading and running a piece of code from the Internet is a fundamentally dangerous act.

Microsoft attempted to ameliorate the problem through a standard called authenticode. This is a technology for signing files with a non-forgeable digital signature. Signing a file this way ensures that any alteration to the file is detectable, and the signer of the file can be positively identified.

To digitally sign a control, you will need to obtain a certificate from a certificate authority, which can be located by using the term “certificate authority” in a Web-based search engine. Follow the directions for signing controls from the certificate authority you decide to use.

If you do not sign and timestamp your executables and .cab files, Internet Explorer may display a warning message (which gets more threatening with each major rev of IE) to let the user know they are taking a risk if they continue with the installation. This is normally suppressed when the source is in the ‘Trusted Sites’ zone.

We provide the cab file and ocx file with the digital signature in the full verion. We allow the licensed users to sign the EDraw Office Viewer Component with your own digital signature too.

How to save the MS Office File to remote server with ASP

Please review the follow sample code to save the open Microsoft Office file in the EDraw Office Viewer Component to the remote server with the ASP code.

SaveDoc1.asp

<%@LANGUAGE=”VBSCRIPT” CODEPAGE=”936″%>
 <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
 <html>
 <head>
 <meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>
 <title>EDraw Office Viewer Component</title>
 </head>
 
 <body>
 <%readonly int enterCount = 12;
 string[] requestValues = new string[3];
 string newFile = Server.MapPath(”.”)+”\\Document\\” + DateTime.Now.ToFileTime().ToString() + “NewDoc.doc”;
 
 BinaryReader br = new BinaryReader(Request.InputStream);
 
 
 br.BaseStream.Seek(0,SeekOrigin.Begin);
 
 int enterNo = 0;
 int streamHeadLen = 0;
 while(br.BaseStream.Position < br.BaseStream.Length) {
 streamHeadLen ++;
 char c = (char)br.ReadByte();
 if(enterNo < enterCount){
 if(c == ‘\n’){
 enterNo ++;
 }
 }
 else{
 break;
 }
 }
 br.BaseStream.Seek(0,SeekOrigin.Begin);
 string strTemp = System.Text.UTF8Encoding.Default.GetString(br.ReadBytes(streamHeadLen - 1));
 
 string str =”";
 int index=0;
 string[] requestStrings = {”RecordID”,”UserID”};
 for(int i=0;i<requestStrings.Length;i++)
 {
 str = “Content-Disposition: form-data; name=\”"+requestStrings[i]+”\”\r\n\r\n”;
 index = strTemp.IndexOf(str) + str.Length;
 if( index != str.Length - 1)
 {
 for(int j=index;j<strTemp.Length;j++)
 {
 if(strTemp[j] != ‘\r’)
 this.requestValues[i] += strTemp[j];
 else
 break;
 }
 }
 }
 str = “; filename=\”;
 index = strTemp.IndexOf(str) + str.Length;
 if( index != str.Length - 1)
 {
 for(int j=index;j<strTemp.Length;j++)
 {
 if(strTemp[j] != ‘\r’)
 this.requestValues[2] += strTemp[j];
 else
 break;
 }
 }
 
 FileStream newDoc = new FileStream(newFile,FileMode.Create,FileAccess.Write);
 BinaryWriter bw = new BinaryWriter(newDoc);
 bw.BaseStream.Seek(0,SeekOrigin.End);
 while(br.BaseStream.Position < br.BaseStream.Length - 38)
 {
 bw.Write(br.ReadByte());
 }
 br.Close();
 bw.Flush();
 bw.Close();
 
 %>
 </body>
 </html> 

How to get the office file stream from database

Create Getfile.aspx.cs file
Add the namespace:
using System.Data.SqlClient;
using System.Data.SqlTypes;

Modify the Page_Load function:
protected void Page_Load(object sender, EventArgs e)
{
    int pid = Convert.ToInt32(Request["id"]);
    SqlConnection myConnection = new SqlConnection(”Data Source=\”localhost\”;Initial Catalog=\”demo\”;Persist Security Info=True;User ID=demo;Password=demo”);
    SqlCommand mycommand = myConnection.CreateCommand();
    myConnection.Open();
    mycommand.CommandText = “SELECT filedata ” +
    ” FROM Table_word WHERE (ID = ” + pid.ToString() + “)”;
    SqlDataReader myReader = mycommand.ExecuteReader();
    myReader.Read();
    SqlBinary binaryStream = myReader.GetSqlBinary(0);
    myReader.Close();
    myConnection.Close();
    Response.BinaryWrite(binaryStream.Value);
}
So you can get the file stream from the sql database. The component can open it as follow:
var str=window.location.search;
var pos_start=str.indexOf(”id”)+3;
if (pos_start == 2)return;
var fileid = “http://localhost/Getdc.aspx?id=” + str.substring(pos_start);
document.all.OfficeViewer.Open(fileid);