View Full Version : [SOLVED:] Document Image Display in Userform Image Control
dbowlds
04-06-2018, 04:46 PM
Hello, I have a userform with an image control, 2 command buttons, and textbox. What I am trying to accomplish is a mechanism by which the user can easily go through all the images in a document, one by one, and view any existing Alternative Text that has been added to that image, modify/replace it, delete it, add new, etc.
When the user clicks on the first command button (NEXT IMAGE) the program finds the first image in the document and displays it in the userform's image control. (Is this possible?)
Any existing alternative text for that image is displayed in the textbox. The user then modifies the text in the textbox as desired and clicks on the 2nd command button (APPLY ALT TEXT) and the text is applied as alternative text to the image. Or, the user clicks clicks the NEXT IMAGE button, the second image is then displayed in the userform image control and the text box is updated to reflect that image's alt. text.
I have not been able to figure out how to get an inlineshape to display in a userform image control.
I believe I can use ActiveDocument.Selection.Shapes(1).AlternativeText = textbox.txt to assign the textbox contents.
If anyone can give me some advice on the image control issue I would be very appreciative.
Doug
gmaxey
04-06-2018, 06:16 PM
This code loads the first InlineShape and alternate text in the Userform image control:
Private Sub UserForm_Initialize()
Set Image1.Picture = LoadPicture(ActiveDocument.InlineShapes(1).LinkFormat.SourceFullName)
TextBox1.Text = ActiveDocument.InlineShapes(1).AlternativeText
End Sub
dbowlds
04-07-2018, 06:47 AM
Greg, thank you for the quick reply. When I try your code I receive a runtime error 91: Object variable or With block variable not set. I am unsure what object would need to be defined in this scenario. Any ideas?
gmaxey
04-08-2018, 05:50 AM
I suppose that means you don't have an inlineshape in the document. Try:
Private Sub UserForm_Initialize()
Set Image1.Picture = LoadPicture(ActiveDocument.Shapes(1).LinkFormat.SourceFullName)
TextBox1.Text = ActiveDocument.Shapes(1).AlternativeText
End Sub
dbowlds
04-08-2018, 09:01 AM
Hello Greg, I have been able to get the textbox to display the alternative text of the images. But I still get the same runtime error 91: Object variable or With block variable not set when I run the code shown below. I have a blank document with a sentence of text followed by an image, another sentence, another image, etc. for three total images. I have wrapped each of the three images differently as commented in the code below. i now understand that if you wrap an image in line with text VBA considers that image to be an InLineShape and if you wrap an image as tight or square with the text, VBA considers that image to be a Shape.
If I comment out the Set Image... line of code below, the three textboxes load the alternative text properly. Any suggestions as to what I am doing wrong trying to get the actual image to load into the userform1.image1.picture?
Private Sub UserForm_Initialize()
Set Image1.Picture = LoadPicture(ActiveDocument.Shapes(1).LinkFormat.SourceFullName) 'Note I get the same error if I use InLineShapes(1) or Shapes(1) in this line of code
TextBox1.Text = ActiveDocument.InlineShapes(1).AlternativeText '1st image in the document; wrapped in line with text
TextBox2.Text = ActiveDocument.Shapes(1).AlternativeText '2nd image in the document; wrapped as square
TextBox3.Text = ActiveDocument.Shapes(2).AlternativeText '3rd image in the document; wrapped as tight
End Sub
Doug
gmaxey
04-08-2018, 09:31 AM
My code assumes that the picture is "inserted and linked" to an external file (the image file path). Without that path, I don't think that LoadImage will work.
dbowlds
04-08-2018, 10:36 AM
OK, thanks. I just did some googling and discovered that it is not very easy to try to save the images from a document temporarily (to be able to load them as needed into the image control. It sounds like this is a non-starter.
Thanks,
Doug
gmaxey
04-08-2018, 11:09 AM
Maybe this would work:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 4/8/2018
Dim oILS As InlineShape
For Each oILS In ActiveDocument.InlineShapes
oILS.Range.Select
oILS.AlternativeText = InputBox("Review and edit existing alternate text", "Review", oILS.AlternativeText)
Next oILS
lbl_Exit:
Exit Sub
End Sub
dbowlds
04-09-2018, 05:48 AM
Greg! this is brilliant. This works perfectly provided the shape is formatted as wrapped in line with text. But it skips any images that are not-inline (but are wrapped square or tight). If I may ask one more favor of you. Is there also a way to step through the non-inline shapes and select them like you do for the inline shapes? For some reason VBA won't let me .range.select a non-InLineShape.
Doug
gmaxey
04-09-2018, 11:01 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 4/8/2018
Dim oILS As InlineShape
Dim oShp As Shape
For Each oILS In ActiveDocument.InlineShapes
oILS.Range.Select
oILS.AlternativeText = InputBox("Review and edit existing alternate text", "Review", oILS.AlternativeText)
Next oILS
For Each oShp In ActiveDocument.Shapes
oShp.Select
oShp.AlternativeText = InputBox("Review and edit existing alternate text", "Review", oShp.AlternativeText)
Next
lbl_Exit:
Exit Sub
End Sub
dbowlds
04-09-2018, 11:42 AM
I am so embarrassed.:blush I see why my attempt failed. For the non-InLineShapes I was trying to use use shape.range.select instead of just shape.select.
Your code works perfectly!
Thank you Greg! And thank you for your patience with me on this.
Marking this solved.
Doug
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.