Question

VBA to open web page, find text, and click on text's URL link

Asked by: cstraim

I am trying to have VBA open an instance of IE and browse to a specific
URL


Private Sub DrawingIn_Click()
   Set browser = CreateObject("InternetExplorer.Application")
   browser.Navigate ("www.mywebpage.com")
   browser.StatusBar = False
   browser.Toolbar = False
   browser.Visible = True
   browser.Resizable = False
   browser.AddressBar = False
End Sub


I got that portion to work


Now I need to search for a hypertext link named "spreadsheet" within
the page i've navigated to. The hyperlink is to download a spreadsheet.
 Clicking on it brings up the SaveAS dialog box.  I then need to save
the spreadsheet to a specific location.


Can someone please help me complete the code to


1)  Find the Spreadsheet hyperlink
2)  Click it
3) Save the target into a specified directory.


Thanks again


Cory

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2006-11-01 at 14:03:14ID22045830
Tags

vba

,

web

,

open

,

page

Topic

VB Controls

Participating Experts
3
Points
500
Comments
24

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Checking existence of hyperlink from VBA
    Is there a way to check if a url exist in Excel VBA. I'm maintaining a website which includes a number of hyperlinks. The hyperlinks are saved in an Excel workbook where a VBA macro creates the HTML-code. At this time I (ought to) control the existence of the url-adresses ma...
  2. VBA SaveAs and FolderDialog control
    I am trying to make a simple VBA macro to achieve a simple repeatative task. We save email messages to a network share for later use or reference. The format of the email is title-date.msg and place in a particualar directory for each project. I am not able to figure out how ...
  3. Create a hyperlink with VBA
    I have a VBA function that creates text inside a range of cells. How do I tell "on the fly" that the text going into that cell should be a hyperlink? Id like to be able to set the destination URL and the display text. Thanks!
  4. Files saveas different filetype of Excel within Access - usi…
    I have an Excel file (.csv) that is generated by a mainframe database - that saves the file as an older file type. I need to be able to accomplish a filesaveas to update the file type to Excel 2003(.xls). I would like to accomplish this thru vba with Access, because I need ...
  5. saveas wbesite html page into our PC at  VBA excel
    Dear Expert, I would like to save the wbesite as html file and save it in my PC .How Can I can do that after the following code sub, "savewebsite". That is same operation when we save the website in IE window to our local PC dir and file but now it is saving it at...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: vb_elmarPosted on 2006-11-01 at 15:31:50ID: 17854176

This sample searches for the link "S-Grid Documentation.zip (5K)". It will be found
in the window no. 25

The link will be clicked automatically, and the download starts.

Needs 1 Form and 1 Button :
===================

Private Sub Command1_Click()

    Dim o2
    Set o2 = CreateObject("internetexplorer.application")
        o2.navigate "http://www.vbaccelerator.com/home/vb/code/Controls/S_Grid/S-Grid_Documentation.asp"
        o2.Visible = True
        While o2.busy: DoEvents: Wend
     
AppActivate Me.Caption
Set o = o2.Document.All.tags("A")


M = o.length: mySubmit = -1
For r = 0 To M - 1: zz = ""

zz = zz & "Link Index : " & r & " of " & o.length - 1
zz = zz & String(3, vbCrLf)
'
zz = zz & "A . tabindex : " & o.Item(r).TabIndex
zz = zz & String(3, vbCrLf)
'
zz = zz & "A . tagname : " & o.Item(r).tagname
zz = zz & String(3, vbCrLf)
'
zz = zz & "A . href : " & o.Item(r).href
zz = zz & String(3, vbCrLf)
'
zz = zz & "A . type : " & o.Item(r).Type
zz = zz & String(3, vbCrLf)
'
zz = zz & "A . name : " & o.Item(r).Name
zz = zz & String(3, vbCrLf)
'
zz = zz & "A . innerhtml : " & o.Item(r).innerhtml
zz = zz & String(3, vbCrLf)
'
zz = zz & "A . outerhtml : " & o.Item(r).outerhtml
zz = zz & String(3, vbCrLf)
'
zz = zz & "A . rel : " & o.Item(r).rel
zz = zz & String(3, vbCrLf)
'
zz = zz & "A . rev : " & o.Item(r).rev
zz = zz & String(3, vbCrLf)
'
zz = zz & "A . id : " & o.Item(r).Id
zz = zz & String(3, vbCrLf)


