14

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?

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
David
  • 141
  • 1
  • 3
  • 12
    "...to prove someone wrong..." --> +1 – Chris Laplante Jun 23 '11 at 01:02
  • http://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/a9262dc9-5e97-4b61-9fa5-014976c47527/ ? – Merlyn Morgan-Graham Jun 23 '11 at 01:10
  • 2
    A web server embedded in Excel. The mind boggles. – anon Jun 23 '11 at 01:10
  • IMHO, it would be worth the time to learn python for this sort of thing. It's far more lightweight, and can be done in a few lines of code. It'll also help you learn the basics of another language, which is always good. – Patrick Perini Jun 23 '11 at 01:12
  • Oh, I know other languages. I just want to create it in office :P – David Jun 23 '11 at 01:14
  • you might not have permission to open a socket on port 80 – Daniel A. White Jun 23 '11 at 11:51
  • ... Well? Did you figure this out? I want to use this for my e-commerce site. It's an all-in-one web server and database that you can carry on your thumb drive! Plus, I bet you can generate some cool real-time sales reporting. – anon Jun 26 '11 at 18:47
  • Don't forget you can add an internet explorer webcontrol to a form and load it with a temporary html file. A great way to provide tables of datam etc, without 3rd party addons. No reason why you can't generate your reports with that. – Phil Prett Apr 15 '16 at 12:46

4 Answers4

5

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.

michaeln
  • 1,032
  • 17
  • 33
  • I totally plan on trying this, and now I can run a security analysis on network traffic coming in to my computer. Utilizing the new data model / PowerPivot in Excel will be great with this. And I'll share it with my VBA students :) – Rick Henderson Oct 30 '18 at 00:48
  • How easy would it be to adapt this to websockets? – Michael Terry Mar 11 '19 at 18:12
  • It's on the roadmap, but I haven't played with it yet (see https://github.com/michaelneu/webxcel/issues/11). The main issue I see there is handling parallel requests, because if you keep a websocket open, you'll block the server from accepting further regular HTTP requests (VBA is synchronous and single threaded). I still haven't figured out, how I'd approach this, but I recently added things to the TCP handling to implement FastCGI for PHP (not merged yet, but it works already), so feel free to play with it. – michaeln Mar 13 '19 at 10:22
4

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.

Bueller
  • 2,336
  • 17
  • 11
  • 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? – David Jun 23 '11 at 03:20
  • 4
    @David: Please don't put tons of code in a comment. It's obviously illegible. Put it in your question as an edit, and format it as `code`. I'll do it for you this time. – Jean-François Corbett Jun 23 '11 at 06:43
1

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.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • I want to listen for requests on a port to a web page (etc. localhost:234/hi.html). It does not need to reply or anything. – David Jun 23 '11 at 01:05
1

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.

Femi
  • 64,273
  • 8
  • 118
  • 148