What is an ActiveX Control

ActiveX is a software component of Microsoft Windows. If you have Internet Explorer, then ActiveX is already installed on your computer. ActiveX controls are small programs, sometimes called add-ons that are used on the Internet. They can enhance your browsing experience.

You can decide whether or not you want to download ActiveX controls on a case by case basis. Some websites require you to install ActiveX controls to view or perform certain tasks on them, but there can be potential risks. Cybercriminals may develop their own ActiveX controls and can damage computers if users visit web pages that contain malicious ActiveX software.

When you visit a site that uses an ActiveX control, Internet Explorer asks if you want to install the ActiveX control. You can then choose to install the ActiveX control or learn about the risks. Click
Don't run if you do not trust the website and publisher.

Create C# Windows control in Visual Studio

After adding a Windows Control library project, right click on control---> then rename the control "ControlWordExcel".

Open AssemblyInfo.cs and set [assembly: ComVisible(true)].

Right click on references folder--->click on Add References..., then select the following references: Microsoft.Office.Core in (COM tab),
Microsoft.Office.Interop.Excel, and Microsoft.Office.Interop.Word in (.NET tab), all references version 12.0.0.0, Microsoft Office 12.0 Object Library (COM tab).

Right click on ControWordExcel Project--->select Properties--->click on Signing tab.

