Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket connection using Excel VBA

I have a VB.NET application that successfully sends a string to the specified IP address and port.

Public Sub BroadcastData(ByVal toSend As String, ByVal PortToSendTo As Long)
    Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
    Dim sendBUF As Byte() = Encoding.ASCII.GetBytes(toSend)
    Dim ep As New IPEndPoint(MainForm.IPToBroadcastTo, PortToSendTo)
    s.SendTo(sendBUF, ep)
    System.Diagnostics.Debug.WriteLine(s.SendTo(sendBUF, ep))
End Sub

The IPToBroadcstTo is the IPaddress of a remote computer on the local network.

On this remote computer I can receive this string and do what I want with it using VB.NET.

I would to receive the string in Excel and write it to a cell.

like image 562
user3320758 Avatar asked May 14 '26 06:05

user3320758


1 Answers

The code you posted is VB.NET. Excel uses VBA which although it shares some common syntax is a completely different language with a reduced set of libraries.

For example there is no Socket or IPEndPoint class in VBA.

So in short the answer is that you can't use that code to open a socket in Excel.

Have a look at the Winsock control or

Maybe you could write a .NET assembly and expose it through COM interop to allow this to be used in Excel.

like image 118
Matt Wilko Avatar answered May 15 '26 22:05

Matt Wilko



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!