如何将文件从英语重命名为其他语言?

时间:2012-02-02 12:31:42

标签: excel excel-vba vba

我真的希望有人可以帮助我,因为我需要这样做,

我有一个excel文件,其中包含一些列和一列文件名为英文,其他列的文件名为其他语言。 现在我需要做的是用其他语言重命名文件,是否可以重命名。

我试过这段代码

Sub pdfrenamefile()
Dim oldfile As String
Dim nwfile As String
Dim rng As Range
Dim fname As Range
Set rng = Range("Y7", Range("Y" & Rows.Count).End(xlUp))
For Each fname In rng
    If IsEmpty(fname) Or fname = "" Then
    'do nothing
    Else
        If FileFolderExists(Cells(1, 1) & fname) Then
            nwfile = fname.Offset(, 1) & ".PDF"
            Name Cells(1, 1) & fname As Cells(1, 1) & nwfile
            fname.Offset(0, 2) = nwfile
            fname.Offset(0, 3) = "Success"
        Else
            Range("AB" & fname.Row) = "File Not Found"
        End If
    End If
Next fname
End Sub

示例:

示例数据ID OldFileName NewFileName

1 Sales1.PDF తెలుగు1.PDF   
2 Sales02.PDF తెలుగు02.PDF   
3 Sales567.PDF తెలుగు567.PDF   
4 dest67.PDF తెలుగు67.PDF  

我试过但它只转换成英文,但不接受其他。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

我对您的代码进行了最少的更改以使其正常工作。但是,我发现您的代码令人困惑,所以我也建议进行一些进一步的更改。

我必须将样本数据放在X6:Z10范围内,因此第一个旧文件名在单元格Y7中。

我必须将包含文件的文件夹的名称放在单元格A1中。

我希望我的改变的原因很清楚。问他们是不是。

Sub pdfrenamefile()

  Dim oldfile As String
  Dim nwfile As String
  Dim rng As Range
  Dim fname As Range

  ' I find your names confusing.  For example, You should rename fname to make
  ' clear that it is a range.  You have declared oldname but do now use it. 

  ' You are using methods that require a file system object
  Dim fso As Object
  Set fso = CreateObject("Scripting.FileSystemObject")

  Set rng = Range("Y7", Range("Y" & Rows.Count).End(xlUp))

  ' You use too many different methods of located cells.  You use 
  ' rng.Offset(0, c), Cells(r, c) and "AB" & r.  Will you understand this
  ' code in six months?  What if you decide to change the position of the
  ' table of names?

  For Each fname In rng

    ' fname is a range.  fname.Value is its value.
    If IsEmpty(fname.Value) Or fname.Value = "" Then
    Else
      ' I have replaced "FileFolderExist" by "fso.FileExists".
      ' "Cells(1, 1)" is acceptable but I prefer "Cells(1, 1).Value"
      ' which makes absolutely clear you want the value.   
      If fso.FileExists(Cells(1, 1).Value & fname.Value) Then
        ' You already have the extension in the worksheet so
        ' do not need to add ".PDF".
        nwfile = fname.Offset(0, 1).Value
        ' You check the old file exists but not that the new file
        ' does not exist.  I have added another If-Then-Else-End If.
        If fso.FileExists(Cells(1, 1) & nwfile) Then
          Range("AB" & fname.Row) = nwfile & " already exists"
        Else
          ' The Name statement will not accept non-English letters.
          ' I have used MoveFile which will accept non-English letters. 
          ' "fname.Value" not "fname" because "fname" is a range.
          fso.movefile Cells(1, 1).Value & fname.Value, _
                                                   Cells(1, 1).Value & nwfile
          ' You have nwfile in Offset(0,1).  Why duplicate it?
          fname.Offset(0, 2) = nwfile
          fname.Offset(0, 3) = "Success"
        End If
      Else
        ' I have added the old name to the message to make clear
        ' what has not been found.
        Range("AB" & fname.Row) = fname.Value & " not found"
      End If
    End If
  Next fname

End Sub