MsgBox zz
If InStr(1, o.Item(r).innerhtml, "S-Grid Documentation.zip (5K)", vbTextCompare) Then
    MsgBox "= F O U N D =" & vbCrLf & o.Item(r).innerhtml
    o.Item(r).Click: Exit For
End If
Next


'        o2.Quit
'        Set o2 = Nothing
End Sub

 

by: cstraimPosted on 2006-11-01 at 16:22:07ID: 17854519

Thank you very much for your help.  I will try it tomorrow when i get into the office. This code will help me out so much as this process has to be done on 50 different navigated pages.  I will post the results as soon as it's done

Thanks again

 

by: cstraimPosted on 2006-11-01 at 16:55:16ID: 17854635

I had to try the code just by itself tonight.  It works like a charm.  Thanks again.  

I really appreciate how you helped me to see the steps the code took to get to the end result.  Is there a way to automatically have the file that is downloaded save as a certain name in a certain directory?  For testing puroses, I would just save it as c:\test.zip.  I also realized that when saving it will be overwriting the existing file c:\test.zip in the file, can vba code say yes to overwrite the existing file as well, if so, can you include that in the code as well.

Thanks again, I am buying a vba for beginners book this week off of safari, so I can have a basic understanding of how it comes together.

Cory

 

by: vb_elmarPosted on 2006-11-01 at 23:45:18ID: 17856189

You can save it with another file name, eg. "c:\test.zip".

-It is also possible to overwrite the existing file c:\test.zip if
the file already exists. - Try the following sample :




Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Dim o2
    Set o2 = CreateObject("internetexplorer.application")
        o2.navigate "http://www.vbaccelerator.com/home/vb/code/Controls/S_Grid/S-Grid_Documentation.asp"
        o2.Visible = True
        While o2.busy: DoEvents: Wend
     
Set o = o2.Document.All.tags("A")


M = o.length: mySubmit = -1
For r = 0 To M - 1

If InStr(1, o.Item(r).innerhtml, "S-Grid Documentation.zip (5K)", vbTextCompare) Then
    'MsgBox "= F O U N D =" & vbCrLf & o.Item(r).innerhtml
    o.Item(r).focus
    o.Item(r).Click: Exit For
End If
Next

mySleep 50 'wait five seconds
SendKeys "{enter}"
Clipboard.Clear
Clipboard.SetText "c:\test.zip"

mySleep 30 'insert new name
SendKeys "^v"

mySleep 10 'tab to go to the ok button
SendKeys "{tab}"

mySleep 10 'tab to go to the ok button
SendKeys "{tab}"

mySleep 30'press the 'save' button
SendKeys "{enter}"

mySleep 20 'Msgbox appears: 'file exists - do you want to overwrite'
SendKeys "{tab}", True 'because default is 'no' we need to send 1 tab

mySleep 20
SendKeys "{enter}", True

AppActivate Me.Caption
MsgBox "file saved"

'        o2.Quit
'        Set o2 = Nothing
End Sub



Sub mySleep(ByVal deciSeconds As Long)
    For r = 1 To deciSeconds: Sleep 100: DoEvents: Next
End Sub


 

by: cstraimPosted on 2006-11-02 at 06:31:56ID: 17858162

For some reason i cannot call the "sleep" function. I realize that it is a windows API call, but I don't have it installed in my reference library.  Do you know which reference library it is located in. I am using VBA for office 2003

 

by: cstraimPosted on 2006-11-02 at 06:34:53ID: 17858193

The actual error message I get when i run the code is

"complile error"
Sub or Function not defined.  It then Highlights Sleep.  I looked for it in the object broswer and didnt see it

 

