Upload image in Excel from a directory

0

Good afternoon!

I'm using the code below to put load images in the excel of certain directory plus my formula is experiencing error

Followthecode:

PublicFunctiongetImage(ByValsCodeAsString)AsStringDimsFileAsStringDimoSheetAsWorksheetDimoSellAsRangeDimoImageAsShapeSetoCell=Application.CallerSetoSheet=oCell.ParentSetoImage=NothingFori=1TooSheet.Shapes.CountIfoSheet.Shapes(i).Name=sCodeThenSetoImage=oSheet.Shapes(i)ExitForEndIfNextiIfoImageIsNothingThensFile="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 anyone help me identify the error of this formula?

    
asked by anonymous 14.07.2017 / 19:37

1 answer

0

Try these settings:

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

    
31.08.2017 / 15:49