【问题标题】:Add every file inside a folder将每个文件添加到文件夹中
【发布时间】:2020-03-04 05:29:17
【问题描述】:

我正在尝试做一个宏,让用户选择一个文件夹,里面有更多文件夹,这个文件夹可以有图像 .jpg 或 .png。我想要的是宏只添加excel中的图像文件,任何图像文件。它现在所做的是添加图像,但前提是它们的名称为 1.jpg、2.jpg、3.jpg 等。

Dim Secfolder As String
MsgBox ("Busque y seleccione la carpeta que contiene las carpetas de los sectores en el punto que realizará.")
With Application.FileDialog(msoFileDialogFolderPicker)

.Title = "Buscar carpeta"
.ButtonName = "Aceptar"
.InitialFileName = "C:\"

If .Show = -1 Then
 Secfolder = .SelectedItems(1)
End If

Sheets("Matriz_de_Hallazgos").Select

l = 1

For i = 1 To 200
idm = (Worksheets("Matriz_de_Hallazgos").Cells(i + 2, 1))

If idm = 1 Then
Application.SpellingOptions.IgnoreCaps = True
 ' Colocar la ruta de las fotos; las fotos deben llamarse como números. Ej: 1.jpg'
    RutaCompleta = Secfolder & "\" & "sector " & idm & "\" & l & ".jpg"
    ActiveSheet.Cells(i + 2, 3).Select
    With ActiveSheet.Shapes.AddPicture(Filename:=RutaCompleta, linktofile:=msoFalse, _
        SaveWithDocument:=msoCTrue, Left:=0, Top:=0, Width:=0, Height:=0)
        .LockAspectRatio = 0
        .Top = ActiveCell.Top
        .Left = ActiveCell.Left
        .Width = ActiveCell.Width
        .Height = ActiveCell.Height
    End With
    l = l + 1
End If
Next i

有什么想法吗?谢谢

【问题讨论】:

    标签: excel vba image add


    【解决方案1】:

    它正在添加1.jpg,2.jpg 等,因为您在以下行中使用了l 变量。

    RutaCompleta = Secfolder & "\" & "sector " & idm & "\" & l & ".jpg"
    

    您必须使用实际文件名而不是 l 变量。

    您可以使用以下内容循环浏览所选文件夹的文件。

    FolderName = "D:\" 'Replace it with selected folder
    Set FSOLibrary = CreateObject("Scripting.FileSystemObject")
    Set FSOFolder = FSOLibrary.GetFolder(FolderName)
    Set FSOFile = FSOFolder.Files
    'Use For Each loop to loop through each file in the folder
    For Each FSOFile In FSOFile
        'Insert actions to be perfomed on each file
        MsgBox FSOFile.Name
    Next
    

    【讨论】:

      猜你喜欢
      • 2013-11-19
      • 2015-12-03
      • 2018-03-08
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 2017-12-21
      相关资源
      最近更新 更多