如何修复我的VBA重命名程序的0返回?

时间:2014-02-26 14:33:18

标签: vba return-value file-rename

我愿意彻底改变这段代码。原始链接在代码本身中。我确信有一种更简单的方法,实际的重命名部分不是我自己的代码,所以我会重做它,所以它不是抄袭。我不能使用批处理文件重命名器来做它;我需要自己做以避免合法的麻烦:)没有灰色区域!

无论如何,经过我自己的几十次尝试之后,我终于陷入困境并在线抓取了这个代码,该代码应该重命名我指定的文件。我编辑它以适应我的参数和分配的变量/目录。但是,当我运行它时,我总是返回零并且文件没有被重命名。我能想到的一件事是这个目录将转到文件夹的完整路径名而不是最后一个“\”之后的部分。但我不知道如何解决这个问题。我想过试着告诉它只是告诉它拉,说字符串的最后8个字符,但这不会起作用,因为这些字符串长度会从一个字符变化到20个左右的字符。

这是我的代码:

Private Sub Apply_Click()
'This will initiate Module 1 to do a batch rename to find and replace all
'Module 1 will then initiate the resolving links process

Dim intResponse As Integer 'Alerts user to wait until renaming is complete
intResponse = MsgBox("Your folders are being updated. Please wait while your files are     renamed and your links are resolved.")

If intResponse = vbOK Then 'Tests to see if msgbox_click can start a new process
Dim i As Integer
Dim from_str As String
Dim to_str As String
Dim dir_path As String


from_str = Old_Name_Display.Text
to_str = New_Name.Text

dir_path = New_Name.Text
If Right$(dir_path, 1) <> "\" Then dir_path = dir_path _
    & "\"

Old_Name_Display = dir$(dir_path & "*.*", vbNormal)
Do While Len(Old_Name_Display) > 0
    ' Rename this file.
    New_Name = Replace$(Old_Name_Display, from_str, to_str)
    If New_Name <> Old_Name_Display Then
Name Old_Name_Display.Text As New_Name.Text
        i = i + 1
    End If

    ' Get the next file.
    Old_Name_Display = dir$()
Loop

MsgBox "Renamed " & Format$(i) & " files. Resolving links now."
If intResponse = vbOK Then
MsgBox "You selected okay. Good luck coding THIS." 'Filler line to test that next step     will be ready to initialize
Else: End
End If

Exit Sub


'Most of batch renaming process used from VB Helper, sponsored by Rocky Mountain Computer Consulting, Inc. Copyright 1997-2010; original code available at http://www.vb-helper.com/howto_rename_files.html


End Sub

有没有人有另一个理论为什么我得到0回报/如何解决这个潜在的问题?

1 个答案:

答案 0 :(得分:1)

看起来在重命名中引用了目录。

更改

Name Old_Name_Display.Text As New_Name.Text

Name Dir_Path & Old_Name_Display.Text As Dir_Path & New_Name.Text