• Privacywetgeving
    Het is bij Helpmij.nl niet toegestaan om persoonsgegevens in een voorbeeld te plaatsen. Alle voorbeelden die persoonsgegevens bevatten zullen zonder opgaaf van reden verwijderd worden. In de vraag zal specifiek vermeld moeten worden dat het om fictieve namen gaat.

Foto's in Excel importeren vanuit (Sub) Mappen

Status
Niet open voor verdere reacties.

Henri232

Gebruiker
Lid geworden
18 apr 2012
Berichten
23
Beste mensen,

Hierbij een bestand dat uit een map de gevraagde foto's ophaalt waarvan
de naam wordt gevraagd in kolom B.
Graag zou ik echter ook naar de bestanden willen zoeken in de submappen.
Mijn kennis gaat echter niet zo ver. Wie kan mij helpen?

Alvast bedankt,
Henri
 

Bijlagen

  • Bloemen.xlsm
    19,9 KB · Weergaven: 46
Ik heb zoveel mogelijk van je originele macro behouden.
Code:
Public PathPicture As String

Private Sub CommandButton1_Click()
Dim pictureNameColumn   As String 'column where picture name is found
Dim picturePasteColumn  As String 'column where picture is to be pasted

Dim pictureName         As String 'picture name
Dim lastPictureRow      As Long   'last row in use where picture names are
Dim pictureRow          As Long   'current picture row to be processed
Dim pathForPicture      As String 'path of pictures

    pictureNameColumn = "B"
    picturePasteColumn = "H"
    
    pictureRow = 2 'starts from this row
    
    'find row of the last cell in use in the column where picture names are
    lastPictureRow = Cells(Rows.Count, pictureNameColumn).End(xlUp).Row
    
    'stop screen updates while macro is running
    Application.ScreenUpdating = False
   
    pathForPicture = "C:\Bloemen\"
    'loop till last row
    Do While (pictureRow <= lastPictureRow)
     
        pictureName = Cells(pictureRow, "B") 'This is the picture name
        
        'if picture name is not blank then
        If (pictureName <> vbNullString) Then
            'check if pic is present
            Call Enlist_Directories(pathForPicture, pictureName & ".jpg")
            
            If (Dir(PathPicture) <> vbNullString) Then
            
                Cells(pictureRow, picturePasteColumn).Select 'This is where picture will be inserted
                ActiveSheet.Pictures.Insert(PathPicture).Select 'Path to where pictures are stored
                
                With Selection
                    .Left = Cells(pictureRow, picturePasteColumn).Left
                    .Top = Cells(pictureRow, picturePasteColumn).Top
                    .ShapeRange.LockAspectRatio = msoFalse
                    .ShapeRange.Height = 100#
                    .ShapeRange.Width = 130#
                    .ShapeRange.Rotation = 0#
                End With
            Else
                'picture name was there, but no such picture
                Cells(pictureRow, picturePasteColumn) = "No Picture Found"
            End If
        Else
            'picture name cell was blank
        End If
        'increment row count
        pictureRow = pictureRow + 1
    Loop
     
    Application.ScreenUpdating = True

End Sub

Public Sub Enlist_Directories(strPath As String, sPicture As String)
Dim strFldrList() As String
Dim lngArrayMax, x As Long
lngArrayMax = 0
strFn = Dir(strPath & "*.*", 23)
While strFn <> ""
  If strFn <> "." And strFn <> ".." Then
    If (GetAttr(strPath & strFn) And vbDirectory) = vbDirectory Then
      lngArrayMax = lngArrayMax + 1
      ReDim Preserve strFldrList(lngArrayMax)
      strFldrList(lngArrayMax) = strPath & strFn & "\"
    Else
        If UCase(strPath & strFn) = UCase(strPath & sPicture) Then PathPicture = strPath & strFn: Exit Sub
    End If
  End If
  strFn = Dir()
Wend
If lngArrayMax <> 0 Then
  For x = 1 To lngArrayMax
    Call Enlist_Directories(strFldrList(x), sPicture)
  Next
End If
End Sub
 
Hoi Rudi,

Het werkt!!! Heel erg bedankt. Nu snap ik waarom sommige forumleden
de titel 'Mega Honourable Senior Member' dragen. :D

Groeten,
Henri
 
Hoi Rudi,

De code werkt op zich heel goed alleen wanneer een onbekende naam in
kolom 'B' wordt gebruikt geeft de code als uitkomst de foto van de regel
daarboven.
Zou je daar nog even naar willen kijken?

