You are currently offline, waiting for your internet to reconnect

HOW TO: Call a Visual Basic .NET Class Library from Visual Basic for Applications in Microsoft Office

Support for Office 2003 has ended

Microsoft ended support for Office 2003 on April 8, 2014. This change has affected your software updates and security options. Learn what this means for you and how to stay protected.

This article was previously published under Q317535
SUMMARY
Use this step-by-step guide to call a .NET class library from Visual Basic for Applications. This sample shows how a Visual Basic for Applications program can use a Visual Basic .NET class library to encrypt and decrypt a string. You can use the cryptography namespace, included in the .NET Framework, for the encryption/decryption.

back to the top

Create the Visual Basic .NET Class Library

  1. Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. Under Visual Basic Projects, select Class Library. Name the class CryptoClass and click OK. Class1 is created by default.
  2. Replace the contents of Class1 with the following code:

    Imports System.Security.Cryptography<ComClass(Class1.ClassId, Class1.InterfaceId, Class1.EventsId)> Public Class Class1    Public Const ClassId As String = "98349785-8BE2-4604-848D-F5B103D61715"    Public Const InterfaceId As String = "36613EE9-125F-493d-9968-771E18C2226A"    Public Const EventsId As String = "A036F02F-F87E-4548-A536-7DD7EA8E62B5"    Const sKey As String = "MyKey"    Public Function EncryptTripleDES(ByVal sIn As String) As String        Dim DES As New TripleDESCryptoServiceProvider()        Dim hashMD5 As New MD5CryptoServiceProvider()        ' Compute the MD5 hash.        DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(sKey))        ' Set the cipher mode.        DES.Mode = CipherMode.ECB        ' Create the encryptor.        Dim DESEncrypt As ICryptoTransform = DES.CreateEncryptor()        ' Get a byte array of the string.        Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(sIn)        ' Transform and return the string.        Return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length))    End Function    Public Function DecryptTripleDES(ByVal sOut As String) As String        Dim DES As New TripleDESCryptoServiceProvider()        Dim hashMD5 As New MD5CryptoServiceProvider()        ' Compute the MD5 hash.        DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(sKey))        ' Set the cipher mode.        DES.Mode = CipherMode.ECB        ' Create the decryptor.        Dim DESDecrypt As ICryptoTransform = DES.CreateDecryptor()        Dim Buffer As Byte() = Convert.FromBase64String(sOut)        ' Transform and return the string.        Return System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length))    End FunctionEnd Class						

    Note You can use the GUID generator to generate new GUIDs for the ClassId, InterfaceId, and EventsId. To generate new GUIDs, click Create GUID on the Tools menu.
  3. From Project Properties, select Configuration Properties. Click Build and then select the Register for COM Interop check box. Click OK.
  4. On the Build menu, click Build Solution to create the DLL.
back to the top

Create a Visual Basic for Applications Macro to Call The .NET Class Library

  1. Start Microsoft Excel. Press ALT+F11 to start the Visual Basic Editor.
  2. On the Insert menu, click Module to insert a blank module.
  3. On the Tools menu, click References. Add a reference to the CryptoClass library and click OK.
  4. Type or paste the following code in the Module1 code window:
    Sub TestCrypto()  Dim oCrypto As New CryptoClass.Class1  Dim sCrypt As String    sCrypt = oCrypto.EncryptTripleDES("This is a test")  MsgBox "Encrypted text = " & sCrypt  MsgBox "Decrypted text = " & oCrypto.DecryptTripleDES(sCrypt)End Sub					
back to the top

Test the Code

  1. On the Tools menu, click Macros. In the list of macros, click TestCrypto, and then click Run.
  2. If a message box appears showing the encrypted string, click OK. A second message box appears showing the decrypted string.
  3. If the class library needs to be registered on another computer running .NET Framework, copy the DLL to the system and run the following command:
    regasm CryptoClass.dll /tlb:CryptoClass.tlb
back to the top
REFERENCES
For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
301070 HOW TO: Encrypt and Decrypt a File by Using Visual Basic .NET
For more information, see the following Microsoft Developer Network (MSDN) Web site: back to the top
crypto vba
Properties

Article ID: 317535 - Last Review: 03/29/2007 02:29:32 - Revision: 8.5

  • Microsoft Office Access 2003
  • Microsoft Access 2002 Standard Edition
  • Microsoft Office Excel 2003
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Office PowerPoint 2003
  • Microsoft PowerPoint 2002 Standard Edition
  • Microsoft Office Word 2003
  • Microsoft Visual Studio .NET 2003 Professional Edition
  • Microsoft Word 2002
  • Microsoft Visual Studio .NET 2002 Professional Edition
  • kbcrypt kbsecurity kbhowtomaster KB317535
Feedback
data-bi-slot="2"> Privacy & cookies
  • Trademarks
  • © 2015 Microsoft