How do I use WinHttp within Excel 2003 Macro?

  • Thread starter Thread starter Mak
  • Start date Start date
M

Mak

Hi all, I want to get web content into Excel and store it to variables (not
in the Cells), how can I do it? I know WinHttp can help but I do not know how
to 'include' WinHttp in Excel. Please help.
 
goto
(e-mail address removed)
and look in the files section. There are several free files.
I have a couple of free files under author donalb36
 
Within the VBA project, you need to use menu option > Tools >
References, then add the XMLHTTP reference library (Microsoft XML, x.
0).
 
Randy, but after activating XMLHTTP lib, it says the "WinHttp.WinHttpRequest"
has not been defined.
I think I have to activate WinHttp.dll, but the default path is
c:\Users\USER\Documents\%SystemRoot%\system32\
and I should copy winhttp.dll to the path manually (winhttp.dll is currently
in Windows\system32\), but I don't know what %SystemRoot% stands for. Could
you pls help?
 
Could you pls guide me how to access (e-mail address removed) step
by step? Thanks in advance.
 
I have downloaded some files, for those VBA that I can view, they are using
QueryTable with Destination = a cell or range. But I want to store the web
content in variables first. Any hints?
 
What you want is easy to do. Post your code and/or send your workbook to my
address below.
 
Hi all,
Pls find the code:


Const INTERNET_OPEN_TYPE_DIRECT = 1
Const INTERNET_OPEN_TYPE_PROXY = 3
Const INTERNET_FLAG_RELOAD = &H80000000
Private Declare Function InternetOpen Lib "wininet" Alias "InternetOpenA"
(ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As
String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As
Long) As Integer
Private Declare Function InternetReadFile Lib "wininet" (ByVal hFile As
Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long,
lNumberOfBytesRead As Long) As Integer
Private Declare Function InternetOpenUrl Lib "wininet" Alias
"InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal lpszUrl As String,
ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlags As
Long, ByVal dwContext As Long) As Long

Private Declare Function InternetAttemptConnect Lib "wininet" (ByVal
dwReserved As Long) As Long

Sub Fetch()
Dim tTxt As String

tTxt = DownloadPage(Range("URL"))

End Sub


Function DownloadPage(sURL As String, Optional Length As Long) As String

Dim hOpen As Long, hFile As Long, sBuffer As String, Ret As Long

If Length = 0 Then Length = 10000

sBuffer = Space(Length)
hOpen = InternetOpen("", INTERNET_OPEN_TYPE_DIRECT, vbNullString,
vbNullString, 0)
hFile = InternetOpenUrl(hOpen, sURL, "", 0, INTERNET_FLAG_RELOAD, ByVal
0&)
InternetReadFile hFile, sBuffer, Length, Ret
InternetCloseHandle hFile
InternetCloseHandle hOpen
DownloadPage = Left$(sBuffer, Ret)

End Function
 
Hi all,
Pls find the code:

Const INTERNET_OPEN_TYPE_DIRECT = 1
Const INTERNET_OPEN_TYPE_PROXY = 3
Const INTERNET_FLAG_RELOAD = &H80000000
Private Declare Function InternetOpen Lib "wininet" Alias "InternetOpenA"
(ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As
String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As
Long) As Integer
Private Declare Function InternetReadFile Lib "wininet" (ByVal hFile As
Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long,
lNumberOfBytesRead As Long) As Integer
Private Declare Function InternetOpenUrl Lib "wininet" Alias
"InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal lpszUrl As String,
ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlagsAs
Long, ByVal dwContext As Long) As Long

Private Declare Function InternetAttemptConnect Lib "wininet" (ByVal
dwReserved As Long) As Long

Sub Fetch()
Dim tTxt As String

tTxt = DownloadPage(Range("URL"))

End Sub

Function DownloadPage(sURL As String, Optional Length As Long) As String

    Dim hOpen As Long, hFile As Long, sBuffer As String, Ret As Long

    If Length = 0 Then Length = 10000

    sBuffer = Space(Length)
    hOpen = InternetOpen("", INTERNET_OPEN_TYPE_DIRECT, vbNullString,
vbNullString, 0)
    hFile = InternetOpenUrl(hOpen, sURL, "", 0, INTERNET_FLAG_RELOAD, ByVal
0&)
    InternetReadFile hFile, sBuffer, Length, Ret
    InternetCloseHandle hFile
    InternetCloseHandle hOpen
    DownloadPage = Left$(sBuffer, Ret)

End Function






- Show quoted text -

HI,
I have tried above code and function but it fatches nothing.
No errors but debug.pring is nothing.

Any idea?
It might be quite usefull to me.

Regards,
Madiya
 
Back
Top