+ Reply to Thread
Results 1 to 10 of 10

VBA and MSHTML, extract img ALT attribute from a link

  1. #1
    Registered User
    Join Date
    02-10-2012
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    19

    VBA and MSHTML, extract img ALT attribute from a link

    Hi forum,

    I'm fairly new to VBA.

    Scenario:

    I'm using VBA and MSHTML library to extract an anchor text from the URL (back link). Let's say domain.com is linking to domain2.com, so, I'm using this to scan domain.com and see which anchor text it's using to link to domain2.com
    Below is an extract from the code, which finds the anchor text which is used in the URL:

    =======

    HTML Code: 
    =======

    My question is: How do I go about a situation where I would like it to return an ALT attribute from a link which doesn't have an anchor but an ALT attribute instead; link format is the following:

    <a href="domain2.com"><img src="(image location)" alt="ALTTEXT"/></a>

    Basically I'm trying to get it to return ALTTEXT value instead or, even better, check if it has anchor and if not, then return ALTTEXT value.


    Thanks.
    Last edited by guitalex; 02-14-2012 at 06:19 AM. Reason: solved

  2. #2
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: VBA and MSHTML, extract img ALT attribute from a link

    You could do something like this:
    Please Login or Register  to view this content.
    But note that it looks at only the first child of the link (and assumes that that child exists). You might have loop through all child elements to find the img element.

  3. #3
    Registered User
    Join Date
    02-10-2012
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: VBA and MSHTML, extract img ALT attribute from a link

    Hi Chippy, thanks.

    I've tried combing your solution with the one I'm using (not mine btw), but it didn't quite work out.. Do you think there are any alternative ways of doing that, without looping? Below is the full code from the Sub which I'm using to find anchor texts (sorry, should've pasted it here the first time). I'd like the results to be displayed in targetRange.Cells(1, 3).


    HTML Code: 
    this gets the anchor and the target url.. but I can't figure out how to get "alt"..


    Thanks again.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA and MSHTML, extract img ALT attribute from a link

    Hello guitalex ,

    The Alt property applies only to an Img object. You would need to check each anchor object for the presence of an image object as child node. If the image object exists then the alt property, if used, can be loaded into the cell (targetRange.Cells(1, 5)). Here is the modified code (untested).

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    02-10-2012
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: VBA and MSHTML, extract img ALT attribute from a link

    Hi Leith,

    That's exactly what I was looking for, thanks!

  6. #6
    Registered User
    Join Date
    02-10-2012
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: VBA and MSHTML, extract img ALT attribute from a link

    Hi Leith,


    Thanks again for your previous suggestion.
    Just one more (I thought I'll ask here instead of starting a new thread): how would I go about counting the number of occurrences of "websiteURL" on a specific page if there is more than one link containing domain2.com?


    Thank you

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA and MSHTML, extract img ALT attribute from a link

    Hello guitalex,

    I am not sure I understand the question. Is "websiteURL" a URL embedded within a link/image or simply any reference to this URL in the page source?

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA and MSHTML, extract img ALT attribute from a link

    Hello guitalex,

    I am not sure I understand the question. Is "websiteURL" a URL embedded within a link/image or simply any reference to this URL in the page source?

  9. #9
    Registered User
    Join Date
    02-10-2012
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: VBA and MSHTML, extract img ALT attribute from a link

    Hi Leith,

    "websiteURL" - it's any reference to this URL (or even a partial string) found in the page source.

    Thank you

    p.s. Regarding non-image links: after some testing I've discovered that when I'm using my original approach "... If (InStr(link.href, websiteURL) > 0) Then ... " it doesn't find some of the links for some reason.. not sure why.. I was thinking maybe there's a way of using something else instead of "> 0", like partial string match or something... couldn't come up with solution just yet..
    Last edited by guitalex; 03-23-2012 at 11:05 AM.

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA and MSHTML, extract img ALT attribute from a link

    Hello guitalex,

    Can you post the HTML page source as a text file? I can then use it to test some ideas.

  11. #11
    Registered User
    Join Date
    02-10-2012
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: VBA and MSHTML, extract img ALT attribute from a link

    Hi Leith,

    I've attached a txt with the source code.

    For example, I'm using "www.cartransport" (without the quotes) as a partial string (websiteURL) to be found in the source code, and it doesn't get detected for some reason. In fact, none of the URLs which this guy has on his homepage can be found using this method..

    Using different user agents in vba code didn't help either..

    I hope this helps..

    Thanks
    Attached Files Attached Files
    Last edited by guitalex; 03-23-2012 at 03:43 PM.

  12. #12
    Registered User
    Join Date
    09-20-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1

    Re: VBA and MSHTML, extract img ALT attribute from a link

    Superlike, Save a lot.
    Thanks for sharing such a use full function.
    How can I fetch anchor text form a given link say for an example, I have url: http://www.abc.com/2013/contetnt.html and I have to fetch all anchors which were given in a content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1