473,785 Members | 3,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Excel File Doesn't Exist

57 New Member
Hello all,
I am new at VB6, but the more i learn, the more i love it! But i do have a question. I've created a form, the user has to choose an excel file from a list that they want to open, when they highlight the file name, the directory path shows up in another label box; then the user clicks the Open button to actually open the excel file for viewing. But what if the file doesn't exist yet? How do i add to my IF/Then statement a controlled message that tells the user that the file doesn't exist and to go back and select another file? Here is the code that i have so far:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2. Dim xl
  3. Dim xlsheet
  4. Dim xlwbook
  5. Dim MyFileName As String
  6.  
  7. Me.Label3.ShowWhatsThis
  8. MyFileName = Me.Label3.Caption
  9.  
  10. If Me.Label3.Caption <> "" Or Me.Label3.Caption <> ".xls" Then
  11.  
  12. Set xl = CreateObject("Excel.Application")
  13. Set xlwbook = xl.Workbooks.Open(MyFileName)
  14. Set xlsheet = xlwbook.Sheets.Item(1)
  15.  
  16. xl.Visible = True
  17.  
  18. End If
  19.  
  20. End Sub
  21.  
  22. Private Sub Command2_Click()
  23. Unload Form3
  24. End Sub
  25.  
  26. Private Sub Form_Load()
  27. List1.AddItem " BIO Only"
  28. List1.AddItem " CHEM Only"
  29. List1.AddItem " NUC Only"
  30. List1.AddItem " ROTA Only"
  31. End Sub
  32.  
  33. Private Sub List1_Click()
  34. Label3.Caption = List1.Text
  35. Select Case List1.ListIndex
  36. Case 0
  37.     Label3.Caption = "C:\file1.xls"
  38. Case 1
  39.     Label3.Caption = "C:\file2.xls"
  40. Case 2
  41.     Label3.Caption = "C:\file3.xls"
  42. Case 3
  43.     Label3.Caption = "C:\file4.xls"
  44. End Select
  45. End Sub
Thanks,
Terra
Apr 3 '08 #1
2 1409
kadghar
1,295 Recognized Expert Top Contributor
Hello all,
I am new at VB6, but the more i learn, the more i love it! But i do have a question. I've created a form, the user has to choose an excel file from a list that they want to open, when they highlight the file name, the directory path shows up in another label box; then the user clicks the Open button to actually open the excel file for viewing. But what if the file doesn't exist yet? How do i add to my IF/Then statement a controlled message that tells the user that the file doesn't exist and to go back and select another file? Here is the code that i have so far:
Thanks,
Terra
i only gave your code a fast glimpse.

Why dont you try with an error handler?
e.g.
Expand|Select|Wrap|Line Numbers
  1. Sub MySub()
  2. On error goto ErrHandler
  3. 'your code
  4. exit sub
  5. ErrHandler:
  6. msgbox "An error ocurried"
  7. end sub
You can also check the Err object as well as it properties (number, description, etc) so you can give a "profession al look" to your error handler.
Also try with another goto, or with a loop to retry loading a file if it wasnt found the first time.

HTH
Apr 3 '08 #2
Perl Beginner
57 New Member
Thank you so much HTH!! The error handler works great!
Terra
Apr 3 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
12671
by: jim | last post by:
I'm trying to stream a html page to the user as an excel file. I'm currently using Response.ContentType = "application/vnd.ms-excel" Response.AddHeader "Content-Disposition", "inline; filename=download.xls " The actual streaming and excel file are working fine but in excel its not picking up the filename its actually showing up as the url to the page that downloads this. I
1
363
by: David Krmpotic | last post by:
hi!! I use the following code to operate excel document: Excel.Application excelApp = new Excel.ApplicationClass(); //excelApp.Visible = true; string workbookPath = "c:\\eltina.xls"; Excel.Workbook excelWorkbook = excelApp.Workbooks.Open("eltina.xls",0,false,5,"","",false,Excel.XlPlatform.
3
11932
by: John | last post by:
Is there a way to code the button that's available in the query window--microsoft excel icon that exports to excel. I know transferspreadsheet will do this---but I want the query, which is in a pivot table view, to be exported as a pivot, not just a data list. Does this code exist? Is it transferspreadsheet but with a twist?
2
6376
by: Alvin Bruney | last post by:
anybody know how to launch desktop excel from a webpage and feed it either a dataset or a file? i've got MS sample code but it aint compiling with the office 10 library. i wonder if they changed the libraries cuz i used object browser and the interface they are calling doesn't exist in the class. I'm not looking for an OWC solution either
6
18573
by: Sam Johnson | last post by:
HI I tried to send the following SQL string to an open databse, to export a table into excel format: g.Connection = conn 'valid OleDBConnection and Command objects g.CommandText = "SELECT * INTO XLSTest IN 'C:\' 'Excel 8.0;' FROM Table1" g.ExecuteNonQuery()
10
11829
by: Niklas | last post by:
Hi Before I start coding I need to be sure that nobody else has not done it yet and that I can use it. I need an import utility which import data from Excel to a database or some object in .NET. The data in Excel is not in structured columns but can exist everywhere in the workbook. For example if I am supposed to import a person and all his/her cars which exist in a workbook I want to be able to create an import protocol and specify...
10
8205
by: Steve | last post by:
I am trying to create a DLL in Visual Studio 2005-Visual Basic that contains custom functions. I believe I need to use COM interop to allow VBA code in Excel 2002 to access it. I've studied everything I can find on COM Interop and .NET. I've also tried many of the 'Walkthroughs' on the MSDN site relating to COM add-ins, .NET and Office XP but am unable to get even these working in Excel 2002 or Word 2002. I've installed the Office XP...
16
2705
by: alexia.bee | last post by:
Hi all, In some weird reason, excel instance won;t die if i remove the comment from 4 lines of setting values into struct. here is a snipcode public System.Collections.Generic.List<frmMain.sDBTest> LoadTestSet(string TestSetFile, System.Collections.Generic.List<frmMain.sDBTestDBviewList)
15
16190
by: OfficeDummy | last post by:
Hello all! I searched the Internet and this forum for a similar problem, but I found no help... Also, I'm a complete newbie to the fascinating world of programming, VBA and Access, so my question can very well be very stupid. The tasks are: 1)Import an Excel table into Access 2)Add a new column and fill it with variables of date/time type. Steps 1 and 2 need to be done only once, and I've almost managed to accomplish them. Now...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10327
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10151
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.