#region "API Calling"
///<summary>/// Retrieves a handle to the top-level window whose
/// class name and window name match the specified strings.
///</summary>///<paramname="strclassName">calss name</param>///<paramname="strWindowName">window class name</param>///<returns>If the function succeeds, the return value is a handle to the window
/// that has the specified class name and window name.</returns>
[DllImport("user32.dll")]
publicstaticexternint FindWindow(string strclassName, string strWindowName);
///<summary>/// Changes the parent window of the specified child window.
///</summary>///<paramname="hWndChild">A handle to the child window</param>///<paramname="hWndNewParent">A handle to the new parent window. If this parameter
/// is NULL, the desktop window becomes the new parent window.</param>///<returns>If the function succeeds, the return value
/// is a handle to the previous parent window</returns>
[DllImport("user32.dll")]
staticexternint SetParent(int hWndChild, int hWndNewParent);
///<summary>/// Changes the size, position, and Z order of a child, pop-up, or top-level window.
/// These windows are ordered according to their appearance on the screen.
/// The topmost window receives the highest rank and is the first window in the Z order
///</summary>///<paramname="hWnd">A handle to the window</param>///<paramname="hWndInsertAfter">A handle to the window
/// to precede the positioned window in the Z order.</param>///<paramname="X">position of the left side of the window</param>///<paramname="Y">position of the top of the window</param>///<paramname="cx">width of the window</param>///<paramname="cy">height of the window</param>///<paramname="uFlags">/// window sizing and positioning flags.
/// SWP_DRAWFRAME(0x20):Draws a frame (defined in the window's class description) around the window
/// SWP_NOMOVE(0x2):Retains the current position (ignores X and Y parameters)
/// SWP_NOZORDER(0x4):Retains the current Z order (ignores the hWndInsertAfter parameter).
///</param>///<returns>If the function succeeds, the return value is nonzero</returns>
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
staticexternbool SetWindowPos(int hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
#endregion

Use namespace System.Runtime.InteropServices. Create objects of MS Word Microsoft.Office.Interop.Word.Document and Microsoft.Office.Interop.Word.ApplicationClass document and application. Next object for MS Excel Microsoft.Office.Interop.Excel.Workbook and Microsoft.Office.Interop.Excel.ApplicationClass for Excel Workbook and application.

Now write code to open Word or Excel in the control. OpenDocument takes two parameters sMyFileName and sAppType of type string. According to passing type, the application class object is created. Then call
the FindWindow function takes two parameters: class name and window name. For MS-Word, use class name Opusapp and MS-Excel uses class name XLMAIN.
The next parameter is null for window name.

FindWindow returns a non zero value if success, pass this value in
the SetParent function. Next call the Open function for Word or Excel according to sAppType, whose value is W or E. Next, pass all missing values by creating object oMissing = System.Reflection.Missing.Value;.
Open for MS-Word takes value by ref, but in MS-Excel,
the Open function is not required to pass ref. To view MS-Word, use property Visible=true and call
the Activate() function of the Microsoft.Office.Interop.Word.ApplicationClass object. Same for
the Microsoft.Office.Interop.Excel.ApplicationClass object, use property Visible=true and UserControl = true. Finally, call SetWindowPos to set Word or Excel application in control
at proper location.

Write function to close Word or Excel application with save. For this, the CloseAndSave function takes
a parameter sAppType
of type string. According to this parameter, we call the function Save(), Close(), and Quit(). Finally assign a null
value to the object and also pass zero value to the iWindow object of type int.

Now write file open dialog code. For this, create the BrowseFileDialog() function
that takes a parameter sAppType of type string.
According to type, set filter for the file open dialog, get filename, and call OpenDocument().

Finally save and build the project, and view the .dll file in the Release or
Debug folder. The next step is to register assembly with codebase.

Control Register (Regasm.exe)

The Assembly Registration tool reads the metadata within an assembly and adds the necessary entries to the
Registry, which allows COM clients to create
.NET Framework classes transparently. Create a Codebase entry in the Registry, use /codebase options. The Codebase entry specifies
the file path for an assembly that is not installed in the Global Assembly Cache. To unregister, use
the /u option.

Create New Web Site

Open default.aspx and write code to display control.

Create new JavaScript file

Create new jscontrol.js file on root location, to handle JavaScript functions related to
the control.

Finally build code and run the website.

Create Digital Certificate

The Certificate Creation tool generates X.509 certificates for testing purposes only. It creates a public and private key pair for digital signatures
and stores it in a certificate file. This tool also associates the key pair with a specified publisher's name and creates an X.509 certificate that
binds a user-specified name to the public part of the key pair.

-r Creates a self-signed certificate, -sv specifies the subject's
.pvk private key file. The file is created if none exists.

-n Specifies the subject's certificate name. This name must conform to the X.500 standard. The simplest method is to specify
the name in double quotes, preceded by CN=; for example, "CN=myName".

-b Specifies the start of the validity period. Defaults to the certificate's creation date (mm/dd/yyyy).

-e Specifies the end of the validity period date (mm/dd/yyyy). Defaults to 12/31/2039 11:59:59 GMT.

Open VS command prompt and type the makecert command to create a digital certificate, enter any password for digital certificate, press
OK.

The next step again will ask password type same, press ok.

Go to Visual Studio command prompt location, and you will find three files: .cer, .pfx, and .pvk.

Signing Digital Certificate

The Sign Tool is a command-line tool that digitally signs files, verifies signatures in files, or time stamps files.
signwizard launches the signing wizard. Only a single file can be specified for the file name command-line argument.

To sign a digital certificate for all files .dll, .msi, and .cab, repeat
the same step for all files.
Open the VS command prompt and type the signtool.exe signwizard command, press Enter. A wizard will appear and follow the below steps.

ActiveX control setting

If you find any problems viewing the control, please add the URL in your trusted site zone in
Security tab and unchecked
form Require http server verification (https:) option, then Add and Close.

Installation Digital Certificate

Publish your web application on IIS, and run on localhost. After running, the control will appear on browser.
Allow permission and install certificate,
click on unknown publisher, then click on View certificate, click on Install certificate.

View Installed Control in Manage Add-ons

Open IE browser, then Tools menu-->Internet Options--->Program tab-->click on Manage Add-ons--->select All add-ons in dropdown box, then view our control and
double click to view more information.

Uninstall ActiveX control

Conclusion

We covered the following topics: how to call Word/Excel application in ASP.NET web, how to create
an ActiveX control, how to register a control, how to create
a setup of control for COM, how to create a .cab file, how to create a Digital Certificate, how to sign
a control, how to publish a control, how to install
a Digital Certificate, how to set IE ActiveX control settings, View certificate, and check manage add-ons.

Hello and congratulations for the article (It´s really wonderfull). I have a problem, when i press in close button in the word document my web application also is closed.
Is there any way to avoid pressing exit button or that when y press it my web app don´t be closed?

I have tested whith the Application_DocumentBeforeClose event, but it´s not fired from web when i press int the word exit button (x) (but it´s fired when i press on the button that launch then fclosedocument event).

Error 1 Cannot import the following key file: wordcontrol.pfx. The key file may be password protected. To correct this, try to import the certificate again or manually install the certificate to the Strong Name CSP with the following key container name: VS_KEY_11AD4EE456A1247D ControlWordExcel

Ok found a bit more information, you can delete the PFX key from solution and create a new certificate. Also note that for Windows 8 kits, they have removed the Signtool wizard.

I was able to get the control to show up in WPF, but very few of the functions in the toolbar work correctly, for example if one picks a shape which works ok selection wise, it doesn't do anything when trying to draw on the spreadsheet.

Yes cell focus already available in sheet but not show,
when we press the key then focus will appear.
Due to time problem i am unable to find this solution,
If in future i have time then i will try to find this solution.