• Status: Solved
  • Priority: Medium
  • Security: Private
  • Views: 31
  • Last Modified:

How to click on a IE save button via VBA using the qtip ID instead of the button id?

The below code works, but the save button ID on our company website that uses iFrames keeps changing.
Current code:
objFrameDoc.getElementById("ext-gen53").Click

Open in new window



Here are two examples that I have discovered so far.  I’m not sure if this is it or if there will be anymore.  
<TD class="x-btn-mc "><EM unselectable="on"><BUTTON id=ext-gen41 class=" x-btn-text" style="OVERFLOW: hidden; BACKGROUND-IMAGE: url(https://servman/sm/cwc/../images/obj16/tsave.gif); OVERFLOW-X: hidden; OVERFLOW-Y: hidden; WIDTH: 46px" qtip="Save Record (Ctrl+Shift+F4)">Save</BUTTON></EM></TD>

Open in new window


<TD class="x-btn-mc "><EM unselectable="on"><BUTTON id=ext-gen53 class=" x-btn-text" style="OVERFLOW: hidden; BACKGROUND-IMAGE: url(https://servman/sm/cwc/../images/obj16/tsave.gif); OVERFLOW-X: hidden; OVERFLOW-Y: hidden; WIDTH: 46px" qtip="Save Record (Ctrl+Shift+F4)">Save</BUTTON></EM></TD>

Open in new window

0
kbay808
Asked:
kbay808
  • 10
  • 10
1 Solution
 
Rgonzo1971Commented:
Hi,

pls try
Set objCollection = IE.document.getElementsByTagName("BUTTON")
For Each el In objCollection
    If el.outerHtml Like "*Save Record (Ctrl+Shift+F4)*" Then
        el.Click
        Exit For
    End If
Next el

Open in new window

Regards
0
 
kbay808Author Commented:
No joy, I got the attached error.  I also declared the objCollection As Object and got the same error.
Error.JPG
Debug.JPG
0
 
Rgonzo1971Commented:
try to replace IE with objIE

or  ie.document with objFrameDoc
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
kbay808Author Commented:
Now I get a "Can’t execute code in break mode" error.  Here is the entire code with the above change.

Sub WIP_Ticket()

    Dim objIE As SHDocVw.InternetExplorer
    Dim objShell As SHDocVw.ShellWindows
    Dim objIEDoc As MSHTML.HTMLDocument
    Dim objFound As MSHTML.HTMLBaseElement
    Dim strURL As Variant
    Dim blnFoundWebSite As Boolean
    Dim objDialog As FileDialog
    Dim objFSO As Scripting.FileSystemObject
    Dim objText As Scripting.TextStream
    Dim objDebugFile As Scripting.TextStream
    
    Dim varFrame As Variant
    Dim varFrame2 As Variant
    Dim objFrameDoc As MSHTML.HTMLDocument
    Dim objFrameDoc2 As MSHTML.HTMLDocument
    Dim i As Integer
    Dim j As Integer
    
    Dim objElem As MSHTML.HTMLBaseElement
    Dim objElemDoc As MSHTML.HTMLDocument
    Dim objElem2 As MSHTML.HTMLBaseElement
    Dim objElemDoc2 As MSHTML.HTMLDocument
    Dim objCollection As Object

        
    Set objShell = New SHDocVw.ShellWindows
    Set objFSO = New Scripting.FileSystemObject
    ' **** Set the Web Address Here ****
    strURLS = "https://servman/sm/index.do,https://servman/sm/index.do#,https://servman/sm/index.do?lang=en,https://servman/sm/index.do?lang=en&mode=index.do,https://servman/sm/index.do?lang=en&mode=index.do&logout_msg=LogoutPage.session_timeout"
  
    ' Look for the appropriate open IE Window
    For Each objIE In objShell
        If TypeOf objIE.document Is MSHTML.HTMLDocument Then
            Set objIEDoc = objIE.document
            If Not (objIEDoc Is Nothing) Then
                For Each strURL In Split(strURLS, ",")
                    If objIEDoc.url = strURL Then
                        blnFoundWebSite = True
                        Exit For
                    End If
                Next
                If blnFoundWebSite = True Then Exit For
            End If
        End If
    Next
    
    Set objFrameDoc2 = objIEDoc

    If blnFoundWebSite Then
        ' Make sure IE is done loading the page
        Do Until Not objIE.Busy
            DoEvents
        Loop
        
        ' Make Sure we had some frames returned
        If objIEDoc.frames.Length > 0 Then
            ' Get the Document from the 1st Frame
            Set varFrame = objIEDoc.frames.Item(0)
            Set objFrameDoc = varFrame.document
            
            ' Get the Detail Frame (Sub Frame #2)
            If objFrameDoc.frames.Length > 1 Then
                Set varFrame2 = objFrameDoc.frames.Item(1)
                Set objFrameDoc2 = varFrame2.document
                
                'Title
                
                Set objFound = objFrameDoc2.getElementById("X95")
                If objFound Is Nothing Then
                    
                Else
                    Range("K25").Value = objFound.Value
                    objFound.Value = Range("M31")
                End If
                
                'Update
                
                Set objFound = objFrameDoc2.getElementById("X159")
                If objFound Is Nothing Then
                    
                Else
                    objFound.Value = "Beginning assignment process"
                End If
                

                
            Else
               
                'Title
                Set objFound = objFrameDoc.getElementById("X95")
                If objFound Is Nothing Then
                    
                Else
                    Range("K25").Value = objFound.Value
                    objFound.Value = Range("M31")
                End If
                
                Set objFound = objFrameDoc.getElementById("X159")
                If objFound Is Nothing Then
                    
                Else
                    objFound.Value = "Beginning assignment process"
                End If
                
            End If ' objFrameDoc.frames.Length > 1
            
        End If ' objIEDoc.frames.Length > 0
        
        
        
    End If
    
    'Original Title
    Range("AX75") = Range("M5")
    
    'Save ticket
        Set objCollection = objIE.document.getElementsByTagName("BUTTON")
    For Each el In objCollection
        If el.outerHTML Like "*Save Record (Ctrl+Shift+F4)*" Then
            el.Click
            Exit For
        End If
    Next el
    
    Set objIE = Nothing
    Set objShell = Nothing
    Set objIEDoc = Nothing
    Set objIEDoc = Nothing
    Set objFound = Nothing
    Set objDialog = Nothing
    Set objFSO = Nothing
    Set objText = Nothing
    Set objDebugFile = Nothing
