It could be a timing issue and you might need to incorporate DoEvents to ensure that the file has been created before the code tries to rename it.
Run-Time error '53': File not found. Window Message
I have a macro that creates “prn file” then rename this prn file in the same directory from the the cell "C6" of the FRUIT Worksheets.
The macro does the following:
- Open Apple1.docm
- I will see the print window to select the printer.
- Then I click to check “Print to File” box
- Click "OK"
- Type B in the file name box
- Save it as “B” under the directory C:\Bananas\PRN Files\
- Click "OK"
- Close the Apple1.docm.
- Rename the “9.rpn” to ‘Los Angeles_Bananas_Yellow.prn
However, the filename is not renaming it and I get the window message below, highlighted in yellow as shown in the pictures below.
Obviously, the file “b” is no renamed (see the picture below)
Could you please support me to fix this error '53'
This is the macro:
'###########################################################
Sub MainAll()
Call ALLApples
Call ALLBananas
End Sub
Sub ALLApples()
Call A
End Sub
Sub A()
Set wd = CreateObject("word.application")
wd.Application.Documents.Open "C:\Apple\Apple1.docm"
wd.Application.Visible = True
wd.Application.Run "PrinterSelectionInWord.PrinterSelectionInWord"
Set wd = Nothing
End Sub
Sub ALLBananas()
Call E
End Sub
Sub E()
Dim oldName As String
Dim newName As String
'************************* Take the Cell Value from Excel *************************
Job = Worksheets("FRUIT").Range("C6")
'************************* Take the Cell Value from Excel *************************
oldName = "OLDFILE": newName = "NEWFILE" '
'************************* Rename the Filename ************************************
oldName = "C:\Bananas\PRN Files\B.prn": newName = "C:\Bananas\PRN Files" & Job & "_Banana_Yellow.prn"
'************************* Rename the Filename ************************************
Name oldName As newName '
End Sub
'###########################################################
Microsoft 365 and Office | Word | For home | Windows
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
-
Doug Robbins - MVP - Office Apps and Services 322.1K Reputation points MVP Volunteer Moderator2021-07-23T00:43:09+00:00
2 additional answers
Sort by: Most helpful
-
Anonymous
2021-07-23T02:09:10+00:00 It could be a timing issue and you might need to incorporate DoEvents to ensure that the file has been created before the code tries to rename it.
Doug,
Thank you very much for your prompt response.
I have another question. Do I have to place the DoEvent between both macros?
Or where do you think is more effectively to locate it?
Thanks
Maperalia
-
Anonymous
2021-07-23T05:19:32+00:00 Doug,
I applied this statement before the code tries to rename it, as you advised me.
'**************************************************
Application.Wait (Now + TimeValue("0:00:15"))
'**************************************************
And IT IS WORKING PERFECTLY!!!!!
Thank you very much. I appreciate your support
Maperalia,