0

can anyone guide me in the right direction when it comes to http requests with python? What I'm after is a Excel-VBA add-in which will track which workbooks the user opens and when etc. While that's already done, I would now like to get the information to a database.

For that purpose I can imagine running a very simple Python server which would be used to store the information. The question thus is, how do I set up a simple http server so that VBA can post a simple string which then gets stored?

Thanks!

EDIT:

Thanks chf! I went ahead and followed your advice - I replaced flask with django though as I had some brief experience with that. I've got my first API ever created now but can't post using the VBA code you posted. I can do httpie like so: "http POST http:/127.0.0.1 name="somename" workbookname="someworkbook".

Sub TestFramework()

Dim newClient As New WebClient
Dim newRequest As New WebRequest
Dim Response As WebResponse

newClient.BaseUrl = "http://127.0.0.1:8000/api/create/"

newRequest.Method = HttpPost

newRequest.Format = WebFormat.plaintext
newRequest.AddBodyParameter "name", "somename"
newRequest.AddBodyParameter "workbook_name", "Sheet1"

Set Response = newClient.Execute(newRequest)

End Sub

Any chance you might point me to the right way?

RuntimeError: You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to 127.0.0.1:8000/api/create/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings. [03/Aug/2016 20:13:18] "POST /api/create HTTP/1.1" 500 60534

Edit2: nevermind, got it working :)

1 Answer 1

1

Nice project! You can use Flask for the Python part to build a small REST Api and for the VBA part you can use VBA-WEB to consume that API.

Both Flask than VBA-WEB are very well documented with a lot of examples. I use VBA-WEB in a lot of small "SAK" (swiss army knife) utilities in Excel and it's very useful and powerful. For some rest apis with json output Flask is a nice tool to use.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.