mircea80
Instructor
- Jun 11, 2008
- 2
I have a challenge! 
How can I combine the two next scripts so I can POST data to a website (from my Excel) and the data from the called website to be retrieved into a new worksheet?
[POST SCRIPT]
'**************************************** Post form data - begin
'Data from Excel
Sub test()
Dim CUI as Range
'CUI is a Unique Identification Code for Employers
Set CUI=[A1]
Call PostRequest(" _
"cod", _
CUI)
End Sub
'sends form fields specified In Names/Values arrays To the URL
Sub PostRequest(URL, Names, Values)
Dim I, FormData, Name, Value
'Enumerate form names And it's values
'and built string representaion of the form data
Name = URLEncode(Names)
Value = URLEncode(Values)
If FormData <> "" Then FormData = FormData & "&"
FormData = FormData & Name & "=" & Value
'Post the data To the destination URL
IEPostStringRequest _
URL, _
FormData
End Sub
'sends URL encoded form data To the URL using IE
Sub IEPostStringRequest(URL, FormData)
'Create InternetExplorer
Dim WebBrowser: Set WebBrowser = CreateObject("InternetExplorer.Application")
'You can uncoment Next line To see form results As HTML
WebBrowser.Visible = True
'Send the form data To URL As POST request
Dim bFormData() As Byte
ReDim bFormData(Len(FormData) - 1)
bFormData = StrConv(FormData, vbFromUnicode)
WebBrowser.Navigate URL, "_Self", , bFormData, _
"Content-Type: application/x- + Chr(10) + Chr(13)
Do While WebBrowser.busy
' Sleep 100
DoEvents
Loop
'WebBrowser.Quit
End Sub
'URL encode of a string data
Function URLEncode(Data)
Dim I, c, Out
For I = 1 To Len(Data)
c = Asc(Mid(Data, I, 1))
If c = 32 Then
Out = Out + "+"
ElseIf c < 48 Then
Out = Out + "%" + Hex(c)
Else
Out = Out + Mid(Data, I, 1)
End If
Next
URLEncode = Out
End Function
'**************************************** Post form data - end
[RETRIEVING DATA INTO A WORKSHEET]
Option Compare Text
Option Explicit
Sub DownloadItemFromHTMLpage()
'an example given below - insert the URL you want
Application.Workbooks.Open ("'you will need to know the range to select for the required
'download, or, you can add in a 'Find' function here to search
'for it - the example given below is for a known range...
Range("A40").Select
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
ActiveWindow.Close
End Sub
How can I combine the two next scripts so I can POST data to a website (from my Excel) and the data from the called website to be retrieved into a new worksheet?
[POST SCRIPT]
'**************************************** Post form data - begin
'Data from Excel
Sub test()
Dim CUI as Range
'CUI is a Unique Identification Code for Employers
Set CUI=[A1]
Call PostRequest(" _
"cod", _
CUI)
End Sub
'sends form fields specified In Names/Values arrays To the URL
Sub PostRequest(URL, Names, Values)
Dim I, FormData, Name, Value
'Enumerate form names And it's values
'and built string representaion of the form data
Name = URLEncode(Names)
Value = URLEncode(Values)
If FormData <> "" Then FormData = FormData & "&"
FormData = FormData & Name & "=" & Value
'Post the data To the destination URL
IEPostStringRequest _
URL, _
FormData
End Sub
'sends URL encoded form data To the URL using IE
Sub IEPostStringRequest(URL, FormData)
'Create InternetExplorer
Dim WebBrowser: Set WebBrowser = CreateObject("InternetExplorer.Application")
'You can uncoment Next line To see form results As HTML
WebBrowser.Visible = True
'Send the form data To URL As POST request
Dim bFormData() As Byte
ReDim bFormData(Len(FormData) - 1)
bFormData = StrConv(FormData, vbFromUnicode)
WebBrowser.Navigate URL, "_Self", , bFormData, _
"Content-Type: application/x- + Chr(10) + Chr(13)
Do While WebBrowser.busy
' Sleep 100
DoEvents
Loop
'WebBrowser.Quit
End Sub
'URL encode of a string data
Function URLEncode(Data)
Dim I, c, Out
For I = 1 To Len(Data)
c = Asc(Mid(Data, I, 1))
If c = 32 Then
Out = Out + "+"
ElseIf c < 48 Then
Out = Out + "%" + Hex(c)
Else
Out = Out + Mid(Data, I, 1)
End If
Next
URLEncode = Out
End Function
'**************************************** Post form data - end
[RETRIEVING DATA INTO A WORKSHEET]
Option Compare Text
Option Explicit
Sub DownloadItemFromHTMLpage()
'an example given below - insert the URL you want
Application.Workbooks.Open ("'you will need to know the range to select for the required
'download, or, you can add in a 'Find' function here to search
'for it - the example given below is for a known range...
Range("A40").Select
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
ActiveWindow.Close
End Sub