Austin R
Austin R

Reputation: 795

Scrollable image in userform

I have a word doc with a bunch of ActiveX Control buttons or whatever on it, and each time a button is clicked, a corresponding image needs to be displayed in a popup box.

I have a userform called ImageForm, and this is what I'm doing right now:

Sub Button_Clicked()
    ImageForm.Picture = LoadPicture("appropriate_image_path")
    ImageForm.Show
End Sub

Each of these images has a width of 8.5 inches, but their heights can vary anywhere from like 3 to 20 inches (they're snippets of a pdf). So I've set the width of the userform to a little more than 8.5 inches, and that looks fine. But I need to be able to scroll vertically through the image in the userform, since some of the images could be taller than a user's monitor.

I'm completely stuck on this. What I've tried so far is adding a frame to the form, then adding an image control inside the form, and setting the "ScrollBars" property of the frame to vertical. Then instead of using "ImageForm.Picture = ..." I use "ImageForm.ImageControl.Picture = ..." But it doesn't work.

Any insight here would be greatly appreciated. Hopefully this question is clear enough, I've only been using VBA for a month or so now. (I miss Java so, so much)

Thanks!

Upvotes: 3

Views: 11541

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149315

Here is a neat little trick based on one of my posts

The idea is to ensure that the image control is in frame control and the image control doesn't have a border. Also the image control's PictureSizeMode is set to fmPictureSizeModeClip so that we can scroll the image

SNAPSHOT (DESIGN TIME)

enter image description here

SNAPSHOT (RUN TIME)

enter image description here

CODE

Private Sub UserForm_Initialize()
    With Frame1
        '~~> This will create a vertical scrollbar
        .ScrollBars = fmScrollBarsVertical

        '~~> Change the values of 2 as Per your requirements
        .ScrollHeight = .InsideHeight * 2
        .ScrollWidth = .InsideWidth * 9
    End With

    With Image1
        .Picture = LoadPicture("C:\Users\Public\Pictures\Sample Pictures\Desert.jpg")
        .BorderStyle = fmBorderStyleNone
        .PictureSizeMode = fmPictureSizeModeClip
    End With
End Sub

Upvotes: 5

Related Questions