PDA

View Full Version : Javascript:_doPostback



murfyang
07-08-2008, 07:34 AM
Hi, I am trying to import data from a secure site into excel for data analysis. I have managed to login with this code at:

http://www.vbaexpress.com/forum/showthread.php?t=18968&highlight=login+site

from there, i need to go another 2 level down to reach the client's info. However, the link to extract the client data is: javascript:__doPostBack('dgCltsHoldsAndTrans$_ctl2$lnkbtnClientName','')"

I managed to get the code below, i believed from this site:

Function clientData()
'This project includes references to "Microsoft Internet Controls", shdocvw.dll and
'"Microsoft HTML Object Library", mshtml.tlb

'Variable declarations
Dim myIE As New InternetExplorer 'New '
Dim myURL As String
Dim myDoc As HTMLDocument
Dim s As String

'Set starting URL and search string
myURL = "javascript:__doPostBack('dgCltsHoldsAndTrans$_ctl2$lnkbtnClientName','')"

'Make IE navigate to the URL and make browser visible
myIE.navigate myURL
myIE.Visible = False

'Wait for the page to load

' the code breaks on the below line with the msg as shown below '
Do While myIE.Busy Or myIE.readyState <> READYSTATE_COMPLETE
''''''''''' Error Msg: Automation Error, Unspecified Error. '''''''''''

DoEvents
Loop

'Set IE document into object
Set myDoc = myIE.document


myIE.Quit
Set myIE = Nothing
Set myDoc = Nothing

End Function

have tried to search other forums, can't find anything on this javascript:_doPostback so if anybody who can help, much appreciate any suggestion on how i can go about solving this part. Learning still a great deal from all the greats in this forum.

murf from S'pore

Oorang
07-09-2008, 08:25 AM
Get rid of the the myIe.busy and just wait for ready state complete.

murfyang
07-10-2008, 08:38 AM
thanks Oorang, tried what you suggested.

1] remove myIE.busy
2] completely remove the Do While myIE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
3] without the do while, added Application.Wait (Now + TimeValue("0:00:05"))
4] completely removing all the above,

error now occurs at Set myDoc = myIE.document line with the same error msg.
Could it be the link is in another frame? can't go far with my limited knowledge of vba & java.
tried to attached the source code, but it's 56pages long & can't attached in txt format.
:stars:
thanks oorang for whatever help you can provide.

murf fr s'pore

RonMcK
07-10-2008, 09:37 AM
Can you 'zip' a text file of the source code and upload that as an attachment?

Thanks,

murfyang
07-11-2008, 12:32 AM
why didn't i think of that! thanks Ron, in the meantime, am researching:reading: on this dopostpost through some java sites, understand that there is some looping involved but just don't know enough to breakthrough it, ...at the moment.
attached is the txt file. The codes for the javascript:_dopostback(........) is located near the end of the code.
Any guide / help from anyone is always most appreciated.

murf fm S'pore

TomSchreiner
07-11-2008, 04:41 AM
You should be able to use something like...

myURl = "__doPostBack('dgCltsHoldsAndTrans$_ctl2$lnkbtnClientName','')"
myIE.navigate myURl

or

myIE.document.parentwindow.execscript "__doPostBack('dgCltsHoldsAndTrans$_ctl2$lnkbtnClientName','')"

or

myIE.document.all("dgCltsHoldsAndTrans__ctl2_lnkbtnClientName").Click

murfyang
07-11-2008, 08:19 AM
Hi everyone, no words can be enough to tell all how thankful i am for the help i am getting here. :ipray:

Tom, i have tried
myIE.document.parentwindow.execscript "__doPostBack('dgCltsHoldsAndTrans$_ctl2$lnkbtnClientName','')"

Or

myIE.document.all("dgCltsHoldsAndTrans__ctl2_lnkbtnClientName").Click

both same error & did not go beyond this line.

while for

myURL = "__doPostBack('dgCltsHoldsAndTrans$_ctl2$lnkbtnClientName','')"
myIE.navigate myURL
myIE.Visible = True ' Screen supposed to show up some info but it's blank
Application.Wait (Now + TimeValue("0:00:05"))
Set myDoc = myIE.document

Managed to execute up to
Set myDoc = myIE.document
then same error msg appears "Error Msg: Automation Error, Unspecified Error."

I wished that i can tell you guys that i have tried certain other things but i am just stucked with all these alien jargon:banghead:

:bow: :bow::bow: thanks thanks thanks once again!

murf fm s'pore

TomSchreiner
07-11-2008, 09:27 AM
Murf. Download this and click on the button to open a userform with a webbrowser control. Enter your first url into the textbox and click on navigate. Continue through your typical navigation steps. Each page that is loaded will be saved in some textboxes on sheet1. If there is proprietary text located within the html code, select the texbox, press CTRL-a to select all, CTRL-c to copy, and CTRL-v to paste into notepad. Edit out the confidential and paste it back into your reply wrapped in html tags. Otherwise, just copy and paste into your reply.

NavRecorder.zip (http://home.fuse.net/tstom/NavRecorder.zip)

murfyang
07-12-2008, 08:01 AM
hi Tom, thanks for your great help. I have recorded the codes from those tetxboxes. Believed i am following you correctly. Anyway i have done image shots of the screens from login stage to the screen where i am having the javascript_dopostback link so that you can see how the flow is. But it's in word file, hope that is ok with you. : pray2: Pray that this is the code that will solve my last piece of the puzzle.

murfyang
07-20-2008, 01:01 AM
Hi, not too sure if i should post this as a new thread coz this is together with what i am working on.

The site has changed their javascript_dopostback code & now i am able to go to the data page that i want but here i am unable to execute IE.EexcWB. Refering to the code below:

Function ExtractClientAccount()
Dim myIE As New InternetExplorer 'New '
Dim myDoc As HTMLDocument

EXaccount = Sheets("ataglance").Range("ad4").Value 'Account link

Sheets.Add After:=Sheets(Sheets.Count)
Range("A1").Select
ActiveSheet.Name = "Temp"
Range("a1").Select

Set myIE = CreateObject("InternetExplorer.Application")
With myIE
.Visible = True
.navigate EXaccount
Do Until .readyState = 4
DoEvents
Loop

End With
Do Until Not myIE.Busy
DoEvents
Loop

Application.Wait (Now + TimeValue("0:00:05"))
Set myDoc = myIE.Document
myIE.ExecWB 17, 0 '// SelectAll
myIE.ExecWB 12, 2 '// Copy selection

'the cursor seems to be 'lock' in the account cell as I notice that when i
set the paste special to text, it is able to paste only that account number & not the page data that i want

Sheets("Temp").PasteSpecial Format:="HTML", link:=False, DisplayAsIcon:= _False

Set myIE = Nothing
' IE.Quit
End Function

I have attached an image file of the page for easy understanding of the problem I am facing.

Thanks once again for the help.
murf from spore

TomSchreiner
07-20-2008, 06:16 AM
Sorry Murfy. I lost track of this thread. Since you have gotten to the page there is no reason to discuss the problem before which was a dynamically created IFrame. Now for this page, I am suspecting that we are still dealing with one or more frames. This is not a problem, it is just difficult to help without access to the pages. For now, try this first:

Application.Wait (Now + TimeValue("0:00:05"))
Set myDoc = myIE.Document
myIE.Document.Focus
myIE.ExecWB 17, 0 '// SelectAll
myIE.ExecWB 12, 2 '// Copy selection

If that fails, please edit your code below this line...
Set myDoc = myIE.Document
...by adding:
PutInClipboard myIE.Document

Set a reference to MSForms. If you have any userforms or activeX controls on any worksheets, you will already have a reference to this library.
Add this little procedure into the same module:
Private Sub PutInClipboard(Doc As Object)
Dim CB As New DataObject

CB.SetText Doc.body.outerHTML
CB.PutInClipboard
End Sub

This will place the relevant source onto the clipboard. Paste it into notepad and attach it to your reply.

Thanks... :)

Code should look like:
Function ExtractClientAccount()
Dim myIE As New InternetExplorer 'New '
Dim myDoc As HTMLDocument

EXaccount = Sheets("ataglance").Range("ad4").Value 'Account link

Sheets.Add After:=Sheets(Sheets.Count)
Range("A1").Select
ActiveSheet.Name = "Temp"
Range("a1").Select

Set myIE = CreateObject("InternetExplorer.Application")
With myIE
.Visible = True
.Navigate EXaccount
Do Until .ReadyState = 4
DoEvents
Loop

End With
Do Until Not myIE.Busy
DoEvents
Loop

Application.Wait (Now + TimeValue("0:00:05"))
Set myDoc = myIE.Document
PutInClipboard myIE.Document
myIE.ExecWB 17, 0 '// SelectAll
myIE.ExecWB 12, 2 '// Copy selection

'the cursor seems to be 'lock' in the account cell as I notice that when i
Set the paste special To text, it Is able To paste only that account number & Not the page data that i want

Sheets("Temp").PasteSpecial Format:="HTML", link:=False, DisplayAsIcon:= _False

Set myIE = Nothing
' IE.Quit
End Function

Private Sub PutInClipboard(Doc As Object)
Dim CB As New DataObject

CB.SetText Doc.body.outerHTML
CB.PutInClipboard
End Sub

murfyang
07-22-2008, 09:02 AM
hi Tom, thanks. You know what, they have upgraded the site over the weekend & for awhile I thought I was almost there which I beleived strongly would have help some of my colleagues with vastly improving their work productivity, analysing clients portfolio in a more efficient way, saving them as much as 50% off this part of the work.
You mentioned about MSFroms reference set, mine is checked at Microsoft Forms 2.0 Object Library, if that is what you are refering to.

Alas!, back to login problem -

Set frm = ieDoc.frames(0).Document.forms 'runtime error 424 object required.

Discovered the IE developer toolbar, to those not aware of this, it can be set at IE toolbar View, Explorer Bar, IE Developer Toolbar.

There are 2 Frames - sublogin & enclogin. Form frmFAMELogin & IFRAME ifrtblLoginMenu_FAHubMenuBar105 is located at FRAME sublogin while another Form LoginForm is located at enclogin. Hoping that this may be of some help. I have again attached a snapshot of the the page with the IE developer toolbar turn on & also the code. Slowly having a grasp of these alien jargon with all your great unselfish generous assistance. :bow: