Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to return a string from a .net program using vba

Tags:

vb.net

vba

I have this vba code

Dim strpath As String
Dim this As String
strpath = "C:\Users\johbra\Documents\Visual Studio 2010\Projects\WindowsApplication6\WindowsApplication6\bin\Debug\WindowsApplication6.exe"
this = Shell(strpath)

MsgBox this

I also have this function in a .net program

Public Class Form1

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    dothis()
    Application.Exit()

End Sub

Function dothis()
    Return "HI"
End Function
End Class

Im trying to get the vba code to give me a messagebox that says "HI".. Is this possible?

like image 284
user867621 Avatar asked Aug 13 '26 14:08

user867621


1 Answers

The most accepted and reliable way to call .NET code from VBA is to expose the .NET class as a COM class (no extra code needed, just tick the appropriate box in project properties), then add a reference the COM class in your VBA project, the way you would for any other other COM class, and call the function.

Here's a guide on how exactly this is done.

Please let me know if you need more details on how to do this.

like image 109
AFischbein Avatar answered Aug 16 '26 20:08

AFischbein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!