PDA

View Full Version : Get web page object through Word VBA?



EdNerd
05-14-2007, 08:27 AM
Is it possible in a Word 2003 macro to set objects to the the IE application and the current web page? I'd like to check a checkbox and activate the NextPage link (as long as there's a next page to go to).

I found some references to scripts that can do this, but I can't figure them out. I can't find an InternetExplorer library to reference in the VBE.

Any help is greatly appreciated.

Ed

EdNerd
05-17-2007, 08:08 AM
With much help from several people, I have a Word VBA macro that allows me to open an instance of IE, browse to the page I need, and read information from the page. What I can not do, though, is find and activate the two controls I need. I want to find and check a checkbox, and then find and activate the Next page link. I tried using SendKeys, but it didn't work - I think perhaps because I was stepping through the macro and the web page didn't have the focus. If anyone would like to point me in the right direction, your further help is greatly appreciated.
Ed


Sub Try_IE()
Dim objIE
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
'fmDoIE.Show
If MsgBox("Do you want to continue?", vbYesNo) = vbNo Then
objIE.Quit
Set objIE = Nothing
Exit Sub
End If
'MsgBox objIE.LocationURL
Dim objIE2
Set objIE2 = GetIE(objIE.LocationURL)
Dim docIE
Set docIE = objIE2.Document
'Get each line of the web page into an array
Dim PageArray
PageArray = Split(docIE.Body.InnerHTML, Chr(13))
Dim objDoc As Document
Dim docRng As Range
Dim parRng As Range
Set objDoc = Documents.Open("C:\Desktop\TestMe.doc")
Set docRng = objDoc.Content
Dim x As Long, y As Long, z As Long
x = UBound(PageArray)
Stop
For z = 0 To x
y = docRng.Paragraphs.Count
Set parRng = objDoc.Paragraphs(y).Range
parRng.Text = PageArray(z) & Chr(13)
Next z
Set parRng = objDoc.Paragraphs(docRng.Paragraphs.Count).Range
parRng.Text = "^p" & "^p" & "^p"
Set parRng = objDoc.Paragraphs(docRng.Paragraphs.Count).Range
Stop
On Error GoTo EndLoop
Dim frmIE
For Each frmIE In docIE.Forms
parRng.Text = "Name:= " & frmIE.Name & "; " & "ID:= " & frmIE.ID & vbCrLf
Set parRng = objDoc.Paragraphs(docRng.Paragraphs.Count).Range
Next frmIE
EndLoop:
On Error GoTo 0
'objIE2.Activate
SendKeys "^F", Wait
SendKeys "Clear checkboxes", Wait
SendKeys "{ENTER}", Wait
SendKeys "{ESC}", Wait
SendKeys "{TAB}", Wait
SendKeys " ", Wait

Stop
Set objIE = Nothing
Set objIE2 = Nothing
Set docIE = Nothing
'objDoc.Close wdDoNotSaveChanges
End Sub
'*******************************************
' huge thanks to Tim Williams for this function!
'Find an IE window with matching location
' Assumes no frames.
Function GetIE(sAddress As String) As Object

Dim objShell As Object, objShellWindows As Object, o As Object
Dim retVal As Object, sURL As String

Set retVal = Nothing
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows

'see if IE is already open
For Each o In objShellWindows
sURL = ""
On Error Resume Next
sURL = o.Document.Location
On Error GoTo 0
If sURL <> "" Then
If sURL Like sAddress & "*" Then
Set retVal = o
Exit For
End If
End If
Next o

Set GetIE = retVal
End Function