How can I send an HTTP POST request to a server from Excel using VBA?

前端 未结 6 2016
执笔经年
执笔经年 2020-11-22 08:01

What VBA code is required to perform an HTTP POST from an Excel spreadsheet?

6条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 08:32

    If you need it to work on both Mac and Windows, you can use QueryTables:

    With ActiveSheet.QueryTables.Add(Connection:="URL;http://carbon.brighterplanet.com/flights.txt", Destination:=Range("A2"))
        .PostText = "origin_airport=MSN&destination_airport=ORD"
        .RefreshStyle = xlOverwriteCells
        .SaveData = True
        .Refresh
    End With
    

    Notes:

    • Regarding output... I don't know if it's possible to return the results to the same cell that called the VBA function. In the example above, the result is written into A2.
    • Regarding input... If you want the results to refresh when you change certain cells, make sure those cells are the argument to your VBA function.
    • This won't work on Excel for Mac 2008, which doesn't have VBA. Excel for Mac 2011 got VBA back.

    For more details, you can see my full summary about "using web services from Excel."

提交回复
热议问题