Saving files with visual basic
Saving files with visual basic
(OP)
I have a macro in excel that is used to save the excel file to my a: drive when a certain sequence of events occur. My problem is that if there is no floppy in the drive i get an error message that allows me to retry or cancel. If i cancel it returns me to the debug screen.
Is there any way i can either check for the prescence of a floppy or capture the error so i exit the macro without entering the debugger?
Thanks for any help.
Is there any way i can either check for the prescence of a floppy or capture the error so i exit the macro without entering the debugger?
Thanks for any help.





RE: Saving files with visual basic
you should study the 'ON ERROR' help. One can trap any
error and process it as you like with error trapping provided in VB and VBA.
RE: Saving files with visual basic
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo errHandler
Dir "a:\."
ThisWorkbook.SaveAs "A:\book1.xls"
Exit Sub
errHandler:
If Err.Number = 52 Then
Dim tmp As Integer, message As String
message = "There is no disk in drive 'A'. " _
& "Do you want to try again?"
tmp = MsgBox(message, vbYesNo, "Drive Error")
If tmp = vbYes Then Resume
End If
End Sub
RE: Saving files with visual basic
Just one thing though when i ran that code I had to change the error number from 52 to 71 to get it to run properly.
RE: Saving files with visual basic
Private Sub Worksheet_Activate()
On Error GoTo errHandler
Open "A:\excel.txt" For Output As #1
Print #1, "Hello from Excel"
Close #1
Exit Sub
errHandler:
If Err.Number = 71 Then
Dim tmp As Integer, message As String
message = "There is no disk in drive 'A'. " _
& "Do you want to try again?"
tmp = MsgBox(message, vbYesNo, "Drive Error")
If tmp = vbYes Then Resume
End If
End Sub