Upload image in Excel from a directory

Asked

Viewed 236 times

0

Good afternoon!

I am using the code below to load images in excel of certain directory plus my formula is showing error.inserir a descrição da imagem aqui

Follows the code:

Public Function getImage(ByVal sCode As String) As String

Dim sFile As String
Dim oSheet As Worksheet
Dim oSell As Range
Dim oImage As Shape

Set oCell = Application.Caller
Set oSheet = oCell.Parent

Set oImage = Nothing
For i = 1 To oSheet.Shapes.Count
    If oSheet.Shapes(i).Name = sCode Then
    Set oImage = oSheet.Shapes(i)
    Exit For
End If
 Next i

 If oImage Is Nothing Then
 sFile = "C:\Temp\Nova pasta" & sCode & ".jpg"
 Set oImage = oSheet.Shapes.AddPictute(sFille, msoCTrue, oCell.Left, oCell.Top, oCell.Width, oCell.Height)
 oImage.Name = sCode

 Else
    With oImage
        .Left = oCell.Left
        .Top = oCell.Top
        .Width = oCell.Width
        .Height = oCell.Height
    End With
End If

getImage = ""

End Function

Could someone help me identify the error of this formula?

1 answer

0

Try it with these adjustments:

Public Function getImage(ByVal sCode As String) As String

Dim sFile As String
Dim oSheet As Worksheet
Dim oCell As Range 'Declaração com nome diferente do código
Dim oImage As Shape

Set oCell = Application.ActiveCell
Set oSheet = oCell.Parent

Set oImage = Nothing
For i = 1 To oSheet.Shapes.Count
    If oSheet.Shapes(i).Name = sCode Then
    Set oImage = oSheet.Shapes(i)
    Exit For
End If
 Next i

If oImage Is Nothing Then
sFile = "C:\Temp\Nova pasta\" & sCode & ".jpg" 'Faltava uma "\"
Set oImage = oSheet.Shapes.AddPicture(sFile, msoTrue, msoTrue, oCell.Left, oCell.Top, oCell.Width, oCell.Height) 
'O nome da variável sFile estava incorreto e faltava um argumento
oImage.Name = sCode

Else
   With oImage
        .Left = oCell.Left
        .Top = oCell.Top
        .Width = oCell.Width
        .Height = oCell.Height
   End With
End If

getImage = ""

End Function

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.