Error Handling - Error 400 when cancelling dialogue box mid-code RRS feed

  • Question

  • Hello,

    I'm trying to create a button which allows users to upload images onto a form as an OLE option/attachment. The code itself is working fine, except for when they click cancel mid-dialogue (because they are done selecting images and want to exit the windows explorer window) then i get error code 400. If you're wondering why they'd ever click cancel mid-dialogue is because I've put the function on a loop, so they can upload  multiple images into a range, as stated in the code. It keeps going until the ranges are full. 

    But if they only decide to upload 1 image (or a number of images less than the size of the range), then they have to close the file explorer and this causes the error 400 to show up. I'm trying to find a way to work around this error so it doesn't show... or get it to display a different message? (such as "Cancelled").... but i can't seem to figure it out. is there a way to do this? Or if possible is there a work around that ends the statement if they press the "cancel" or "x" button in the file explorer? Otherwise it continues. 

    Please let me know if this is clear!


    here is the code:


    Sub InsertImage()

        Dim vFile As String
        Dim objI As Object
        Dim rngI As Range

          For Each rngI In Range("H14:H19")    'make range any size, or use Selection for flexibility
            vFile = Application.GetOpenFilename("All Files,*.*", Title:="Please Insert a screenshot of your issue")

    ''this is where you can press cancel or x and see the error 400 dialogue box come up. 

            Set objI = ActiveSheet.OLEObjects.Add(Filename:=vFile, Link:=False, DisplayAsIcon:=True, IconLabel:=vFile)
            objI.Left = rngI.Left
            objI.Top = rngI.Top
            objI.Border.LineStyle = xlLineStyleNone
            objI.ShapeRange.Fill.Transparency = 1
            objI.Height = rngI.Height
            objI.Width = rngI.Width
            If objI.Height > rngI.Height Then
                objI.Height = rngI.Height
            End If
            If objI.Height < rngI.Height Then
                objI.Height = rngI.Height
            End If

     Next rngI

    End Sub

    Friday, June 1, 2018 7:17 PM


  • Below the line

        vFile = Application.GetOpenFilename(…)


        If vFile = "False" Then Exit Sub

    Regards, Hans Vogelaar (

    Friday, June 1, 2018 9:25 PM

All replies