• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Rename all pictures in Excel according to a name list

Herman4532

New Member
upload_2014-1-16_17-50-18.png

I have hundreds of pictures in the same worksheet in column F, and I would like to rename them to column E (as shown in the picture attached). For example, Picture in Cell F1 renamed to the name in Cell E1. I was able to rename the pictures to Column E using the following macros. However, they are not in the correct order. For example, Picture in Cell F1 renamed to the name in Cell E12 and so forth.Can somebody help please?

Run the macro “GetShapeNames” first to retrieve all freeform shape names Excel assigned to the shapes and write it to column A. Call “SetShapeNames” afterwards to rename all pictures with the names of column E.

Code:
Sub GetShapeNames()
Dim shp As Shape
Dim i As Long

i = 1
For Each shp In ActiveSheet.Shapes
ActiveSheet.Range("A1").Offset(i, 0).Value = _
ActiveSheet.Shapes(i).Name
i = i + 1
Next shp

End Sub

Sub SetShapeNames()
Dim shp As Shape
Dim i As Long

i = 1
For Each shp In ActiveSheet.Shapes
ActiveSheet.Shapes(i).Name = _
ActiveSheet.Range("E1").Offset(i, 0).Value
i = i + 1
Next shp
 
Last edited:
Dear Herman4532

Using
ActiveSheet.Shapes(i).Name = _
ActiveSheet.Range("E1").Offset(ActiveSheet.Shapes(i).BottomRightCell.Row - 1, 0).Value

in setshapenames() in place of

ActiveSheet.Shapes(i).Name = _
ActiveSheet.Range("E1").Offset(i, 0).Value

seems to work
 
Dear Herman4532

Using
ActiveSheet.Shapes(i).Name = _
ActiveSheet.Range("E1").Offset(ActiveSheet.Shapes(i).BottomRightCell.Row - 1, 0).Value

in setshapenames() in place of

ActiveSheet.Shapes(i).Name = _
ActiveSheet.Range("E1").Offset(i, 0).Value

seems to work

Thanks Jake Collins! You are my Hero!!!!
 
Hi, Herman4532!

