'Automating the SAP netweaver portal - The Everest of Automation!
'
'There are a few articles on the web discussing the automation of the SAP Portal - some even saying its a myth.
'
'Ok it is possible using VBA..(Excel). Could be adapted for vb script.
'
'This assumes you have some knowledge of HTML DOM and general automation.
'
'
'1. Get the URL of your portal. Sometimes the portal window does not display the address bar.
'This example will get the URL of a window with "Home - SAP NetWeaver Portal" (depending on your env).
'Open a portal and run this function.
Debug.Print oGetPortal.LocationURL
Function oGetPortal( _
Optional sWindowTitle AsString = "Home - SAP NetWeaver Portal" _
) AsObject
Set objShellApp = CreateObject("Shell.Application")
ForEach objWindow In objShellApp.Windows
DoEvents
If LCase(objWindow.LocationName) = LCase(sWindowTitle) Then
Set oGetPortal = objWindow
ExitFor
EndIf
Next
Set objShellApp = Nothing
Set objWindow = Nothing
EndFunction
'2 For automation you probably want to create a new instance of the IE portal rather than attach to
'an existing window. The Portal often runs on an intranet.
'In windows 7 the IE object automatically detaches from intranet pages - not very useful for automation.
'
'So the below code works around this issue & should work on earlier windows versions and non intranet pages
'it also opens up the portal in an IE window with an address bar and is easier to get access to the IE developer tools.
Debug.Print oGetLatestPortal.LocationURL
PrivateDeclareSub Sleep Lib"kernel32" (ByVal dwMilliseconds AsLong)
PrivateDeclareFunction GetForegroundWindow Lib"user32" () AsLong
Function oGetLatestPortal( _
Optional sURL AsString = "http://myportalpath/portal#", _
Optional sWindowTitle AsString = "Home - SAP NetWeaver Portal") AsObject
Dim IE As InternetExplorer
Set IE = New InternetExplorer
' only works for about 5 portal windows
IE.Navigate sURL
Dim hwnd AsLong
hwnd = GetForegroundWindow() ' get latest IE window
Set IE = Nothing ' work around for windows 7 issue
i = 0
DoWhile i < 10And IE IsNothing
i = i + 1
Set objShellApp = CreateObject("Shell.Application")
ForEach objWindow In objShellApp.Windows
DoEvents
If LCase(objWindow.LocationName) = LCase(sWindowTitle) Then
If objWindow.hwnd = hwnd Then'active IE window if more than one with same title
Set IE = objWindow
EndIf
EndIf
Next
DoEvents
Sleep 100
Loop
If IE IsNothingThen
MsgBox "nothing"
ExitFunction
End If
Set oGetLatestIE = IE
EndFunction
'3. Wait for IE page to load. You cannot do any automation until after
'the page is loaded. This function returns true if the page or frames top level is loaded (frames discussed later)
Debug.Print lWait(oGetLatestPortal)
Function lWait(LOIE AsObject) AsBoolean
Dim t1 AsLong
Dim t2 AsLong
OnErrorResumeNext
t1 = Timer
t2 = t1 + 60
Do
If Timer > t2 Then lWait = False: ExitFunction
If VarType(LOIE.Document) = vbString ThenExitDo
Loop
Do
If Timer > t2 Then lWait = False: ExitFunction
If LOIE.Busy = FalseThenExitDo
Loop
Do
If Timer > t2 Then lWait = False: ExitFunction
If VarType(LOIE.Document.readyState) = "C"ThenExitDo
Loop
Do
If Timer > t2 Then lWait = False: ExitFunction
If LOIE.Document.readyState = "complete"ThenExitDo
Loop
lWait = True
EndFunction
'4. Using developer tools in IE8+ (F12) with the window you just opened using the above code.
'Use the IE inspect tool (arrow) to click on the field or link/field/button you want to automate. This will
'expand the HTML DOM and show the HTML tag corresponding to the field. If you can see the tag you can automate
'it! But it can be tricky.
'
'5. Clicking on a link. Now that you have open a portal window you want to do something usually click on a link.
'Using the developer tools in IE. Find the link you want. If the link you want is inside a HTML form or frame
'tag see further on.
lWait IE
Set link = IE.Document.getElementById("MyLink_1_1") ' Can be a div or anchor
link.click
' Waiting for elements to appear.
'
' Even though you call lWait and IE is ready, the portal may be doing things. eg.
' displaying spinners (etc) Sometimes you must wait for the element to appear.
' Note elements can be forms, frames, links or fields or anything.
'
' safer way to get a element that will wait until it appears
Dim element AsObject
Dim t1 AsLong
Dim t2 AsLong
t1 = Timer
t2 = t1 + 10' secs
While element IsNothing
DoEvents
If Timer > t2 Then
MsgBox "timeout"
ExitDo
EndIf
' note, IEdoc can be IE.Document, SubFrame.contentWindow.Document or SubForm.Document (discussed later)
Set element = IEdoc.getElementById("elementid")
Wend
'If you still have problems try to get an element preceeding the element you want
'Use the VB debugger and also put sleeps in and use while loops as shown also you must get any
' forms or frames preceeding the element.
'
'
'
'6. Elements in Forms or Frames
'If you still can't get the element by elementbyid or by looping through getElementsByTagName,
'It could be because the field you want is in a form or frame.
'You should get a reference to any forms or frame preceeding the element first. Use F12 developer tools
'to find out if there are any precedding forms or frames.
'6 a.HTML frames
'Get an obj reference to a html frame
Dim subframe1 AsObject
' Assuming you have any preceeding Forms or Frames
Set subframe1 = IE.Document.getElementById("frameid") ' example
lWait subframe1.contentWindow
'Once you get a obj reference to a frame you must get the sub document
'before you can do any further searches
Dim myframedocument AsObject
Set myframedocument = subframe1.contentWindow.Document
Set subframe2 = myframedocument.getElementById("frameid2") ' example
'
'6b HTML forms
'
'A HTML Form is similar, again you must get any preceeding Forms or Frames
Dim subform AsObject
Set subform = IE.Document.getElementById("formid") ' example
Dim myformdocument AsObject
Set myformdocument = subform.Document
Set mylink = myformdocument.getElementById("mylink") ' example
'7.Dynamic Content. In the portal HTML elements id's can change depending on the context ie whether certain
'sections are data driven or due to different user's role, certain elements are displayed. In the previous
'example Link_1_1 might be a different menu option on one users login to another. So in these cases
'you need to write more generic code. 2 examples.
'
' using a tags attributes
For k1 = 0To IE.Document.links.Length - 1
If IE.Document.links.Item(k1).getAttribute("title") = "Show Attachments"Then
IE.Document.links.Item(k1).Click
ExitFor
EndIf
Next k1
' Loop through a table looking for a link's screen text
Set oTbl = IE.Document.getElementById("myNavigationTable")
Set oTBody = oTbl.getElementsByTagName("TBODY").Item(0)
Set oTRow = oTBody.getElementsByTagName("TR").Item(0)
Set oTds = oTRow.getElementsByTagName("TD")
ForEach oTd In oTds
DoEvents
If InStr(oTd.innertext, "Link Text") > 0Then
Set link = oTd.getElementsByTagName("A").Item(0)
ExitFor
EndIf
Next oTd
link.Click
'8.If you hit a back button element on a page you might have to
're-establish VB objects in your code as sometimes they lose reference.
'
'9.Finally this is not a turn key solution - ideally a function could be written to find a control
'by searching the DOM tree handling all frames and forms and waits
'
'I hope this post helps