|
|
 |
| 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.
|
 |

May 13th, 2007, 08:17 PM
|
|
Registered User
|
|
Join Date: May 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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!
|

May 14th, 2007, 04:07 PM
|
|
Friend of Wrox
|
|
Join Date: Feb 2007
Location: Davenport, IA, USA.
Posts: 150
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |