Link to home
Start Free TrialLog in
Avatar of kbay808
kbay808Flag for United States of America

asked on

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

Avatar of Rgonzo1971
Rgonzo1971

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
Avatar of kbay808

ASKER

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

or  ie.document with objFrameDoc
Avatar of kbay808

ASKER

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

do you get the error on break mode ?
Avatar of kbay808

ASKER

I don't know what break mode is.
How do you run the macro?
Avatar of kbay808

ASKER

I assign it to a button and run it from there.
At which line is the error?
Avatar of kbay808

ASKER

Line 119
Set objCollection = objIE.document.getElementsByTagName("BUTTON")
then try

objFrameDoc.getElementsByTagName("BUTTON") instead
Avatar of kbay808

ASKER

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
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

Avatar of kbay808

ASKER

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 ")
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

Avatar of kbay808

ASKER

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

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
Avatar of kbay808

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kbay808

ASKER

Thanks for trying