使用Application.FileDialog重命名VBA中的文件 [英] using Application.FileDialog to rename a file in VBA

查看:311
本文介绍了使用Application.FileDialog重命名VBA中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用VBA.我的脚本将文件移动到目录中.如果目标目录中已经存在该文件名,则希望在执行移动之前提示用户重命名源文件(正在移动的文件).

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.

因为我想让用户知道目录中还存在其他文件(因此他们不选择其他已经存在的文件名),所以我的想法是打开一个FileDialog框,列出目录内容,以便用户可以使用FileDialog框的本机重命名功能.然后,我将循环该FileDialog,直到源文件名和目标文件名不再相同为止.

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.

以下是一些示例代码:

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

但是当我重命名file2并单击确定"时,出现错误:

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

Run-time error '53': File not found

,然后进入调试器,显示file2.name的值为<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:这时,我正在考虑制作一个UserForm,其中包含一个使用相关文件名填充的列表框和一个带有用于输入新名称的默认值的输入框.不过,仍然不确定在文件重命名后如何保留对象引用.

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.

推荐答案

以下是使用Application.FileDialog返回用户选择的文件名的示例.也许会有所帮助,因为它证明了获得用户提供的价值.

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.

修改为另存为"对话框,而不是打开文件"对话框.

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

编辑两个:根据评论,我将一个样本拼凑在一起,该样本似乎完全可以完成您想要的操作.当然,除非您要一遍又一遍地将同一文件从"D:\ Temp"复制到"D:\ Temp \ Backup",否则需要修改变量分配. :)

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

这篇关于使用Application.FileDialog重命名VBA中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