2017-07-26 187 views
0

在此先感谢您的帮助和意见。如何重命名多个pdf文件使用excel数据库vba

我有以下问题,但我不知道是否有可能...我试图重命名文件夹C中的PDF文件:\ ...我需要根据工作表重命名我在excel中,根据pdf文件进行排序..我想用excel中的电子表格数据进行重命名?

我有我研究一个代码,但它不搜索我的数据库,但它要求我输入的每个文件

公用Sub lsSelecionaArquivo() 昏暗Caminho作为字符串 昏暗NomeBase的名称作为字符串

Caminho = InputBox("Informe o local dos arquivos a serem renomeados:", "Pasta", "C:\TEMP") 
NomeBase = InputBox("Informe o local dos arquivos a serem renomeados:", "Renomear", "") 


lsRenomearArquivos Caminho, NomeBase 

结束子

公用Sub lsRenomearArquivos(Caminho作为字符串,NomeBase作为字符串)

Dim FSO As Object, Pasta As Object, Arquivo As Object, Arquivos As Object 
Dim Linha As Long 
Dim lSeq As Long 
Dim lNovoNome As String 

Set FSO = CreateObject("Scripting.FileSystemObject") 

If Not FSO.FolderExists(Caminho) Then 
    MsgBox "A pasta '" & Caminho & "' não existe.", vbCritical, "Erro" 
    Exit Sub 
End If 

lSeq = 1 

Set Pasta = FSO.GetFolder(Caminho) 
Set Arquivos = Pasta.Files 

Cells(1, 1) = "De" 
Cells(1, 2) = "Para" 

Linha = 2 

For Each Arquivo In Arquivos 

    Cells(Linha, 1) = UCase$(Arquivo.Path) 
    lNovoNome = Caminho & "\" & NomeBase & lSeq & Right(Arquivo, 4) 
    Name Arquivo.Path As lNovoNome 

    Cells(Linha, 2) = lNovoNome 
    lSeq = lSeq + 1 
    Linha = Linha + 1 

Next 

End Sub

回答

0

对于重命名部分,考虑这一点。

Sub RenameFiles() 
'Updateby20141124 
Dim xDir As String 
Dim xFile As String 
Dim xRow As Long 
With Application.FileDialog(msoFileDialogFolderPicker) 
    .AllowMultiSelect = False 
If .Show = -1 Then 
    xDir = .SelectedItems(1) 
    xFile = Dir(xDir & Application.PathSeparator & "*") 
    Do Until xFile = "" 
     xRow = 0 
     On Error Resume Next 
     xRow = Application.Match(xFile, Range("A:A"), 0) 
     If xRow > 0 Then 
      Name xDir & Application.PathSeparator & xFile As _ 
      xDir & Application.PathSeparator & Cells(xRow, "B").Value 
     End If 
     xFile = Dir 
    Loop 
End If 
End With 
End Sub 

https://www.extendoffice.com/documents/excel/2339-excel-rename-files-in-a-folder.html

,考虑到这。

Sub ListFiles() 
Dim MyFolder As String 
Dim MyFile As String 
Dim j As Integer 
MyFolder = "C:\DealerExam" 
MyFile = Dir(MyFolder & "\*.*") 
a = 0 
Do While MyFile <> "" 
    a = a + 1 
    Cells(a, 1).Value = MyFile 
    MyFile = Dir 
Loop 
End Sub 

这将列出所有在目录中的单元格开始“A1”

0

感谢您的帮助

这是一个有点紧张更改语言,因为我学习Java文件,并开始做VBA 。

当我运行的代码,我看到了,这是必要的电子表格有旧文件名和新的插入数据,但没有办法得到它刚刚得到新的数据?我试着搜索如何将它们制作为PDF,而不必将文件扩展名放在工作表中。

对不起,我没有太多的联系与VBA。

我非常感谢你帮助我。

Sub RenameFiles() 
 

 
Dim xDir As String 
 
Dim xFile As String 
 
Dim xRow As Long 
 
With Application.FileDialog(msoFileDialogFolderPicker) 
 
    .AllowMultiSelect = False 
 
If .Show = -1 Then 
 
    xDir = .SelectedItems(1) 
 
    xFile = Dir(xDir & Application.PathSeparator & "*") 
 
    Do Until xFile = "" 
 
     xRow = 0 
 
     On Error Resume Next 
 
     xRow = Application.Match(xFile, Range("A:A"), 0) 
 
     If xRow > 0 Then 
 
      Name xDir & Application.PathSeparator & xFile As _ 
 
      xDir & Application.PathSeparator & Cells(xRow, "B").Value 
 
     End If 
 
     xFile = Dir 
 
    Loop 
 
End If 
 
End With 
 
End Sub 
 

 
Sub ListFiles() 
 
Dim MyFolder As String 
 
Dim MyFile As String 
 
Dim j As Integer 
 
MyFolder = "C:\Users\AnaWill\Desktop\Holerites Folha\Nova pasta" 
 
MyFile = Dir(MyFolder & "\*.*") 
 
a = 0 
 
Do While MyFile <> "" 
 
    a = a + 1 
 
    Cells(a, 2).Value = MyFile 
 
    MyFile = Dir 
 
Loop 
 
End Sub