Maldred
Maldred

Reputation: 1104

Unlock Aspect Ratio from Prompted Input

I'm getting a compile error on img.LockAspectRatio = msoFalse. All I'm trying to do is unlock the aspect ratio of the image that is being imported from the user. I would assume I'm not using the proper syntax, any help would be great

Sub ChangeImage()
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .ButtonName = "Submit"
        .Title = "Select an image file"
        .Filters.Clear
        .Filters.Add "All Pictures", "*.*"

        If .Show = -1 Then
            Dim img As Object
            Set img = ActiveSheet.Pictures.Insert(.SelectedItems(1))

            img.LockAspectRatio = msoFalse

            img.Left = 141
            img.Top = 925

            img.Width = 600
            img.Height = 251
        Else
            Debug.Print "Prompt closed"
        End If
    End With
End Sub

Upvotes: 0

Views: 96

Answers (1)

cyboashu
cyboashu

Reputation: 10433

As you are trying to insert picture, declare the type as Picture and not as Object. Use LockAspectRatio is available under the ShapeRange property.

Sub ChangeImage()
        With Application.FileDialog(msoFileDialogFilePicker)
            .AllowMultiSelect = False
            .ButtonName = "Submit"
            .Title = "Select an image file"
            .Filters.Clear
            .Filters.Add "All Pictures", "*.*"

            If .Show = -1 Then
                Dim img As Picture '/ Declare as Picture
                Set img = ActiveSheet.Pictures.Insert(.SelectedItems(1))


               '/ Use ShapeRange to toogle aspect ratio
               img.ShapeRange.LockAspectRatio = msoFalse


                img.Left = 141
                img.Top = 925

                img.Width = 600
                img.Height = 251
            Else
                Debug.Print "Prompt closed"
            End If
        End With
    End Sub

Upvotes: 1

Related Questions