POST data from excel on a website and receive data into excel, too

mircea80
06-11-2008, 04:10 PM
Hi there, 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("http://www.mfinante.ro/contribuabili/link.jsp?body=/contribuabili/agenti_cod.jsp", _
"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-www-form-urlencoded" + 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 + "&#37;" + 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
'the website' address after POST
Application.Workbooks.Open ("http://www.mfinante.ro/contribuabili/link.jsp?body=/contribuabili/agenti_cod.jsp")
'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

Cas
06-11-2008, 05:08 PM
Hi mircea, welcome to the forum! :)

Could you edit your post and put tags around the code so it becomes easier to read?
Thanks.

Cas
06-11-2008, 05:50 PM
Okay, now, can you tell us which part is working and which isn't - or if both are working separately but not together? Sorry about the interrogation, but I'm not quite clear on how to help you yet... :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum