使用 Excel VBA 的套接字连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21839142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Socket connection using Excel VBA
提问by user3320758
I have a VB.NET application that successfully sends a string to the specified IP address and port.
我有一个 VB.NET 应用程序,它成功地将字符串发送到指定的 IP 地址和端口。
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.
的IPToBroadcstTo
是在本地网络上的远程计算机的名称IP地址。
On this remote computer I can receive this string and do what I want with it using VB.NET.
在这台远程计算机上,我可以接收这个字符串并使用 VB.NET 执行我想要的操作。
I would to receive the string in Excel and write it to a cell.
我想在 Excel 中接收字符串并将其写入单元格。
回答by Matt Wilko
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.
您发布的代码是 VB.NET。Excel 使用 VBA,虽然它共享一些通用语法,但它是一种完全不同的语言,具有较少的库集。
For example there is no Socket
or IPEndPoint
class in VBA.
例如,VBA 中没有Socket
orIPEndPoint
类。
So in short the answer is that you can't use that code to open a socket in Excel.
简而言之,答案是您不能使用该代码在 Excel 中打开套接字。
Have a look at the Winsockcontrol or
看看Winsock控件或
Maybe you could write a .NET assembly and expose it through COM interopto allow this to be used in Excel.
也许您可以编写一个 .NET 程序集并通过COM 互操作公开它以允许它在 Excel 中使用。