是否可以在 VBA 中托管网络服务器?
我想托管一个 Web 服务器并想使用 VBA 来完成它。这可能吗?我这样做只是为了证明某人是错的,并且真的很想制作这个程序。
那么是否有可能制作一个非常简单的 Web 服务器(仅监听 get 请求)?非常感谢您的帮助。
编辑
我正在尝试这样的事情
Sub startServer()
Set wunsock = CreateObject("OSWINSCK.Winsock")
wunsock.LocalPort = 80
wunsock.Listen
End Sub
Sub wunsock_ConnectionRequest(ByVal requestID As Long)
If sockMain.State <> sckClosed Then
sockMain.Close
End If
sockMain.Accept requestID
End Sub
Private Sub wunsock_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
sockMain.GetData strData, vbString
txtStatus.Text = txtStatus.Text & _
strData & vbCrLf
End Sub
但是它不起作用?
I want to host a web server and want to use VBA to do it. Is this possible? I'm just doing this to prove someone wrong and really want to make this program.
So is it possible to make a really simple web server (just listens for get requests)? Help would be very much appreciated.
EDIT
I'm trying something like this
Sub startServer()
Set wunsock = CreateObject("OSWINSCK.Winsock")
wunsock.LocalPort = 80
wunsock.Listen
End Sub
Sub wunsock_ConnectionRequest(ByVal requestID As Long)
If sockMain.State <> sckClosed Then
sockMain.Close
End If
sockMain.Accept requestID
End Sub
Private Sub wunsock_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
sockMain.GetData strData, vbString
txtStatus.Text = txtStatus.Text & _
strData & vbCrLf
End Sub
However it doesn't work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
虽然这是一个相当老的问题,但我仍然想提一下,我使用普通的 VBA 宏和一些 Winsock C 调用构建了一个 Excel 托管的 REST Web 服务器,如 Daniel A. White 所提议的。
我将其添加为答案而不是评论,因为它是作为模块化库构建的,因此您可以根据您的需求进行调整,而其他人可能正是需要这种库。它可以提供工作表、基本文件,还可以使用 IWebController 创建自定义挂钩来侦听特定路由(OP 在评论中提到了这一点)
: com/michaelneu/webxcel" rel="noreferrer">http://github.com/michaelneu/webxcel
要使用它,您必须将类/模块导入到您的工作簿中,或者让构建脚本创建给你一件新的。请参阅 Main.bas 了解如何从以下位置启动服务器在 VBA 中。
Although this is a rather old question, I'd still like to mention that I built an Excel hosted REST webserver using plain VBA macros and some Winsock C calls, as proposed by Daniel A. White.
I added this as an answer instead of a comment, since it's built as a modular library, so you can adjust it to your needs, and others might need exactly this kind of library. It can serve both worksheets, basic files and also create custom hooks using an
IWebController
to listen on specific routes (which was mentioned by OP in a comment):http://github.com/michaelneu/webxcel
To use it, you'll have to either import the classes/modules into your workbook, or let the build script create a new one for you. See Main.bas on how to start the server from within VBA.
http://www.ostrosoft.com/oswinsck.asp#inst
是一个winsock类型可以从 VBA 使用的库。做你想做的事情是可能的,尽管这不是最有效的事情。
我为你的坚韧鼓掌,希望它对你有用。
http://www.ostrosoft.com/oswinsck.asp#inst
is a winsock type of library which can be used from VBA. It is possible to do what you are looking to do though is not the most efficient thing to do.
I do applaud your tenacity hope it works out for you.
我不确定我是否完全理解这个问题。一般来说,您不是“托管网络服务器”,而是托管网站。
但是,如果您可以使用 VBA 进行 TCP 套接字,那么您可以按照 HTTP 标准协议创建一个极其简单的 Web 服务器。
编辑:根据您的评论,是的,只要您可以打开 TCP 套接字,您就可以制作一个简单的 Web 服务器。
I'm not sure I fully understand the question. Generally, you don't "host a web server", you host a web site.
But if you can do TCP sockets with VBA, then you can make an incredibly simple web server by following the HTTP standard protocol.
Edit: based on your comment, yes you can make a simple web server as long as you can open up a TCP socket.
好吧,冒着违反问题精神的风险,您始终可以使用 VB 对库函数的支持,并且只需创建一个绑定到多个 C 语言 Web 服务器选项之一的库(例如 http://www.acme.com/software/micro_httpd/, http://www.gnu.org/software/libmicrohttpd/ 或 http://code.google.com/p/mongoose/)。您必须从选定的 Web 服务器中创建 DLL,但这相当容易完成,并且在 VBA 中也能正常工作。
Well, at the risk of violating the spirit of the question, you can always use VB's support for library functions and just create a library binding to one of a number of C-language web server options (such as http://www.acme.com/software/micro_httpd/, http://www.gnu.org/software/libmicrohttpd/ or http://code.google.com/p/mongoose/). You'd have to make DLLs out of the selected web server but that is reasonably easily done and this will work just fine in VBA.