Finding files and saving

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am new to VB.Net(2003) and am having some problems. I am running Office 2003 with VB.Net 2003 and currently working through the VB.Net Step by Step book to become familiar with the program. The problem I am having is that when I try to open an existing file using Word, Excel, Access or PPT,I always get an error message saying the file could not be found. Same problem when I try to save a file made with the worksheet builder(xlsheet.saveas("C:\myfolder\mytestsheet.xls"). I have checked into file permissiond and the like and have read every posting possible to try and figure out why none of my office programs can find any existing files. Please help!
 
Greg:

I'm not sure what the particular problem is, but before each of the Open
statements insert this line:

Debug.Assert(File.Exists("C:\myfolder\mytestsheet.xls")). If the file
doesn't exists, then the assertion should fail and you'll know the problem
resides in the locatio nof the file. In general, you should check for the
existence of a file with If File.Exists() for instance before trying to opne
it. Similarly, b/c file opening can fail through things totally not your
fault, you'll probably want to wrap Opens in a try catch and respond if it's
a failure. If you are getting file not found, I would think you have a typo
in the file name or something is incorrect, but the assertions will tell you
that. If it was a permission error, you'd get a different exception.

Also, can you post the code? It'd help see what's going on, but first,
insert those assertions (replacing file name with the variable or string
literal you are currently using for each file open) and see if you get any
assertion failures.

Cheers,

Bill
Greg said:
I am new to VB.Net(2003) and am having some problems. I am running Office
2003 with VB.Net 2003 and currently working through the VB.Net Step by Step
book to become familiar with the program. The problem I am having is that
when I try to open an existing file using Word, Excel, Access or PPT,I
always get an error message saying the file could not be found. Same problem
when I try to save a file made with the worksheet
builder(xlsheet.saveas("C:\myfolder\mytestsheet.xls"). I have checked into
file permissiond and the like and have read every posting possible to try
and figure out why none of my office programs can find any existing files.
Please help!
 
Here is the code I am trying to use
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Clic
Dim xlApp As Excel.Applicatio
Dim xlBook As Excel.Workboo
Dim xlsheet As Excel.Workshee
xlApp = CType(CreateObject("Excel.Application"), Excel.Application
xlBook = CType(xlApp.Workbooks.Add, Excel.Workbook
xlsheet = CType(xlBook.Worksheets(1), Excel.Worksheet

xlsheet.Cells(1, 2) = 500
xlsheet.Cells(2, 2) = 7
xlsheet.Cells(3, 1) = "Total
xlsheet.Range("B3").Formula = "=sum(R1C2:R2C2)
xlsheet.Range("B3").Font.Bold = Tru
xlsheet.Application.Visible = Tru
xlsheet.SaveAs("C:\mytestapps\mytestsheet.xls"

I have also tried using System.Diagnostics.process.start("excel.exe",("C:\mydocuments\myfile"
This will open Excel, Word or Access, but they can never find the files. I tried using the debug.assert but it had a build error from file.exists(file had the jagged underline saying it was not declared).
 
Back
Top