You are on page 1of 3

Sub FicherosCarpeta()

08
Dim Ruta As String
09
Dim Fotos As Object
10

Dim rng As Range, celda As Range

11
12
'Saltamos posibles errores
13
On Error Resume Next
14
Application.ScreenUpdating = False
15 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
16
Dim img As Shape
17
'si existe alguna foto, la borro:
18
On Error Resume Next
19
For Each img In ActiveSheet.Shapes
20
21
22
23
24
25
26
27
28
29
30

If img.Type = 11 Then img.Delete


Next
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Creamos el objeto FileSystemObject que
'proporciona acceso al sistema de archivos de un equipo

Set fso = CreateObject("Scripting.FileSystemObject")


'Indicamos la ruta de donde vamos a obtener
'los ficheros, en este caso E:\excelforo\Fotos\
Ruta = "E:\excelforo\Fotos\"
'definimos dos variables que necesitaremos,
'para recuperar el nombre de la carpeta, y los ficheros que haya
31
dentro
32
Set Carpeta = fso.GetFolder(Ruta)
33
34
35

Set ficheros = Carpeta.Files


'damos un ttulo en negrita para la celda A1
With Range("A1")

36 .Value = "Ficheros de la carpeta " & Ruta


37
.Font.Bold = True
38
End With
39 'escribimos los ficheros, a partir de A2
40
Range("A2").Select
41
For Each archivo In ficheros
42
'escribimos el nombre del fichero
43
ActiveCell = archivo.Name
44
'bajamos una fila
45
ActiveCell.Offset(1, 0).Select
46
Next archivo
47
ActiveCell.EntireColumn.AutoFit
48 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
49 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
50 Set rng = Worksheets("Hoja2").Range("A2:A15")
51
For Each celda In rng

52
53
54
55
56
57
58
59

If Len(Trim(celda)) > 0 Then


'defino la celda equivalente de la columna A y la selecciono
Set r1 = Cells(celda.Row, "B")
r1.Select
'se inserta la imagen de la ruta definida
'Set Fotos = ActiveSheet.Pictures.Insert(Ruta & celda.Value)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'VARIANTE PARA AADIR IMAGEN NO VINCULADA AL FICHERO Y RUTA!!

60 Set Fotos = ActiveSheet.Shapes.AddPicture(Filename:=Ruta & celda.Value,


_
61 linktofile:=msoFalse, savewithdocument:=msoCTrue, _
62
Left:=0, Top:=0, Width:=-1, Height:=-1)
63 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'con la posicin definida respecto a la celda de la columna B
64
seleccionada
65
With Fotos
66
.Top = r1.Top
67
.Width = .Width / 1.5
68
.Height = .Height / 1.5
69 .Left = r1.Left + (r1.Width - Fotos.Width) / 2
70
.ShapeRange.LockAspectRatio = msoFalse
71
r1.EntireRow.RowHeight = .Height
72
.Placement = xlMoveAndSize
73
End With
74
r1.Select
75
End If
76
Next celda
77 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
78 'Limpiamos los objetos y variables definidas
79
Set fso = Nothing
80
Set Carpeta = Nothing
81

Set ficheros = Nothing

82

Set rng = Nothing

83

Set r1 = Nothing

84

Set Fotos = Nothing

85
86
87

Application.ScreenUpdating = True
End Sub

Sub Logo_corpac()

Dim h As Integer
Dim ruta as string
Ruta = "C:\Users\niko\Desktop\1.jpg"

Worksheets(h).Select
Range("C2").Select
Set Fotos = ActiveSheet.Shapes.AddPicture(Filename:=Ruta,
_
linktofile:=msoFalse, savewithdocument:=msoCTrue, _
Left:=0, Top:=0, Width:=-1, Height:=-1)

Range("G12").Select
End Sub

You might also like