Help with Images/Alternative Text/VBA

djm51

New Member
Joined
Sep 1, 2010
Messages
9
Hello,

I have a excel spreadsheet which has many images (500+) in one column, each row. Each image represents a status (there are approx 5 different images). As I would like to be able to sort the spreadsheet, I was wondering if there was any way to convert the image to text. I did some research and the only way I could see is maybe using the alternative text (each png file has different alternative txt depending on the image). I cant find any way just to display the alternative text instead of the image in each cell, so I guess a macro is the best way

When I select on image it says the pictures name is "Picture 1", therefore I would like a macro what selects Picture 1, gets the alternative text, and pasts it in column I, then moves on to "Picture 2" and so on, till they are all done. Not every row has an image (eg. it could be rows 10-30, 50-75...)

Any ideas?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try the sample below. I believe it is what you are asking for.

Hope it helps.

Gary

Code:
Dim oShape As Shape
Dim oTopLeft As Range

For Each oShape In ActiveSheet.Shapes
    If oShape.Type = msoPicture Then
        Set oTopLeft = oShape.TopLeftCell
        ActiveSheet.Range("I" & oTopLeft.Row).Value = oShape.Name
        ActiveSheet.Range("J" & oTopLeft.Row).Value = oShape.AlternativeText
    End If
Next oShape
 
Upvote 0
Hi Gary,

I posted in the code and ran it but nothing changed (eg. no errors or outoput)

Any additional ideas?


Pictures = images
Source:

Col A Col B Col E
Numb Type Status 2
1 b Picture 1
1 b Picture 2
1 d Picture 3
.........
...........
1 c Picture 4


Output needed:

Col A Col B Col E Col I
Numb Type Status 2 Alt Text
1 b Picture 1 Completed
1 b Picture 2 Pending
1 d Picture 3 Completed
.........
...........
1 c Picture 4 Modified







-- removed inline image ---
 
Upvote 0
Where did you paste the code?

Did the sheet that was active when you ran the code have any pictures in it? That is pictures inserted from the Excel main menu:

Insert>Picture>From File ...

Gary
 
Upvote 0
Hi Gary,

I hit Alt + F11

Made a module then a procedure. Then posted the following code:

Public Sub Alttext()
Dim oShape As Shape
Dim oTopLeft As Range

For Each oShape In ActiveSheet.Shapes
If oShape.Type = msoPicture Then
Set oTopLeft = oShape.TopLeftCell
ActiveSheet.Range("I" & oTopLeft.Row).Value = oShape.Name
ActiveSheet.Range("J" & oTopLeft.Row).Value = oShape.AlternativeText
End If
Next oShape
End Sub

I save then go back to excel,l run the macro and nothing happens.

I believe the sheet is active and that all the pics are part of the workbook.

Is there a way I can attach a file as an example for you?

Thanks
 
Upvote 0
Looks like you got it in a module OK.

The code works for me as is. I'd try a brand new workbook, paste the code into a new module just like you did before. Add some images from the "Insert" menu and run it again. It should put the name of the picture in column I in the same row as the top left corner of the picture.

The pictures do not have to fit in the cells. It's using the TopLeft corner only.

Gary
 
Upvote 0
The only thing I can think of that would stop it from working is the shape type check (for msoPicture). It must be something else.

The following sample has the "type check" commented out. It should print an integer in the debug window for the type of each shape on the active worksheet. msoPicture is 13. You can actually use the integer (13) in place of the name msoPicture or whatever else it might be.

Gary

Code:
Public Sub Test()

Dim oShape As Shape
Dim oTopLeft As Range

For Each oShape In ActiveSheet.Shapes
    Debug.Print oShape.Type
    'If oShape.Type = msoPicture Then
        Set oTopLeft = oShape.TopLeftCell
        ActiveSheet.Range("I" & oTopLeft.Row).Value = oShape.Name
        ActiveSheet.Range("J" & oTopLeft.Row).Value = oShape.AlternativeText
    'End If
Next oShape

End Sub
 
Upvote 0
Glad to hear you got it working. What was the problem on the first try? Maybe I can be a little clearer in future replies to others?

Gary
 
Upvote 0
I cant really tell you, just that the second code works which is great! (maybe something to do with the picture format).

One more quick question just if it is possible.

The output in column I looks like this:

C:\Program Files\XXXXX\XXX\Finished_18_14.png

I would like it to say only "Finished" in the column. Due to the fact there are different status', the length changes. I did it in Excel, but would like to have VBA which does it automatically.

Using excel, I found to get rid of the C"\...." it goes to 53 characters. I then used a FIND function for the "_", then used MID function as such... =MID(J14,54,(P14-54)) where P14 = the number of characters to the "_" this seems to work just dont know how to do it in VBA.

Also, If possible I would like to select all the pics/objects on the active sheets, delete them then copy column I and paste it into column E.

Is this easy?

Thanks so much for all your help!
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top