by: vb_elmarPosted on 2006-11-02 at 06:56:37ID: 17858365

I think the sleep function is part of the WSH. Can you add a reference to 'Windows Script Host' ?

 

by: cstraimPosted on 2006-11-02 at 07:01:45ID: 17858432

Sorry About this I know why i got the sleep function error.  I was messing around with the code sample you wrote.  So now I am trying to figure out how to get it to work

I noticed that the second line Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) is made to attach this to a button on a form (at least I think it is)  So I changed it to

Private Sub Command1_MouseDown () so that I can just run it for testing.

Once I am able to run it I get the following error:

"Compile Error" Invalid use of Me Keyword.  It highlights AppActivate.Me.Caption.  I am not sure what this means

 

by: vb_elmarPosted on 2006-11-02 at 07:02:11ID: 17858435

Five minutes ago I read in the internet that neither VBScript nor JavaScript
provides a SLEEP function, and we must use a While Not loop to perform our count.

Sub CountDown
Dim i, time, interval

    time = 5000
    interval = 1
    time = interval * time
      While Not (time = 0)
         time = time - 1
      Wend
Msgbox "Hello"
End Sub

 

by: vb_elmarPosted on 2006-11-02 at 07:06:59ID: 17858474

>>I get the following error:
>>"Compile Error" Invalid use of Me Keyword.  It highlights AppActivate.Me.Caption.

AppActivate.Me.Caption gives the visible focus back to your VB application.

I think we can comment out this "AppActivate.Me.Caption" line.

The only disatvantage will be that the messagebox will appear 'behind' the visible IExplorer window
instead in front of it.

 

by: cstraimPosted on 2006-11-02 at 07:11:20ID: 17858519

If I delete the Appactivate.Me.Caption It continues, however I get a run time error for clipboard Clear

 

by: vb_elmarPosted on 2006-11-02 at 07:11:44ID: 17858526

I progged another mySleep function. It doesn't use the sleep function.




Sub mySleep(ByVal deciSeconds As Long)
    t = Timer: While Timer - t < (deciSeconds / 10): DoEvents: Wend
End Sub

 

by: cstraimPosted on 2006-11-02 at 07:15:08ID: 17858562

I changed the sleep function...but the clipcoard.clear is causing a runtime error for some reason

 

by: vb_elmarPosted on 2006-11-02 at 07:19:49ID: 17858593

>>the clipcoard.clear is causing a runtime error

I progged all the code in VB6. If we can't get the "clipboard.Clear"
method running, i must see what method we can use instead.

-I need 3 days to check it out.

 

by: vb_elmarPosted on 2006-11-02 at 07:35:09ID: 17858729

I've found this to clear the clipboard :

   ' Clear the Clipboard.
   X = EmptyClipboard()

 

by: vb_elmarPosted on 2006-11-02 at 07:49:12ID: 17858876

OpenClipboard()

EmptyClipboard()

SetClipboardText("C:\test.zip")

CloseClipboard()

 

by: cstraimPosted on 2006-11-02 at 07:52:11ID: 17858899

I tried that and it didn't work.  i did find this though

http://www.cpearson.com/excel/clipboar.htm

I'm trying to integrate it into the code as best I can lol.  Wish i could touch the VBA book and just understand it in 10 minutes or less.  LOL.  

Thanks again for all of your help I really appreciate it :)

 

by: cstraimPosted on 2006-11-02 at 07:52:52ID: 17858908

oh waith i didnt try the comment you just typed..let me do that and I'll get back to it :)

 

by: cstraimPosted on 2006-11-02 at 07:55:46ID: 17858947

Ok I did this

mySleep 50 'wait five seconds
SendKeys "{enter}"
OpenClipboard()

EmptyClipboard()

SetClipboardText ("C:\test.zip")

CloseClipboard()
mySleep 30 'insert new name
SendKeys "^v"

I just copied what had in place of

Clipboard.Clear
Clipboard.SetText "c:\test.zip"

it didnt work.  So I'm also going to look at

http://www.cpearson.com/excel/clipboar.htm

Do you see anything in there that could be of use.

 

by: vb_elmarPosted on 2006-11-02 at 08:12:14ID: 17859124

instead ...

Clipboard.Clear
Clipboard.SetText "c:\test.zip"
mySleep 30 'insert new name
SendKeys "^v"


... you can use the following :

SendKeys "c"
SendKeys ":"
SendKeys "\"
SendKeys "t"
SendKeys "e"
SendKeys "s"
SendKeys "t"
SendKeys "."
SendKeys "z"
SendKeys "i"
SendKeys "p"


the new code is :
*******************************************
mySleep 50 'wait five seconds
SendKeys "{enter}"

mySleep 30 'waiting 3 seconds before inserting new name
SendKeys "c"
SendKeys ":"
SendKeys "\"
SendKeys "t"
SendKeys "e"
SendKeys "s"
SendKeys "t"
SendKeys "."
SendKeys "z"
SendKeys "i"
SendKeys "p"




mySleep 10 'tab to go to the ok button
SendKeys "{tab}"

mySleep 10 'tab to go to the ok button
SendKeys "{tab}"

mySleep 30'press the 'save' button
SendKeys "{enter}"

mySleep 20 'Msgbox appears: 'file exists - do you want to overwrite'
SendKeys "{tab}", True 'because default is 'no' we need to send 1 tab

mySleep 20
SendKeys "{enter}", True

'''AppActivate Me.Caption
MsgBox "file saved"

 

by: cstraimPosted on 2006-11-02 at 11:12:40ID: 17860618

Thanks man...works like a charm.  I've re-set the code so that it works for the application.  You are awesome.  Instead of tabs and enter, I just used ALT menu keys

Private Sub Command1_MouseDown() '(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Dim o2
    Set o2 = CreateObject("internetexplorer.application")
        o2.navigate "http://www.vbaccelerator.com/home/vb/code/Controls/S_Grid/S-Grid_Documentation.asp" 'IE navigates to a webpage
        o2.Visible = False 'hides IE
        While o2.busy: DoEvents: Wend
     
Set o = o2.Document.All.tags("A")


M = o.length: mySubmit = -1
For r = 0 To M - 1

If InStr(1, o.Item(r).innerhtml, "S-Grid Documentation.zip (5K)", vbTextCompare) Then
    o.Item(r).focus
    o.Item(r).Click: Exit For
End If
Next

mySleep 50 'wait five seconds
SendKeys "%S" 'Hits the save button
mySleep 50 'wait five seconds and starts typing in the path c:\test.zip to save the file
SendKeys "c"
SendKeys ":"
SendKeys "\"
SendKeys "t"
SendKeys "e"
SendKeys "s"
SendKeys "t"
SendKeys "."
SendKeys "z"
SendKeys "i"
SendKeys "p"

mySleep 10 'waits 1 second
SendKeys "%S" 'Hits the save button
SendKeys "%c" 'Hits the Close this dialog box when download completes

mySleep 20 'Waits 2 seconds and Msgbox appears: 'file exists - do you want to overwrite'
SendKeys "%y"

o2.Quit
Set o2 = Nothing
End Sub

Sub mySleep(ByVal deciSeconds As Long)
    t = Timer: While Timer - t < (deciSeconds / 10): DoEvents: Wend
End Sub

 

by: vb_elmarPosted on 2006-11-02 at 11:26:33ID: 17860755

Thanks - i thought the alt-C (alt-S) combinations are best but
I didn't suggest these combinations because my IExplorer is in german
language and we have alt-S instead of alt-C .

 

by: piyushdabombPosted on 2007-11-14 at 16:12:01ID: 20285281

Wow....Looks like this seems ed to work for the Guru's here. I have a somewhat similar question but a different approach. Please take a look at my question and help me out. I've worked days on this and I think I'm going to turn sick.

http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_22961749.html

 

by: Hari1612Posted on 2008-10-06 at 04:54:00ID: 22649028

Is it possible to get the tags from a frame inside main page. If i try to access any element within fram , i get a error "Access denied"

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...