As a new user you might want (I'd say should and must) read this:
http://chandoo.org/forum/forums/new-users-please-start-here.14/

And regarding your issue, check this link:
http://chandoo.org/forum/threads/vba-code-working-with-shapes.6351/

Regards!


Hi SirJB7, thanks for your advice! If you can tell I'm a new member, I guess I must have done something wrong in this post......Can you be more specific so I won't make the same mistake again? Thanks!
 
Hi, Herman4532!
Nothing wrong at all, since you're a new member just in case you haven't gone thru the 1st link it was just a reminder. And about your main question, I posted the 2nd link where you may find a problem like your's with a solution that I proposed. But I see than Jake Collins' post has solved your issue, so welcome whenever needed or wanted.
Regards!
PS: If even been solved you want further explanations about the subject please continue in this same thread.
 
Hi, Herman4532!
Nothing wrong at all, since you're a new member just in case you haven't gone thru the 1st link it was just a reminder. And about your main question, I posted the 2nd link where you may find a problem like your's with a solution that I proposed. But I see than Jake Collins' post has solved your issue, so welcome whenever needed or wanted.
Regards!
PS: If even been solved you want further explanations about the subject please continue in this same thread.

Thanks for your prompt reply! In fact, the issue is not fully solved. My next step is to extract all these renamed pictures as separate .jpg files inside a folder. And I want these extracted pictures to have the same name as column E. Do you think if it is even possible?
 
Hi, Herman4532!
I'm trying to search another thread related to shapes naming and that kinda stuff but I didn't succeed yet. Will post here when and if I find it.
Regards!
 
@Marc L
Hi!
I've done it here at least 2 or 3 times (including that of the Formula Translator which is password "protected") but I'm unable to find them... :(:mad:
I refuse to do it again... once more... and I miss a lot the old forums search feature, it was 10 times better than the actual, but I guess that in the overall this is much better than the missed one.
If I'm not wrong I've done something with the clipboard, I hope to find it tomorrow.
ReCarlGardsBerg!
 
Hi, Herman4352!

Better later than never... Well, as I didn't found it I did it again :(

Give a look at the uploaded file. It has the workbook and 4 test images. This is the code:
Code:
Option Explicit

' global constants
Const gksWS = "Hoja1"
Const gkiName = 1
Const gkiImage = 2
Const gkiRatio = 3
Const gksPath = "\"
Const gksSuffix = "_copy"
Const gksExtension = ".jpg"
Const gknUnknownFactor = 26.46

' global declarations
Dim cell As Range
Dim gsPicture As String, gnWidth As Single, gnHeight As Single

Sub LoadImages()
    ' constants
    ' declarations
    Dim I As Integer, pic As Object, nWidthW As Single, nHeightW As Single
    ' start
    '  clear shapes (images) in col gkiImage
    With Worksheets(gksWS)
        For I = .Shapes.Count To 1 Step -1
            If .Shapes(I).Type = msoLinkedPicture Then
                If .Shapes(I).Left >= .Cells(1, gkiImage).Left And _
                  .Shapes(I).Left < .Cells(1, gkiImage + 1).Left Then .Shapes(I).Delete
            End If
        Next I
    End With
    ' process
    I = 1
    With Worksheets(gksWS)
        Do Until .Cells(I, gkiName).Value = ""
            ' filename
            gsPicture = ActiveWorkbook.Path & gksPath & _
                .Cells(I, gkiName).Value & gksExtension
            If Dir(gsPicture) <> "" Then
                ' where
                Set cell = .Cells(I, gkiImage)
                ' aspect ratio
                Set pic = LoadPicture(gsPicture)
                nWidthW = pic.Width / gknUnknownFactor
                nHeightW = pic.Height / gknUnknownFactor
                Select Case nWidthW / nHeightW
                    Case Is > cell.Width / cell.Height
                        gnWidth = cell.Width
                        gnHeight = nHeightW * gnWidth / nWidthW
                        .Cells(I, gkiRatio).Value = (pic.Width / gknUnknownFactor) / cell.Width
                    Case Is <= cell.Width / cell.Height
                        gnHeight = cell.Height
                        gnWidth = nWidthW * gnHeight / nHeightW
                        .Cells(I, gkiRatio).Value = (pic.Height / gknUnknownFactor) / cell.Height
                End Select
                ' insert
                .Shapes.AddPicture gsPicture, True, True, _
                    cell.Left, cell.Top, gnWidth, gnHeight
            End If
            I = I + 1
        Loop
    End With
    ' end
    Set pic = Nothing
    Set cell = Nothing
    Beep
End Sub

Sub SaveImages()
    ' constants
    ' declarations
    Dim I As Integer, J As Integer, cht As Chart
    ' start
    ' process
    I = 1
    With Worksheets(gksWS)
        For I = .Shapes.Count To 1 Step -1
            If .Shapes(I).Type = msoLinkedPicture Then
                If .Shapes(I).Left >= .Cells(1, gkiImage).Left And _
                  .Shapes(I).Left < .Cells(1, gkiImage + 1).Left Then
                    ' cell
                    J = .Shapes(I).TopLeftCell.Row
                    Set cell = .Cells(J, gkiImage)
                    ' filename
                    gsPicture = ActiveWorkbook.Path & gksPath & _
                        .Cells(J, gkiName).Value & gksSuffix & gksExtension
                    If Dir(gsPicture) <> "" Then Kill gsPicture
                    ' copy image
                    .Shapes(I).CopyPicture
                    ' create chart
                    Debug.Print I; J, .Shapes(I).Width; .Shapes(I).Height
                    Set cht = .ChartObjects.Add(1, 1, _
                        .Shapes(I).Width * .Cells(J, gkiRatio).Value, _
                        .Shapes(I).Height * .Cells(J, gkiRatio).Value).Chart
                    ' paste on chart
                    cht.Paste
                    cht.ChartArea.Border.LineStyle = 0
                    ' export image & clean up
                    cht.Export gsPicture
                    cht.Parent.Delete
                    Application.CutCopyMode = False
                End If
            End If
        Next I
    End With
    ' end
    Set cht = Nothing
    Set cell = Nothing
    Beep
End Sub

It was finished yet when I realized that the extracted images had the size of the worksheet and not their original sizes. This part isn't finished yet, but give it a try. To get the worksheet size image set the constant gknUnknownFactor to 1 in the code.

Just advise if any issue. Will be back when solve that last issue.

Regards!
 

Attachments

Hi, Herman4352!

Better later than never... Well, as I didn't found it I did it again :(

Give a look at the uploaded file. It has the workbook and 4 test images. This is the code:
Code:
Option Explicit

' global constants
Const gksWS = "Hoja1"
Const gkiName = 1
Const gkiImage = 2
Const gkiRatio = 3
Const gksPath = "\"
Const gksSuffix = "_copy"
Const gksExtension = ".jpg"
Const gknUnknownFactor = 26.46

' global declarations
Dim cell As Range
Dim gsPicture As String, gnWidth As Single, gnHeight As Single

Sub LoadImages()
    ' constants
    ' declarations
    Dim I As Integer, pic As Object, nWidthW As Single, nHeightW As Single
    ' start
    '  clear shapes (images) in col gkiImage
    With Worksheets(gksWS)
        For I = .Shapes.Count To 1 Step -1
            If .Shapes(I).Type = msoLinkedPicture Then
                If .Shapes(I).Left >= .Cells(1, gkiImage).Left And _
                  .Shapes(I).Left < .Cells(1, gkiImage + 1).Left Then .Shapes(I).Delete
            End If
        Next I
    End With
    ' process
    I = 1
    With Worksheets(gksWS)
        Do Until .Cells(I, gkiName).Value = ""
            ' filename
            gsPicture = ActiveWorkbook.Path & gksPath & _
                .Cells(I, gkiName).Value & gksExtension
            If Dir(gsPicture) <> "" Then
                ' where
                Set cell = .Cells(I, gkiImage)
                ' aspect ratio
                Set pic = LoadPicture(gsPicture)
                nWidthW = pic.Width / gknUnknownFactor
                nHeightW = pic.Height / gknUnknownFactor
                Select Case nWidthW / nHeightW
                    Case Is > cell.Width / cell.Height
                        gnWidth = cell.Width
                        gnHeight = nHeightW * gnWidth / nWidthW
                        .Cells(I, gkiRatio).Value = (pic.Width / gknUnknownFactor) / cell.Width
                    Case Is <= cell.Width / cell.Height
                        gnHeight = cell.Height
                        gnWidth = nWidthW * gnHeight / nHeightW
                        .Cells(I, gkiRatio).Value = (pic.Height / gknUnknownFactor) / cell.Height
                End Select
                ' insert
                .Shapes.AddPicture gsPicture, True, True, _
                    cell.Left, cell.Top, gnWidth, gnHeight
            End If
            I = I + 1
        Loop
    End With
    ' end
    Set pic = Nothing
    Set cell = Nothing
    Beep
End Sub

Sub SaveImages()
    ' constants
    ' declarations
    Dim I As Integer, J As Integer, cht As Chart
    ' start
    ' process
    I = 1
    With Worksheets(gksWS)
        For I = .Shapes.Count To 1 Step -1
            If .Shapes(I).Type = msoLinkedPicture Then
                If .Shapes(I).Left >= .Cells(1, gkiImage).Left And _
                  .Shapes(I).Left < .Cells(1, gkiImage + 1).Left Then
                    ' cell
                    J = .Shapes(I).TopLeftCell.Row
                    Set cell = .Cells(J, gkiImage)
                    ' filename
                    gsPicture = ActiveWorkbook.Path & gksPath & _
                        .Cells(J, gkiName).Value & gksSuffix & gksExtension
                    If Dir(gsPicture) <> "" Then Kill gsPicture
                    ' copy image
                    .Shapes(I).CopyPicture
                    ' create chart
                    Debug.Print I; J, .Shapes(I).Width; .Shapes(I).Height
                    Set cht = .ChartObjects.Add(1, 1, _
                        .Shapes(I).Width * .Cells(J, gkiRatio).Value, _
                        .Shapes(I).Height * .Cells(J, gkiRatio).Value).Chart
                    ' paste on chart
                    cht.Paste
                    cht.ChartArea.Border.LineStyle = 0
                    ' export image & clean up
                    cht.Export gsPicture
                    cht.Parent.Delete
                    Application.CutCopyMode = False
                End If
            End If
        Next I
    End With
    ' end
    Set cht = Nothing
    Set cell = Nothing
    Beep
End Sub

It was finished yet when I realized that the extracted images had the size of the worksheet and not their original sizes. This part isn't finished yet, but give it a try. To get the worksheet size image set the constant gknUnknownFactor to 1 in the code.

Just advise if any issue. Will be back when solve that last issue.

Regards!


Hi

My name is Sorin and i am trying to use your code to to the exact same operation as described. The problem is that when i run the load photos i get this error message:
Compile error. Variable not defined.
The debugger points to the first Sub LoadImages()line.
Can you tell me what's wrong?

Many thanks in advance

Sorin
 
Back
Top