Excel VBA: Define File Path

When writing VBA code I noticed some machines error when I include the file extension. Does anyone know the reason and way to account for this in my code? See examples below:

Example1: Name "C:\TestFolder\TestFile.txt" AS "C:\TestFolder\RenamedFile.txt" <---.txt included: works on some machines and fails on others...
Example2: Name "C:\TestFolder\TestFile" AS "C:\TestFolder\RenamedFile.txt" <--------.txt excluded: works on some machines, but fails on others...

Note: This isn't just when using Name function. I am having this issue for everything VBA that requires defining a file path.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
How is the code failing?

As far as I know this problem occurs because of Windows settings, specifically whether or not to show file extensions for known file types.

Normally the solution is to always include the file extension, but it really depends on the code being used.
ouestqueAuthor Commented:
Thank you Norie! I agree. I always try to include the file extension. Nonetheless, when I do, VBA throws an error on some machines. I think it is a 1004 error. Let's assume the only code being used is the code below. What would you do if you were in my scenario knowing that it would get a 1004 error on some machines.

Function RenameFile ()
Dim OldFileName, NewFile Name as string

   OldFileName = "C:\TestFolder\TestFile.txt"
   NewFileName = "C:\TestFolder\RenamedFile.txt"
   Name OldFileName AS NewFileName

End Function
NorieAnalyst Assistant Commented:
I might try something with Dir, or perhaps use the FSO, see below, to rename the file, really depends on the error message.
Dim fso As Object
Dim OldFileName As String
Dim NewFileName As String

   OldFileName = "C:\TestFolder\TestFile.txt"
   NewFileName = "C:\TestFolder\RenamedFile.txt"

   Set fso = CreateObject("Scripting.FileSystemObject")

   fso.MoveFile OldFileName, NewFileName

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.