22

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
GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
Saul
  • 1,387
  • 5
  • 23
  • 45

3 Answers3

31

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
Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
Crispy
  • 326
  • 2
  • 2
22

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.

Community
  • 1
  • 1
Todd Main
  • 28,951
  • 11
  • 82
  • 146
  • When in my visual basic edit screen the tools>references menu command is greyed out. – Saul Jul 06 '10 at 12:49
  • 2
    @Saul: This usually means that code or the debugger is still running. Try clicking Run->Reset and then after that Tools>References. – Todd Main Jul 06 '10 at 14:37
2

You will need to change your references (Tools=>References in the code window). Look for Microsoft WinHTTP Services, version 5.1 (or newer) and tick the box. If you are using Vista and office 2007, you may also need to register it first. Open a command window as administrartor and paste:

>regsvr32.exe "c:\windows\system32\winhttp.dll"

It should say if it works.

Fionnuala
  • 90,370
  • 7
  • 114
  • 152