Voor de rest ben ik zeer gelukkig met het resultaat.

Alvast bedankt,
Henri
 
Code:
Public PathPicture As String
Public FullPath As String

Private Sub CommandButton1_Click()
Dim pictureNameColumn   As String 'column where picture name is found
Dim picturePasteColumn  As String 'column where picture is to be pasted

Dim pictureName         As String 'picture name
Dim lastPictureRow      As Long   'last row in use where picture names are
Dim pictureRow          As Long   'current picture row to be processed
Dim pathForPicture      As String 'path of pictures

    pictureNameColumn = "B"
    picturePasteColumn = "H"
    
    pictureRow = 2 'starts from this row
    
    'find row of the last cell in use in the column where picture names are
    lastPictureRow = Cells(Rows.Count, pictureNameColumn).End(xlUp).Row
    
    'stop screen updates while macro is running
    Application.ScreenUpdating = False
    
    pathForPicture = "C:\Bloemen\"
    'loop till last row
    Do While (pictureRow <= lastPictureRow)
     
        pictureName = Cells(pictureRow, "B") 'This is the picture name
        
        'if picture name is not blank then
        If (pictureName <> vbNullString) Then
            'check if pic is present
            Call Enlist_Directories(pathForPicture, pictureName & ".jpg")
            
            If Dir(PathPicture) <> vbNullString And UCase(PathPicture) = UCase(FullPath & pictureName & ".jpg") Then
            
                Cells(pictureRow, picturePasteColumn).Select 'This is where picture will be inserted
                ActiveSheet.Pictures.Insert(PathPicture).Select 'Path to where pictures are stored
                
                With Selection
                    .Left = Cells(pictureRow, picturePasteColumn).Left
                    .Top = Cells(pictureRow, picturePasteColumn).Top
                    .ShapeRange.LockAspectRatio = msoFalse
                    .ShapeRange.Height = 100#
                    .ShapeRange.Width = 130#
                    .ShapeRange.Rotation = 0#
                End With
            Else
                'picture name was there, but no such picture
                Cells(pictureRow, picturePasteColumn) = "No Picture Found"
            End If
        Else
            'picture name cell was blank
        End If
        'increment row count
        pictureRow = pictureRow + 1
    Loop
     
    Application.ScreenUpdating = True

End Sub

Public Sub Enlist_Directories(strPath As String, sPicture As String)
Dim strFldrList() As String
Dim lngArrayMax, x As Long
lngArrayMax = 0
strFn = Dir(strPath & "*.*", 23)
While strFn <> ""
  If strFn <> "." And strFn <> ".." Then
    If (GetAttr(strPath & strFn) And vbDirectory) = vbDirectory Then
      lngArrayMax = lngArrayMax + 1
      ReDim Preserve strFldrList(lngArrayMax)
      strFldrList(lngArrayMax) = strPath & strFn & "\"
    Else
        If UCase(strPath & strFn) = UCase(strPath & sPicture) Then PathPicture = strPath & strFn: FullPath = strPath: Exit Sub
    End If
  End If
  strFn = Dir()
Wend
If lngArrayMax <> 0 Then
  For x = 1 To lngArrayMax
    Call Enlist_Directories(strFldrList(x), sPicture)
  Next
End If
End Sub
 
Goedemorgen Rudi,

Zojuist de nieuwe code getest en voor zover ik het kan zien werkt het
geweldig. Nogmaals heel hartelijk dank.

Groeten,
Henri
 
Hoi Rudi,

Toch nog een vraagje: Ik heb het pad naar de foto's aangepast naar een
netwerklocatie en nu is de verwerking een stuk trager. :(
Komt dat doordat het een netwerklocatie is of omdat hij nu alle mappen van
de netwerklocatie in beeld brengt ipv alleen de map met de foto's en de submappen
daarvan?

Groeten,
Henri
 
Met netwerken heb ik niet onmiddellijk ervaring maar ik veronderstel van wel.
De macro begint te zoeken vanaf de directory die jij als beginpad aangeeft dus als hij het volledige netwerk moet doorzoeken kan dit wel enorm vertragend worden.
 
OK Dan duurt het gewoon wat langer.
Voor de rest werkt het gewoon perfect.

mvg Henri
 
Status
Niet open voor verdere reacties.
Steun Ons

Nieuwste berichten

Terug
Bovenaan Onderaan