You should call Dir without arguments within the loop:
If nReply = “y” Then
MyFile = Dir(“a:*.*”)
Do While MyFile “”
Kill “a:” & MyFile
MyFile = Dir
Loop
End If
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » -Path Access Error (VBA/Word 2003)
I need to add something to this macro so that I will not get a Path/File Access Error at the line — Kill “a:” & MyFile — when there is a read-only file on the diskette.
Can someone tell me how to do that?
********************
Sub SaveToDiskette()
FileName$ = ActiveDocument.Name
MsgBox “Please label a diskette and insert it into the A drive.” & vbCr & vbCr & “FYI: The chapter file will be closed after it has been saved to the diskette.”, vbOKOnly, “Insert Diskette”
Dim nReply As String
nReply = InputBox(“Do you want to delete any files that may be on this diskette BEFORE you save the chapter file to it (y or n)?” & vbCr & vbCr & “You should clean the diskette if this is the only file, or the first in a batch of files, that you want to save to this diskette.”, “Clean Diskette?”, “n”)
If nReply = “y” Then
Dim MyFile As String
MyFile = Dir$(“a:*.*”)
Do While MyFile “”
Kill “a:” & MyFile
MyFile = Dir$(“a:*.*”)
Loop
End If
ChangeFileOpenDirectory “A:”
ActiveDocument.SaveAs FileName:=FileName$, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:=””, AddToRecentFiles:= _
True, WritePassword:=””, ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
ChangeFileOpenDirectory “G:ProjectsDocs”
MsgBox “Remove the diskette from the A drive after the chapter file has closed.” & vbCr & vbCr & “Your chapter file is being sent to your network printer.”, vbOKOnly, “Remove Diskette”
End Sub
*************************
Thanks,
Jerry
No, I replied to the original question. Try this version, I have added an error handler:
Sub SaveToDiskette()
Dim FileName As String
Dim nReply As Integer
Dim MyFile As String
On Error GoTo ErrHandler
FileName = ActiveDocument.Name
MsgBox “Please label a diskette and insert it into the A drive.” & vbCr & vbCr & _
“FYI: The chapter file will be closed after it has been saved to the diskette.”, , _
“Insert Diskette”
nReply = MsgBox(“Do you want to delete any files that may be on this diskette ” & _
“BEFORE you save the chapter file to it?” & vbCr & vbCr & _
“You should clean the diskette if this is the only file, ” & _
“or the first in a batch of files, that you want to save to this diskette.”, _
vbYesNo + vbQuestion + vbDefaultButton2)
If nReply = vbYes Then
MyFile = Dir$(“a:*.*”)
Do While MyFile “”
Kill “a:” & MyFile
MyFile = Dir
Loop
End If
ChangeFileOpenDirectory “A:”
ActiveDocument.SaveAs FileName:=FileName
ChangeFileOpenDirectory “G:ProjectsDocs”
MsgBox “Remove the diskette from the A drive after the chapter file has closed.” & _
vbCr & vbCr & _
“Your chapter file is being sent to your network printer.”, , “Remove Diskette”
Exit Sub
ErrHandler:
If Err = 75 Then
‘ Read-only file
Resume Next
Else
MsgBox Err.Description, vbExclamation
End If
End Sub
I have also used a MsgBox with Yes and No buttons instead of an InputBox.
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications