[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Check path before opening workbook in excel vb

Posted on 2009-02-23
7
Medium Priority
?
428 Views
Last Modified: 2013-11-25
I have the following code  below which opens a workbook in excel, however, i was wondering if there was a way to check if the path is valid before trying to open it up and getting an error. I don't want to use an err.number solution. I also put a pic of the error that shows when it tries to open a workbook that doesn't exist.

Workbooks.Open D.[B2].Text, never
Delete1.pdf
0
Comment
Question by:tchristie33
6 Comments
 
LVL 4

Accepted Solution

by:
davidsperling earned 2000 total points
ID: 23710997
Use FileSystemObject!

Google or F1 for info...


Sub Test
   'code probably erranous...
   dim fs as new FilesystemObject
   
   if fs.FolderExists("c:\My\Folder") then
      msgbox "Exists!"
   else
      msgbox "Exists - not!"
   end if
 
end sub

Open in new window

0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23711056
whay you don't want to use an err.number solution ?
0
 
LVL 4

Expert Comment

by:davidsperling
ID: 23711111
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tchristie33
ID: 23711115
i just don't like to code that way i guess, figure there has to be a better way to do it.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 23711149
If Dir$("C:\MyPath\MyFile.Xls") <> "" then
   Set Wbk = WorkBooks.Open("C:\MyPath\MyFile.Xls")
0
 
LVL 4

Expert Comment

by:davidsperling
ID: 23711189
The code i sent will not work in VBScript. Please see

http://msdn.microsoft.com/en-us/library/z9ty6h50(VS.85).aspx

for how to use FileSystemObject in VBS.

(in vbs you gotta use CreateObject() )
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question