Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SendKeys from Attachmate to Internet Explorer

Status
Not open for further replies.

jimwal0367

Programmer
Dec 3, 2005
24
GB
Hi,

I am trying to write a macro using Attachmate Extra 7.1 to copy from an Extra screen, find the correct opened Internet Explorer window then paste the contents into a blank box on Internet Explorer.

I can get the macro to open a new IE window, navigate to a certaqin website(I used Google) pasted in the correct details and pressed enter.

This was completed using:
Call SendKeys("Some Text")
Call SendKeys("{ENTER}")

This all worked fine.

So then I tried to get the macro to look for the correct open IE window. By looking at the Title of each IE window
The macro finds the correct window but I get an error when I try to use SendKeys..
This is the error message I get:- Cannot edit while macro is running.

Here is my code so far(this code was found on this forum, but used the Document.url

Sub Main

Declare Variables
Dim ie as object
Dim objApp as object


Set object as a Shell type application
Set objApp = CreateObject("Shell.Application")

Create a For loop to search each Application open
for i = 0 to 9

Declare variable strName
strName = ""


On Error Resume Next

strNam equals current open IE window
strName = objApp.Windows(i).Document.title


an if method to confirm correct open IE Window
if InStr(strName,"Google") then



set ie = objApp.Windows(i)
exit for
end If
next

if ie is nothing then
msgbox("IE window not found")
else


end if

Call Sendkeys("SOME TEXT")


End Sub

---------------------------------------------------

Is this an easy fix?
Do I need to create another Sub Method?

Any hlep would be most appreciated


 
Your use of sendkeys is attempting to edit your macro as it is the active window when you run your script. Instead of sending "SOME TEXT" to IE your sending it to your macro editor and it assumes your trying to edit the script thats running, hence the error msg.

You should be sending keys to IE through the object your creating. e.x.

IE.Sendkeys("SOME TEXT")

note: the Call statement is not needed in the example.


[small]Sometimes you gotta leave your zone of safety. You have to manufacture Inspirado. You gotta get out of the apartment. You've got to run with the wolves. You've got to dive into the ocean and fight with the sharks. Or just treat yourself to a delicious hot fudge sundae........ with nuts. - Jack Black[/small]
 
Code:
Declare Sub Wait(objIE As Object)

Sub Main()
   Dim objIE As Object, i As Integer
   
   Set objIE = CreateObject("InternetExplorer.Application")

   With objIE
      .Visible = True
      .Navigate "[URL unfurl="true"]http://www.google.com"[/URL]
   End With
   
   Call Wait(objIE)
   
   ' View the HTML source on Google's page to see the
   ' [b]f, q [/b]and [b]btnG[/b] values

   objIE.Document.Forms("[b]f[/b]").all.item("[b]q[/b]").value = "TEST"

   'objIE.Document.Forms("[b]f[/b]").all.item("[b]btnG[/b]").Click   
   'Call Wait(objIE)
End Sub

Private Sub Wait(objIE As Object)
   While objIE.Busy
      DoEvents
   Wend
   
   While objIE.Document.ReadyState <> "complete"
      DoEvents
   Wend
End Sub
 
Thanks for the replies, they are most helpful.

Is objIE.Document.Forms("f").all.item("q").value = "TEST"

Looking for certain things on the webpage.
 
Yes, in Google's HTML source, f is the form name, q is the name of the textbox and btnG is the name of the submit button.
Code:
<form action="/search" name=[b]f[/b]>

<input maxlength=2048 name=[b]q[/b] size=55 title="Google Search" value="">

<input name=[b]btnG[/b] type=submit value="Google Search">
 
WinblowsME - I have tried the code that you posted above, but it returns an error with ref to the following code:
objIE.Document.Forms("f").all.item("q").value = "TEST"

It returns Object value is set to nothing
 
Are you using that code for the google page or for the page you're trying to pass information to?

If it's the page you're trying to pass information to, then it's very likely the form is not "f" and the item is not "q". The easiest thing to do is simply look at the source to find this information, barring that the following code will get you the names of the forms. Change "form" to "input" for buttons, fields, checkboxes, etc. With "input" type, you might want to msgbox the type as well.

Code:
Dim oForms, oForm
Set oForms = oIE.Document.GetElementsByTagName("form")
If oForms Is Nothing Then
  MsgBox "No forms on this page"
Else
  For Each oForm In oForms
    On Error Resume Next
    MsgBox "Name: " & oForm.Name & vbCrLf & "ID: " & oForm.Id
    On Error Goto 0
  Next
End If

Also, I don't think EB has a For Each loop, so you may need to change the beginnig of loop to:
Code:
For i = 1 to oForms.Count
  Set oForm = oForms(i)
 
I was using the code for Google as a test web site. Just to find the text box and enter some text, if I could get this to work I was going to adapt my code to work with an internal website

 
Then, you'd have to post your code because WinblowsME's code works.
 
Thats strange, I have run WinblowsME's code and I get an error saying 'Object set to nothing'
 
Do you get that error on a line? If you step through the code when do you get the error?
 
Line 19 is blank in the example. Post the code you're using.
 
Code:
1.  Declare Sub Wait(objIE As Object)
2.
3. Sub Main()
4. Dim objIE As Object
5.   dim objApp as object
6.      
7.  Set objIE = CreateObject"InternetExplorer.Application")
8.   With objIE
9.      .Visible = True
10.      .Navigate "[URL unfurl="true"]http://www.google.com"[/URL]
11.   End With
12.   
13.   'Call Wait(objIE)
14.   
15.   ' View the HTML source on Google's page to see the
16.   ' f, q and btnG values
17.
18.   set objApp = objIE.Document.Forms("f").all.item"q").value = "TEST"
19.    'set objApp = objIE.document.forms("q").value = "Test"
20.   'objIE.Document.Forms("f").all.item("btnG").Click   
21.   'Call Wait(objIE)
22.    End Sub
23.
24.    Private Sub Wait(objIE As Object)
25.      While objIE.Busy
26.      DoEvents
27    Wend
28.   
29.   While objIE.Document.ReadyState <> "complete"
30.      DoEvents
31.      Wend
32.    End Sub


Line 18 is the problem
 
Don't comment out the Wait procedure because you have to wait until the web page finishes loading before you can access the document objects.
Code:
13.   [COLOR=red]'[/color]Call Wait(objIE)

Typos in red.
Code:
7.  Set objIE = CreateObject[COLOR=red]([/color]"InternetExplorer.Application")

18.   [COLOR=red]set objApp = [/color]objIE.Document.Forms("f").all.item[COLOR=red]([/color]"q").value = "TEST"

Jim, if you use my code and not make a single change, it should run fine.
 
Finally Cracked it...

I used the following code
Code:
Declare Sub Wait(objIE As Object)

Sub Main()
   Dim objIE As Object, i As Integer
   
   Set objIE = CreateObject("InternetExplorer.Application")

   With objIE
      .Visible = True
      .Navigate "[URL unfurl="true"]http://www.google.com"[/URL]
      
      
   End With
   
   Call Wait(objIE)
   
   ' View the HTML source on Google's page to see the
   ' f, q and btnG values
   [COLOR=green]objIE.Document.All("q").Value = "Test"[/color]
   [COLOR=green]objIE.Document.All("btnG").Click[/color]

   
   Call Wait(objIE)
End Sub

Private Sub Wait(objIE As Object)
   While objIE.Busy
      DoEvents
   Wend
   
   While objIE.Document.ReadyState <> "complete"
      DoEvents
   Wend
End Sub
I changed the code in green
Thanks for all of your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top