End Sub

Open in new window

0
 
Rgonzo1971Commented:
do you get the error on break mode ?
0
 
kbay808Author Commented:
I don't know what break mode is.
0
 
Rgonzo1971Commented:
How do you run the macro?
0
 
kbay808Author Commented:
I assign it to a button and run it from there.
0
 
Rgonzo1971Commented:
At which line is the error?
0
 
kbay808Author Commented:
Line 119
Set objCollection = objIE.document.getElementsByTagName("BUTTON")
0
 
Rgonzo1971Commented:
then try

objFrameDoc.getElementsByTagName("BUTTON") instead
0
 
kbay808Author Commented:
No error for that line now, but now there is a run-time error '424' (Object required) for line 120.  
Line 120:  For Each el In objCollection
0
 
Rgonzo1971Commented:
Let's try another method

Set objCollection = objIEDoc.document.getElementsByClassName(" x-btn-text") ' or "x-btn-mc "
For Each el In objCollection
    If el.outerHtml Like "*Save Record (Ctrl+Shift+F4)*" Then
        el.Click
        Exit For
    End If
Next el

Open in new window

0
 
kbay808Author Commented:
Run-time error ‘438’:
Object doesn’t support this property or method
Line with error:
Set objCollection = objIEDoc.document.getElementsByClassName(" x-btn-text") ' or "x-btn-mc "

I also tried the following lines with no luck:
Set objCollection = objIEDoc.document.getElementsByClassName(" x-btn-text") ' or "x-btn-mc ")
Set objCollection = objIEDoc.document.getElementsByClassName(" x-btn-text")
Set objCollection = objIEDoc.document.getElementsByClassName("x-btn-mc ")
0
 
Rgonzo1971Commented:
pls try

Set objCollection = objIEDoc.getElementsByClassName(" x-btn-text") ' or "x-btn-mc "
For Each el In objCollection
    If el.outerHtml Like "*Save Record (Ctrl+Shift+F4)*" Then
        el.Click
        Exit For
    End If
Next el

Open in new window

0
 
kbay808Author Commented:
There is no error, but it does not click save.  Just an idea to throw out there, is there a way to identify the element ID for [qtip="Save Record (Ctrl+Shift+F4)"]?  Then use that to fill in the original code?

objFrameDoc.getElementById("Found ID").Click

Open in new window

0
 
Rgonzo1971Commented:
What is the result in the imediate window

Set objCollection = objIEDoc.getElementsByClassName(" x-btn-text") ' or "x-btn-mc "
For Each el In objCollection
    Debug.Print el.outerHtml
    If el.outerHtml Like "*Save Record (Ctrl+Shift+F4)*" Then
        el.Click
        Exit For
    End If
Next el
0
 
kbay808Author Commented:
There was no debug file created when I ran the above code, but I attached a copy of a debug file from a previous tool.  Hopefully it helps.  I highlighted the part of code for the save button in green.
Debug-File-Sanitized-.docx
0
 
Rgonzo1971Commented:
Sorry can't help further
0
 
kbay808Author Commented:
Thanks for trying
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 10
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now