Вы находитесь на странице: 1из 2

To see this demo in action, and be able to follow it through and learn how to grab from it what you

need:

Create a form with a lable over a textbox, make the textbox multi-line, and stick 3 command buttons
on the form. Don't rename any of them - the program expects the defaults of label1, text1,
command1, command2, command 3

The following CODE is the complete FORM CODE to copy/paste into it. Any questions/claifications
required for the code, ask away...

===========
Option Explicit
Public TargetIE As SHDocVw.InternetExplorer
Private Sub Command1_Click() ' Send text to first IE-document found
GetTheIEObjectFromSystem
SendTextToActiveElementWithSubmitOptionSet (False)
End Sub
Private Sub Command2_Click() ' Send text to IE-document containing EE thread
GetTheIEObjectFromSystem ("Q_26773800")
SendTextToActiveElementWithSubmitOptionSet (False)
End Sub
Private Sub Command3_Click() 'submit text to IE-document containing EE-thread
GetTheIEObjectFromSystem ("Q_26773800")
SendTextToActiveElementWithSubmitOptionSet (True)
End Sub
Private Sub Form_Load()
Me.Label1.Caption = "Text to send"
Me.Text1 = "This is a sample text message set and submitted programmatically"
'make text1 multiline in design
Me.Command1.Caption = "Text to the first IE browser document found"
Me.Command2.Caption = "Text to the EE-thread browser window"
Me.Command3.Caption = "Submit text to the EE thread browser target"
End Sub
Public Sub GetTheIEObjectFromSystem(Optional ByVal inurl As String = ".") '
will be found in ALL browser URLs
Dim SWs As New SHDocVw.ShellWindows
Dim IE As SHDocVw.InternetExplorer
Dim Doc As Object
For Each IE In SWs

"."

If TypeOf IE.Document Is HTMLDocument Then ' necessary to avoid Windows


Explorer
If InStr(IE.LocationURL, inurl) > 0 Then
Set TargetIE = IE
Exit For
End If
End If
Next
Set SWs = Nothing
Set IE = Nothing
End Sub
Private Sub SendTextToActiveElementWithSubmitOptionSet(ByVal bSubmitIt As Boolean)
Dim TheActiveElement As IHTMLElement
Set TheActiveElement = TargetIE.Document.activeElement
If Not TheActiveElement.isTextEdit Then
MsgBox "Active element is not a text-input system"
Else
TheActiveElement.Value = Me.Text1.Text
Dim directParent As IHTMLElement
If bSubmitIt Then
Dim pageForm As IHTMLFormElement
Set directParent = TheActiveElement.parentElement
' find its parent FORM element by checking parent nodes up and up and
up until found or BODY
Do While (UCase(directParent.tagName) <> "FORM" And
UCase(directParent.tagName <> "BODY"))
Set directParent = directParent.parentElement
Loop
If UCase(directParent.tagName) = "FORM" Then
Set pageForm = directParent
pageForm.submit 'intrinsic Form-element Method
Else
MsgBox ("Error: No form unit for submitting the text on this
page!")
End If
End If
Set pageForm = Nothing
Set directParent = Nothing
End If
Set TheActiveElement = Nothing
Set TargetIE = Nothing
End Sub

Вам также может понравиться