Consulting

Results 1 to 11 of 11

Thread: Document Image Display in Userform Image Control

  1. #1
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    68
    Location

    Document Image Display in Userform Image Control

    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

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    68
    Location
    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?

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    68
    Location
    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

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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.
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    68
    Location
    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

  8. #8
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  9. #9
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    68
    Location
    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

  10. #10
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  11. #11
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    68
    Location
    I am so embarrassed. 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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •