p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > Microsoft Office > Excel VBA > Excel VBA
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Excel VBA Discuss using VBA for Excel programming.

Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 13th, 2007, 08:17 PM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Handling Non-Existing Web Files with Workbook.Open

I'm running some VBA to D/L some files (.csv files) from a web page/site.  Works fine, except when the file doesn't exist.  Thing is, I don't know if the file exists or not when I attempt to D/L it.

I'm using Workbook.open ("www.thewebpage.com/fileX.csv")...

which works fine until it hits the case where it can't find the file.  Excel then pops a message saying effectively "Can't find the file dummy!"  Of course, I want to automate this, as there's lot's of files to D/L.  When this error occurs, how can I deal with it such that the VBA handles it and goes on to the next filename (that does exist) smoothly?

Thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old May 14th, 2007, 04:07 PM
Friend of Wrox
Points: 474, Level: 7
Points: 474, Level: 7 Points: 474, Level: 7 Points: 474, Level: 7
Activity: 2%
Activity: 2% Activity: 2% Activity: 2%
 
Join Date: Feb 2007
Location: Davenport, IA, USA.
Posts: 150
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Handle the opening of the file in a different routine.

Example:
------------------------------------------------------------------
Private Sub YourSub

  Dim oSource As Worksheet, sFile As String, sSource_Path As String, bHave_File As Boolean
  'All your previous code prior here
  Do While YourCriteria = True 'Whatever you are using to loop through each one.
    bHave_File = TryOpen(sSourcePath & "/" & sFile) 'Assumes that sSourcePath doesn't end with a '/' already.
    If bHave_File Then
      Set oSource = Workbooks(sFile).Activesheet
      'Your processing of open workbook
      Workbooks(sFile).Close 'Add any appropriate switches desired, I'd suggest explicitly telling it not to save
      sFile = NextFileName 'Whatever method you're determining next file name by
    End If
  Loop
  'Any other out of loop processing

End Sub

Private Function TryOpen(sPassed As String) As Boolean

'Tries to open file, returning true if successful
  On Error Goto TryOpenFailed
    Workbook.Open Filename:=sPassed
  On Error Goto 0
  TryOpen = True
TryOpenFailed:

End Function
------------------------------------------------------------------

Hope this helps.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Workbook Open anup.bihani Excel VBA 2 November 30th, 2006 01:05 AM
UserForm1.show fails if another workbook is open gjelson VB How-To 3 June 27th, 2006 03:58 PM
Open workbook using macro rekha_jsr BOOK: Excel 2000/2002 VBA Programmer's Reference 2 April 19th, 2006 06:40 AM
Error when open workbook gastoncs Excel VBA 2 December 21st, 2005 12:43 PM
Save the Existing Workbook to Other Filename tks_muthu Excel VBA 1 November 29th, 2004 10:41 AM



All times are GMT -4. The time now is 02:58 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc