Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending HTTP requests with VBA from Word

Tags:

I am trying to send data from a Word document to a web page. I have found some code, pasted it into a new module, and saved it. When I run it I get "compile error, user defined type not defined"

My code:

Sub http()    Dim MyRequest As New WinHttpRequest      MyRequest.Open "GET", _     "http://www.google.com"      ' Send Request.     MyRequest.Send      'And we get this response     MsgBox MyRequest.ResponseText  End Sub 
like image 973
Saul Avatar asked Jun 25 '10 15:06

Saul


People also ask

What is WinHttpRequest?

The WinHttpRequest object uses the IErrorInfo interface to provide error data. A description and numerical error value can be obtained with the Err object in Microsoft Visual Basic Scripting Edition (VBScript), and the Error object in Microsoft JScript.


2 Answers

A potential alternative to avoid having to select the library is to use an object i.e.

Sub http() Dim MyRequest As Object      Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")     MyRequest.Open "GET", _     "http://www.google.com"      ' Send Request.     MyRequest.Send      'And we get this response     MsgBox MyRequest.ResponseText  End Sub 
like image 180
Crispy Avatar answered Nov 01 '22 21:11

Crispy


You need to set a reference to Microsoft WinHTTP Services in your VBA Project (Tools -> References).

Here's what it would look like:

Also, you can read more about the Microsoft WinHTTP Services, version 5.1 here.

like image 25
Todd Main Avatar answered Nov 01 '22 21:11

Todd Main