Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6831983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:48:11+00:00 2026-05-26T22:48:11+00:00

Using VBA. My script moves a file into a directory. If that filename already

  • 0

Using VBA. My script moves a file into a directory. If that filename already exists in the target directory, I want the user to be prompted to rename the source file (the one that’s being moved) before the move is executed.

Because I want the user to know what other files are in the directory already (so they don’t choose the name of another file that’s already there), my idea is to open a FileDialog box listing the contents of the directory, so that the user can use the FileDialog box’s native renaming capability. Then I’ll loop that FileDialog until the source file and target file names are no longer the same.

Here’s some sample code:

Sub testMoveFile()

Dim fso As FileSystemObject
Dim file1 As File
Dim file2 As File
Dim dialog As FileDialog

Set fso = New FileSystemObject
fso.CreateFolder "c:\dir1"
fso.CreateFolder "c:\dir2"
fso.CreateTextFile "c:\dir1\test.txt"
fso.CreateTextFile "c:\dir2\test.txt"
Set file1 = fso.GetFile("c:\dir1\test.txt")
Set file2 = fso.GetFile("c:\dir2\test.txt")

Set dialog = Application.FileDialog(msoFileDialogOpen)

While file1.Name = file2.Name
    dialog.InitialFileName = fso.GetParentFolderName(file2.Path)
    If dialog.Show = 0 Then
        Exit Sub
    End If
Wend

file1.Move "c:\dir2\" & file1.Name

End Sub

But when I rename file2 and click ‘OK’, I get an error:

Run-time error '53': File not found

and then going into the debugger shows that the value of file2.name is <File not found>.

I’m not sure what’s happening here–is the object reference being lost once the file’s renamed? Is there an easier way to let the user rename from a dialog that shows all files in the target directory? I’d also like to provide a default new name for the file, but I can’t see how I’d do that using this method.

edit: at this point I’m looking into making a UserForm with a listbox that gets populated w/ the relevant filenames, and an input box with a default value for entering the new name. Still not sure how to hold onto the object reference once the file gets renamed, though.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:48:12+00:00Added an answer on May 26, 2026 at 10:48 pm

    Here’s a sample of using Application.FileDialog to return a filename that the user selected. Maybe it will help, as it demonstrates getting the value the user provided.

    EDIT: Modified to be a “Save As” dialog instead of “File Open” dialog.

    Sub TestFileDialog()
      Dim Dlg As FileDialog
      Set Dlg = Application.FileDialog(msoFileDialogSaveAs)
    
      Dlg.InitialFileName = "D:\Temp\Testing.txt"  ' Set suggested name for user
                                                   ' This could be your "File2"
    
      If Dlg.Show = -1 Then
        Dim s As String
        s = Dlg.SelectedItems.Item(1)  ` Note that this is for single-selections!
      Else
        s = "No selection"
      End If
      MsgBox s
    End Sub
    

    Edit two: Based on comments, I cobbled together a sample that appears to do exactly what you want. You’ll need to modify the variable assignments, of course, unless you’re wanting to copy the same file from “D:\Temp” to “D:\Temp\Backup” over and over. 🙂

    Sub TestFileMove()
      Dim fso As FileSystemObject
    
      Dim SourceFolder As String
      Dim DestFolder As String
      Dim SourceFile As String
      Dim DestFile As String
    
      Set fso = New FileSystemObject
      SourceFolder = "D:\Temp\"
      DestFolder = "D:\Temp\Backup\"
      SourceFile = "test.txt"
      Set InFile = fso.GetFile(SourceFolder & SourceFile)
      DestFile = DestFolder & SourceFile
      If fso.FileExists(DestFile) Then
        Dim Dlg As FileDialog
        Set Dlg = Application.FileDialog(msoFileDialogSaveAs)
        Dlg.InitialFileName = DestFile
        Do While True
          If Dlg.Show = 0 Then
            Exit Sub
          End If
          DestFile = Dlg.Item
    
          If Not fso.FileExists(DestFile) Then
            Exit Do
          End If
        Loop
      End If
    
      InFile.Move DestFile
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using a VBA script in Excel, I'm trying to insert a new row into
I want to run a VBA script from Excel 2007 that can switch focus
i am using vbscript to call a batch file. my script looks like: dim
I want to remove all vba-modules from an MS Word template using VBScript. I
I would like to have a complex form that uses various VBA script files.
I have created a VBA script that detects a number and shows a colour
I'm trying to write a VBA script that finds all embedded (.docx) files within
OK, I've just written a very simple VBA script that goes off and grabs
We have a simple file browser on our intranet, built using ASP/vbscript. The files
I am trying to write a VBA script which imports all of the Excel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.