+ Reply to Thread
Results 1 to 9 of 9

Search website and Pull URL of an Image

  1. #1
    Registered User
    Join Date
    05-01-2009
    Location
    Seattle
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    21

    Search website and Pull URL of an Image

    Hi all I have a question maybe someone can help me with.

    WHAT I'M TRYING TO DO: I am writing an excel program, this program automatically opens up a browser, navigates to a website, then searches the content of the website to find the URL of a specific picture, and the URL to which the picture links to. The URL for both the picture and the link contain a specific string which is defined as a variable from the excel sheet.

    EXAMPLE: The program opens up IE, IE automatically navigates to www.pencil.com (made up website). The website displays the different pencils they sell, similar to this set up from newgg.com:http://www.newegg.com/store/category...19&Tpk=monitorThe program then searches the different images for an instant of a string that was defined in the excelsheet, for example ultragrippencil. When it finds an instance of this name in the image URL and the link URL, it will copy both and paste them back into excel.

    Any help or advice is much appreciated! Thanks in advance

    odo-
    Last edited by odo; 05-28-2009 at 03:30 AM.

  2. #2
    Registered User
    Join Date
    05-01-2009
    Location
    Seattle
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    21

    Re: Search website and Pull URL of an Image

    This is the HTML code from the website

    Please Login or Register  to view this content.
    My question is how can I pull the Image URL (www.google.com/biggoogle.jpg), and the Link URL (www.google.com) from the HTML using VBA and excel? All links and URL's have been replaced with made up ones.

    odo-
    Last edited by odo; 05-21-2009 at 01:54 PM.

  3. #3
    Registered User
    Join Date
    05-01-2009
    Location
    Seattle
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    21

    Re: Search website and Pull URL of an Image

    bump~ Is this even possible to do in VBA?

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

    Re: Search website and Pull URL of an Image

    Based on your example HTML code and description, I wrote this.
    Please Login or Register  to view this content.
    Use the Test subroutine to, errr, test it with your web site URL and image search string. The sImageSearchString parameter is just a text string, which can be picked up from anywhere, including a cell, like this:

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    05-01-2009
    Location
    Seattle
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    21

    Re: Search website and Pull URL of an Image

    Chippy,

    Thank you so much! I'll play around with the code to see how it works~

    Appreciate it~!

    odo-

  6. #6
    Registered User
    Join Date
    05-01-2009
    Location
    Seattle
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    21

    Re: Search website and Pull URL of an Image

    Chippy,

    Again thank you for your help! Your code helped me out a lot~ I have a question though:

    The reference you had as a URL (highlighted in green and in bold) is actually a global string variable defined from another private sub procedure in my program. I tried to change this to the variable's name, as well as change all references to it (highlighted in purple) in the code but I still get a "Byref argument mismatch". A simple fix would be to make the sub procedure paste the URL into a cell, and have the Find_Matching_Images procedure reference that cell.. but I'm just curious is there a better and more professional way to do this?

    Please Login or Register  to view this content.
    This is an example of my setup:

    Please Login or Register  to view this content.
    Again thanks for your help!

    odo-
    Last edited by odo; 05-26-2009 at 04:47 AM.

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

    Re: Search website and Pull URL of an Image

    Your modified version ran successfully on my computer; I didn't get the "Byref argument mismatch" error.

    As far as I can see, all you've done in Find_Matching_Images() is change the variable name of the first parameter from sWebSiteURL to x, and the one line that references it, IE.navigate x.

    Within Find_Matching_Images(), 'x' is still a local variable (because it's declared in the parameter list), not the global 'x' you've defined, so your modified Find_Matching_Images() is effectively the same as mine. Your global 'x' is only global within Define_Variables() and Test(), and Test() is passing the global value of 'x' to the local variable 'x' in Find_Matching_Images(). Do you see how using the same variable name for a global and local variable is confusing?!

    If you want to use your global 'x' string within Find_Matching_Images(), you would change the sub parameter definition to:

    Private Sub Find_Matching_Images(sImageSearchString As String, destinationStartCell As Range)

    BUT, global variables are generally bad programming and should only be used when absolutely necessary. In this case I can't see the point of using a global.

    Instead, you could put the web site URL in a cell, for example A1 in Sheet1, and call Find_Matching_Images() like this:

    Please Login or Register  to view this content.
    The search word could also be picked up from a cell. I gave a clue to this method in the last sentence of my first post.

    If you are calling Find_Matching_Images() in a loop with many different web sites and/or image search strings and performance is an issue, one change I would make to my original code is to move the declaration/definition of the IE object outside Find_Matching_Images() and pass it as a parameter instead, like this:
    Please Login or Register  to view this content.
    With this the IE object is created and destroyed only once, resulting in a faster run time.
    Last edited by Chippy; 05-26-2009 at 07:53 AM.

  8. #8
    Registered User
    Join Date
    05-01-2009
    Location
    Seattle
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    21

    Re: Search website and Pull URL of an Image

    Chippy,

    I want to thank you again for all your help. I took your advice and got rid of my global variables and instead defined cell values in a new worksheet as the variables. Whenever I needed to use the variable I just referenced that specific cell. When I compiled all the different parts of my program together, this made everything flow flawlessly.

    I'm not really pro status with VBA, and everyone has been really patient! This forum is awesome!

    odo-

  9. #9
    Registered User
    Join Date
    10-30-2013
    Location
    Englewood, New Jersey
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    2

    Re: Search website and Pull URL of an Image

    Hey guys would this be possible to have it search on Google and have it return a link or links of images with the search string? I am just getting int VBA and this seems interesting.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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