Solved

VB5 and Excel

Posted on 1999-01-22
3
295 Views
Last Modified: 2010-07-27
I have created an excel object from VB.  I write data to the spreadsheet object and before I close the app I would like to save the workbook.  I am using the commondialog control to set the filename and path and then using workbook.saveas(commondialog.filename).  The problem is if the filename I am using already exists in the path that I have selected then excel brings up the dialog that asks if you want to save over the existing file. If you answer NO then I get a runtime error 1004 "method saveas of object workbook failed".  

Can anyone give me a solution or a way to check if the file exists before the workbook.saveas call?
0
Comment
Question by:gemme
  • 2
3 Comments
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 100 total points
ID: 1470180
You can use the Dir$ function to check if the file already exists or use the Kill function to destroy it.

Maybe a good solution would be to destroy (without checking if the file already exists) and then save your workbook:

on error resume next
kill "c:\file.xls"
on error goto 0
workbook.saveas "c:\file.xls"
 
0
 

Author Comment

by:gemme
ID: 1470181
is there a way to do this from the excel dialogs?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 1470182
You can use the same code in an Excel module but not in dialogs!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel object stays open 19 71
VB error "Type mismatch" 2 50
vb6 connector to mongodb 2 50
How to measure sizes and angles in scanned images ? 3 53
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now