Excel VBA套接字连接 [英] Excel VBA socket connection
问题描述
我已经使用Google尝试找到解决问题的解决方案,但无法找到解决方案
I have used Google to try find a solution to my problem but have not been able to find a solution
我有一个 VB。 NET
应用程序使用此代码成功发送一个字符串到指定的IP地址和端口。
I have a VB.NET
application that using this code successfully sends a string to the specified IP address and port.
在这个远程计算机上,我可以接收这个字符串使用 VB.NET
来做我想要的,但是我需要使用Excel。
On this remote computer I can receive this string and do what I want with it using VB.NET
, but I need to use Excel.
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
其中 IPToBroadcstTo
是本地网络上远程计算机的IP地址。
where the IPToBroadcstTo
is the IPaddress of a remote computer on the local network.
我想让接收字符串的计算机在Excel中使用字符串并将其写入单元格,然后执行计算。
I would like the computer receiving the string to, in Excel, take the string and write it to a cell to then perform calculations with it.
推荐答案
您发布的代码是VB.NET。 Excel使用VBA,尽管它共享一些常见的语法是一个完全不同的语言与一组减少的库。
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.
例如,没有 Socket
或 IPEndPoint
类在VBA中。
For example there is no Socket
or IPEndPoint
class in VBA.
所以简而言之,答案是您无法使用该代码在Excel中打开套接字。
So in short the answer is that you can't use that code to open a socket in Excel.
查看 Winsock 控件或
也许您可以编写.NET程序集并通过 COM interop 以允许在Excel中使用。
Maybe you could write a .NET assembly and expose it through COM interop to allow this to be used in Excel.
这篇关于Excel VBA套接字连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!