Free Code Snippets in C#, Net Framework, Office 365, ASP.Net,WPF, Visual Studio, SQL Server, Antivirus free
#

Download:

Datei 1: Excel_Test_Com.xlsm
Datei 2: Pdf_Text_Reader.dll
Datei 3: Pdf_Text_Reader.pdb
Datei 4: Pdf_Text_Reader.tlb
Datei 5: register.bat
Datei 6: unregister.bat
Datei 7: Class1.cs

Create COM Control for Office

 

This description shows how to create a COM Control and then use it in Microsoft Office.

The COM Control is created in C # in Visual Studio. The description contains the base code in C # for creating a COM control.

The COM Control is created in this example only as a .dll file and does not require visible operator control.

 

 

 

Subject:

Created in Android Studio C # or vb.net as Application type: Class Library

Application in: 

Microsoft Word, Excel, Access, Powerpoint

.Dll .tlb COM ActiveX

 

1) create a new project

In Visual Studio on File-> New-> Project

 

 

Then in the dialog: Select New Project:

Installed-> Windows Desktop-> Class Library (.NET Framework) in Visual C # or Vb.net

The project is called in the example: Pdf_Text_Reader

 

After creating the Class Library (.NET Framework)

 

Start Code C # under Class1.cs

Automatically created by Visual Studio

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Pdf_Text_Reader

{

    public class Class1

    {

    }

}

 

 

 

Basic settings for COM projects

For COM projects, you have to enable some COM options in Project-> Properties:

Application

Then on Assembly Information-> Make assembly COM-Visible

Build

Under Project-> Build: -> Enable Register for COM Interop

 

Signing

Under Project-> Signing-> Sign the assembly

On Chosse as strong name key file: <New ..> choose

 

Then enter a new key

Under Key File name: enter a new name freely definable

 

 

C #: COM code

 

1) under Using InteropServices

using System.Runtime.InteropServices;   

 

2) In the first block you define the accesses visible in MS Office like COM.Functions (..) (Methods in C #).

You simply copy the internal methods from the COM-Class

[InterfaceType(ComInterfaceType.InterfaceIsDual)]

[Guid("your-GUID-1")]

public interface _Visible_Methods

{

    //--------< _Visible_Methods >--------

    //*visible COM Methods of this Control under Office,Excel, Word

    //*simply repeat method from class

    string get_Hello();

 

    //--------</ _Visible_Methods >--------

}

 

Then you give the COM class the attributes ClassInterface, a GUID and a ProgID

[ClassInterface(ClassInterfaceType.None)]

[Guid("your-GUID-2")]

 

[ProgId("Pdf_Text_Reader")]

public class pdf_Reader : _Visible_Methods

{

..

}

 

 

C # code snippet

For COM Control

C # Code template for COM Control dll in MS Office (Excel, Word, PowerPoint ..)

using System;

using System.Runtime.InteropServices;

 

 

[InterfaceType(ComInterfaceType.InterfaceIsDual)]

[Guid("your-GUID1")]

public interface _Visible_Methods

{

    //--------< _Visible_Methods >--------

    //*visible COM Methods of this Control under Office,Excel, Word

    //*simply repeat method from class

    string get_Hello();

 

    //--------</ _Visible_Methods >--------

}

 

[ClassInterface(ClassInterfaceType.None)]

[Guid("your-GUID2")]

 

[ProgId("Pdf_Text_Reader")]

public class pdf_Reader : _Visible_Methods

{

    //----------------< class: pdf_Reader  >----------------

    //*internal method

    public string get_Hello()

    {

        //--------< get_Hello() >--------

 

        return "Hello, this Text is from the C# COM.dll. Code at under CodeDocu.com";

 

        //--------</ get_Hello() >--------

    }

 

    //----------------</ class: pdf_Reader  >----------------

}

 

 

Output files from Visual Studio

Runtime files

You need the files in the / bin / debug or / bin / release directory for the COM Control object.

The .tlb file is only created on the second call of Menu-> Build -> Build Solution.

All 3 files (COM.dll, .pdb, .tlb) must be copied to the distribution directory, which will be used for Office.

 

 

For testing or redistribution, you can copy the files from the project folder to a redistribution directory.

 

 

Register and Uninstall

Before you can use the COM files in MS Office, you have to register the COM_File.tlb file with register.bat in Windows 7 and Windows 10.

You have to call the register.bat as administrator.

 

In regster.exe registry.bat calls regasm.exe "full_path_your_COM_project.dll" with / tlb and / codebase as parameters.

 

Register.bat code

C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm.exe "C:\_Daten\Desktop\VS_Projects\ActiveX\Pdf_Text_Reader\_Test\Pdf_Text_Reader.dll" /tlb /codebase

Pause

 

unregister.bat

You need the Unregister.bat, if you want to remove the COM Control from the Windows computer.

Make sure that regasm.exe is run with / u and points to the xxx_your_com.dll file.

Unregister.bat code

C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm.exe /u "C:\_Daten\Desktop\VS_Projects\ActiveX\Pdf_Text_Reader\_Test\Pdf_Text_Reader.dll"

pause

 

Application in Excel

 

Under Microsoft MS Excel you include this COM file in the vba codebehind page.

You come to the vba side with Alt-F11.

 

Create a reference:

Then you have to go under Menu-> Extras-> References with Browse ... on the .tlb file of the project.

 

Vba Code

Then you can integrate the COM Control with Intellisense as follows.

 

Vba code example

Option Explicit On

 

Public Sub Test()

    Dim pdf_Reader As New Pdf_Text_Reader.pdf_Reader

 

    Dim s As String

    s = pdf_Reader.get_Hello

 

    MsgBox s

End Sub

 

 

 

Execution in Excel

Then the execution under Excel:

The dialog appears from the return string of the COM DLL

 

 

COM and ActiveX

This description is for a COM application as .dll, which under Microsoft Office serves for calling and executing Windows programs.

This COM is without visible user interface.

Building an ActiveX Usercontol application is pretty similar.

Mobile
»
Create C# COM Control for Office
»
Office Studio: Create your own COM controls for MS Office applications
»
Error: The assembly could not be changed to a type library. Type library exporter encountered an error while processing

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de