Excel 使用VBA Web将单元格值作为正文发送

Excel 使用VBA Web将单元格值作为正文发送,excel,vba,rest,Excel,Vba,Rest,我正在研究VBA Web,这是一个使用excel进行Web请求的有用脚本。这是一些邮政编码: Dim Client As New WebClient Client.BaseUrl = "https://api.testing.com/" Dim Request As New WebRequest Request.Resource = "pages" Request.Method = WebMethod.HttpPost ' Formatting ' ---------- ' ' Simple

我正在研究VBA Web,这是一个使用excel进行Web请求的有用脚本。这是一些邮政编码:

Dim Client As New WebClient
Client.BaseUrl = "https://api.testing.com/"

Dim Request As New WebRequest
Request.Resource = "pages"
Request.Method = WebMethod.HttpPost

' Formatting
' ---------- '
' Simple: Request.Format sets four things:
'         1. Content-Type header
'         2. Accept header
'         3. Request Body conversion
'         4. Response Data conversion
Request.Format = WebFormat.Json

' Medium: Set separate request and response format
Request.RequestFormat = WebFormat.Json ' Set Content-Type and request converter
Request.ResponseFormat = WebFormat.Json ' Set Accept and response converter

' Advanced: Set separate everything
Request.RequestFormat = WebFormat.Json
Request.ContentType = "application/json"
Request.ResponseFormat = WebFormat.Json
Request.Accept = "application/json"

' Body
' ---- '
' a. Add body as string
Request.Body = "{""a"":123,""b"":[456, 789]}"

' b. Add parameters (converted with RequestFormat)
Request.AddBodyParameter "a", 123
Request.AddBodyParameter "b", Array(456, 789)
' -> Request.Body (for json): "{""a"":123,""b"":[456, 789]}"

' c. Add body as dictionary (or Collection or Array)
Dim Body As New Dictionary
Body.Add "a", 123
Body.Add "b", Array(456, 789)
Set Request.Body = Body
' -> Request.Body (for json): "{""a"":123,""b"":[456, 789]}"

' Add other things common to all Requests
Request.AddCookie "cookie", "abc"
Request.AddHeader "header", "def"
Request.AddQuerystringParam "querystring", "ghi"
' etc.

' To debug the request:
' 1. From VBA, Open Immediate Window (View > Immediate Window or Ctrl + g)
' 2. Type: RestHelpers.EnableLogging = True
' 3. Run request

' Make the request
Dim Response As WebResponse
Set Response = Client.Execute(Request)

' POST https://api.testing.com/pages?querystring=ghi
' header: def
' User-Agent: Excel Client v3.1.4 (https://github.com/timhall/Excel-REST)
' Content-Type: application/json
' Accept: application/json
' Content-Length: 23
' Cookie: cookie=abc
'
' {"a":123,"b":[456,789]}

' Handle the response
If Response.StatusCode = WebStatusCode.Created Then
    ' ...
End If
但是,我在文档中看不到如何从单元格(例如单元格A1和B1)中获取值并将其作为JSON发送


这似乎是可能的,但它似乎只是硬编码的身体价值观

一个例子如下:

Request.Body = ThisWorkbook.Worksheets("Sheet1").Range("A1").value

它不在VBA-WEB的文档中,因为从单元格读取值在VBA for Excel的文档中;-)