Microsoft KB Archive/317535

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

Article ID: 317535

Article Last Modified on 3/29/2007

-

APPLIES TO


 * 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 Standard Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition

-



This article was previously published under Q317535



IN THIS TASK
SUMMARY
 * Create the Visual Basic .NET Class Library
 * Create a Visual Basic for Applications Macro to Call the .NET Class Library
 * Test the Code

REFERENCES



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
 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.  Replace the contents of Class1 with the following code:

Imports System.Security.Cryptography

 Public Class Class1 Public Const ClassId As String = &quot;98349785-8BE2-4604-848D-F5B103D61715&quot; Public Const InterfaceId As String = &quot;36613EE9-125F-493d-9968-771E18C2226A&quot; Public Const EventsId As String = &quot;A036F02F-F87E-4548-A536-7DD7EA8E62B5&quot;

Const sKey As String = &quot;MyKey&quot;

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 Function End 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.  From Project Properties, select Configuration Properties. Click Build and then select the Register for COM Interop check box. Click OK. 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
 Start Microsoft Excel. Press ALT+F11 to start the Visual Basic Editor.</li> On the Insert menu, click Module to insert a blank module.</li> On the Tools menu, click References. Add a reference to the CryptoClass library and click OK.</li>  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(&quot;This is a test&quot;) MsgBox &quot;Encrypted text = &quot; & sCrypt MsgBox &quot;Decrypted text = &quot; & oCrypto.DecryptTripleDES(sCrypt) End Sub </li></ol>

back to the top

Test the Code
 On the Tools menu, click Macros. In the list of macros, click TestCrypto, and then click Run.</li> If a message box appears showing the encrypted string, click OK. A second message box appears showing the decrypted string.</li> 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

</li></ol>

back to the top

<div class="references_section">