Results 1 to 12 of 12
  1. #1
    osuejm is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2013
    Posts
    8

    Automate IE navigation works when stepping through code but does not work when runnin

    So I have been trying to automate some actions on a intranet website we have to upload and download files the user wants.

    When I step into the logic I created and press F8 to go line by line, the code works, it extracts the hyperlink that I want.

    But when I run the code, it gives no error, but it does not find what I am looking for either.

    Here is the code I have been using: '''PortalValue is defined from user and just not included in this excerpt of code
    strURL = "http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxx&folderID=" & PortalValue & ""

    'view to go straight to upload
    Set objIE = CreateObject("InternetExplorer.Application")
    objIE.Visible = True


    objIE.Navigate strURL

    'wait to page comes up
    While objIE.Busy
    DoEvents
    Wend


    Set LinkCollection = objIE.Document.links


    i = 1

    'this code below works but still stops at 7 instead of 35
    Debug.Print LinkCollection.length
    For Each link In LinkCollection
    ' While i < LinkCollection.Length
    'If InStr(LinkCollection.Item(i).href, "download") > 0 Then
    If InStr(link, "download") > 0 Then
    Debug.Print link 'successful hit if I reach here
    End If
    'i = i + 1
    Next

    Set LinkCollection = Nothing
    Set objIE = Nothing


    So as you see in the code, i started down the path of figuring out what is occurring and when I am in debug mode, it tells me there is 35 links. But when I run the code it only returns 7 links total (none of which are what i want).

    The site itself is a mix of html tables and javascript, but I am just not sure how to get the running code to equal what I get in debug mode. I read that sometimes you need to code a little different when javascript is present, but I am not an expert when it comes to this stuff. Can anyone help me figure out next steps and how to resolve this issue?

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    The other links are probably nested within another node. For instance, 7 hrefs are at the document body level while others are within another element such as TABLE.

    Did you look at the entire source code to see if the HREF's you are looking for are within other open and close tags?

  3. #3
    osuejm is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2013
    Posts
    8
    Quote Originally Posted by ItsMe View Post
    The other links are probably nested within another node. For instance, 7 hrefs are at the document body level while others are within another element such as TABLE.

    Did you look at the entire source code to see if the HREF's you are looking for are within other open and close tags?

    There are several tables but I do not know how to jump to the next sequence. The webpage itself has a header area across the top, then a left column and a right column (its a internal built document repository).

    I can find where in the source code it stops but when I tried to get elements by tagname and look for tables, it does the same thing, in debug mode shows 47 tables but when it runs it only returns 5 tables. So I am still stumped.

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    So LinkCollection.Length has all of the HREF you are looking for? Maybe it is the instr "Download" that is limiting your result to 7.

    Specifically, what has the value of 35 in the code you posted?

  5. #5
    osuejm is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2013
    Posts
    8
    Quote Originally Posted by ItsMe View Post
    So LinkCollection.Length has all of the HREF you are looking for? Maybe it is the instr "Download" that is limiting your result to 7.

    Specifically, what has the value of 35 in the code you posted?
    The 35 refers to the LinkCollection.Length when in debug mode. the instr "Download" in debug mode will eliminate the 35 down to 1 answer, which is what I want. So when I run the code, the LinkCollection.length only returns 7 before it even gets to the instr code.

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Try declaring link

    Dim link as HTMlLinkelement

    Then comment out the instr lines

    put in
    Debug.Print link.innertext

  7. #7
    osuejm is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2013
    Posts
    8
    Quote Originally Posted by ItsMe View Post
    Try declaring link

    Dim link as HTMlLinkelement

    Then comment out the instr lines

    put in
    Debug.Print link.innertext

    I currently have LInkCollection as Object and Dim link and thats it. Dim link as HTMLLinkelement is not understood by Access when I put it in.

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Reference Microsoft HTML Object Library

  9. #9
    osuejm is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2013
    Posts
    8
    Quote Originally Posted by ItsMe View Post
    Reference Microsoft HTML Object Library
    Ok got the reference set, ran in debug mode and I get 11 innertext fields back. When I run it, i get 2 back.

  10. #10
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Try this code. If it is a link it will have an HREF tag. This should go after all of the HREF's in the Document Body Node. Forget step debug for now. Use Control + G and look at what prints in your immediate window. The code should be bug free. Not tested.. You will need to get your URL adjusted.

    Code:
    Private Sub Command0_Click()
    
    Dim strURL As String
    strURL = "http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxx&folderID=" & PortalValue & ""
    Dim objIE As InternetExplorer
    Dim htmlDoc As HTMLDocument
    Dim LinkCollection As IHTMLElementCollection
    Dim htmlElement As HTMLHtmlElement
    Dim intCount As Integer
    'view to go straight to upload
    Set objIE = CreateObject("InternetExplorer.Application")
    objIE.Visible = True
    objIE.navigate strURL
    'wait to page comes up
    While objIE.ReadyState <> 4 Or objIE.Busy = True: Wend
    Set htmlDoc = objIE.Document
    While htmlDoc.ReadyState <> "complete": Wend
    Set LinkCollection = htmlDoc.getElementsByTagName("HREF")
    intCount = 0
    For Each htmlElement In LinkCollection
    Debug.Print "HREF element Index " & intCount
    Debug.Print "outerHTML = " & htmlElement.outerHTML
    Debug.Print "innerText = " & htmlElement.innerText
    intCount = intCount + 1
    Next
    
    Set htmlElement = Nothing
    Set LinkCollection = Nothing
    Set htmlDoc = Nothing
    Set objIE = Nothing
    End Sub

  11. #11
    osuejm is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2013
    Posts
    8
    Quote Originally Posted by ItsMe View Post
    Try this code. If it is a link it will have an HREF tag. This should go after all of the HREF's in the Document Body Node. Forget step debug for now. Use Control + G and look at what prints in your immediate window. The code should be bug free. Not tested.. You will need to get your URL adjusted.

    Code:
    Private Sub Command0_Click()
    
    Dim strURL As String
    strURL = "http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxx&folderID=" & PortalValue & ""
    Dim objIE As InternetExplorer
    Dim htmlDoc As HTMLDocument
    Dim LinkCollection As IHTMLElementCollection
    Dim htmlElement As HTMLHtmlElement
    Dim intCount As Integer
    'view to go straight to upload
    Set objIE = CreateObject("InternetExplorer.Application")
    objIE.Visible = True
    objIE.navigate strURL
    'wait to page comes up
    While objIE.ReadyState <> 4 Or objIE.Busy = True: Wend
    Set htmlDoc = objIE.Document
    While htmlDoc.ReadyState <> "complete": Wend
    Set LinkCollection = htmlDoc.getElementsByTagName("HREF")
    intCount = 0
    For Each htmlElement In LinkCollection
    Debug.Print "HREF element Index " & intCount
    Debug.Print "outerHTML = " & htmlElement.outerHTML
    Debug.Print "innerText = " & htmlElement.innerText
    intCount = intCount + 1
    Next
    
    Set htmlElement = Nothing
    Set LinkCollection = Nothing
    Set htmlDoc = Nothing
    Set objIE = Nothing
    End Sub

    I entered it all and I get the same 7 as I did with the before code.

    So here is a snippet of the source webpage code (taking out the web address) on where it stops highlighted in blue:



    <a href="http://portal.xxxxxxxxxxxxxxxxxxxxxxxxxxx/overview?projID=57454" style="font-weight:bold; font-size:105%">
    Overview
    </a>

    </td>
    <td class="banHeaderBg" width="0%" style="padding:0">
    <img src="http://portal.xxxxxxxxxxxxxxxxxxxxxxxx/private/img/clear.gif" height="1" width="2" align="absmiddle" border="0" />
    </td>


    <td width="0%" class="collabTabOn actionbarText" align="center" noWrap style="padding-left:15; padding-right:15; color:black">

    <img src="http://portal.xxxxxxxxxxxxxxxxxxxxx/img/genericFileIcon.gif" align="absmiddle" border="0" />

    <a style="font-weight:bold; font-size:105%">
    Documents
    </a>


    </td>
    <td class="banHeaderBg" width="0%" style="padding:0">
    <img src="http://portal.xxxxxxxxxxxxxxxxxxxxxx/private/img/clear.gif" height="1" width="2" align="absmiddle" border="0" />
    </td>


    <td width="100%">&nbsp;</td>
    </tr>
    </table>


    <!--------------------------------- End Tabs ---------------------------------->


    </td></tr>
    <tr><td height="4"></td></tr>
    <tr height="100%"><td>
    <table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%" >
    <tr><td valign="top">




    <script type="text/javascript">
    //the script here is for the detail

    The part I need is after this javascript in the middle of the page. Here is what I am looking for (highlighted in green):

    <isFolder class="Boolean">false</isFolder>
    <canCheckOut class="Boolean">true</canCheckOut>
    <canCheckIn class="Boolean">false</canCheckIn>
    <canCreateShortcut class="Boolean">true</canCreateShortcut>
    <canNotify class="Boolean">true</canNotify>
    <canPublish class="Boolean">true</canPublish>
    <canSubmitToWorkflow class="Boolean">true</canSubmitToWorkflow>
    <canCopy class="Boolean">true</canCopy>
    <canMove class="Boolean">true</canMove>
    <canWebEdit class="Boolean">false</canWebEdit>
    <canOverrideCheckOut class="Boolean">false</canOverrideCheckOut>
    <canModerate class="Boolean">false</canModerate>
    <type>d</type>
    <canEdit class="Boolean">true</canEdit>
    <canDelete class="Boolean">true</canDelete>
    <id>161831</id><columnValues class="Array">
    <columnValue index="0" class="PTTextColumnValue">
    <value>&lt;![CDATA[<html-frag><nobr><img src="http://portal.xxxxxxxxxxxxxxxxxxx/private/img/clear.gif" height="6" width="6" align="absmiddle" border="0" /><img src="http://portal.xxxxxxxxxxxxxxxxx/private/img/iconDocPDF16.gif" height="16" width="16" align="absmiddle" border="0" />&nbsp;<a href="http://portal.xxxxxxxxxxxxxxxxxxxxxxxxxxxx/collab/docman/download/161831/0/0/0/Service Desk.pdf" target="_blank" class="collabAnchorText" title="Service Desk.pdf">Service Desk.pdf</a>

  12. #12
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    This seems to be working....

    Code:
    Private Sub Command0_Click()
    Dim strURL As String
    strURL = "C:\Test\Internet\Sample.htm"
    Dim objIE As InternetExplorer
    Dim htmlDoc As HTMLDocument
    Dim LinkCollection As IHTMLElementCollection
    Dim linkElement As HTMLLinkElement
    Dim strLink As String
    Dim strOuter As String
    Dim intCount As Integer
    Dim strFindMid As String
    strFindMid = "href=" & """"
    'view to go straight to upload
    Set objIE = CreateObject("InternetExplorer.Application")
    objIE.Visible = True
    objIE.navigate strURL
    'wait to page comes up
    While objIE.ReadyState <> 4 Or objIE.Busy = True: Wend
    Set htmlDoc = objIE.Document
    While htmlDoc.ReadyState <> "complete": Wend
    Set LinkCollection = htmlDoc.getElementsByTagName("A")
    intCount = 0
    For Each linkElement In LinkCollection
    Debug.Print "A element Index " & intCount
    strOuter = linkElement.outerHTML
        If InStr(strOuter, strFindMid) Then
            strOuter = Left(strOuter, InStrRev(strOuter, """" & ">") - 1)
            strLink = Mid(strOuter, InStrRev(strOuter, strFindMid) + 6)
        End If
        
    Debug.Print "strOuter = " & strOuter
    Debug.Print "strLink = " & strLink
    intCount = intCount + 1
    Next
    Set htmlElement = Nothing
    Set LinkCollection = Nothing
    Set htmlDoc = Nothing
    Set objIE = Nothing
    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. similar code, one works, the other doesn't
    By johnseito in forum Access
    Replies: 15
    Last Post: 11-03-2013, 07:59 PM
  2. Replies: 6
    Last Post: 09-02-2012, 04:30 PM
  3. Record navigation buttons work backward
    By jonesy29847 in forum Access
    Replies: 22
    Last Post: 04-11-2011, 06:17 AM
  4. Replies: 1
    Last Post: 01-04-2011, 05:04 AM
  5. Replies: 3
    Last Post: 10-18-2009, 09:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums