Articles → VBA FOR EXCEL → Call C# code from VBA

Call C# code from VBA






Create a C# library




using System;
using System.IO;

namespace SampleComVisibleLibrary {
	public class SampleComVisibleClass {
		public void WriteTimeStampInLogFile() {
			string str = System.Guid.NewGuid().ToString();
			string path = string.Format(@"c:\temp\Log_{0}", str);

			File.WriteAllText(path, DateTime.Now.ToLongDateString());
		}
	}
}





Make C# class library com visible







Click to Enlarge




Click to Enlarge




Click to Enlarge




Click to Enlarge




Register the DLL using RegAsm








C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe C:\temp\ComVisibleLibrary\SampleComVisibleLibrary\SampleComVisibleLibrary\bin\Debug\SampleComVisibleLibrary.dll /codebase







Click to Enlarge


Add reference of C# library in VBA







Click to Enlarge




Click to Enlarge




Use C# library




Sub SampleComVisibleAssembly()
	
	Dim obj As New SampleComVisibleClass
	obj.WriteTimeStampInLogFile
End Sub



Output





Click to Enlarge

Posted By  -  Karan Gupta
 
Posted On  -  Monday, October 22, 2018

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250