+ Reply to Thread
Results 1 to 3 of 3

Set picture box to change pic and reset code to do it again on click

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-29-2012
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    103

    Set picture box to change pic and reset code to do it again on click

    I've been using this code for months without any problems when suddenly, it stopped working and I really can't figure out what has changed because I didn't alter the code... I downloaded the latest Visual Studio's update and now it's not working... Anyways, it's for an Excel 2003 Macro that, when you click on the picture, allows you to select a new picture from your computer, resizes it to fit in a specific area, then gives the new picture the original code to do it all over again. Trouble is, it suddenly stopped working and I now get this error:

    Run Time Error '1004':

    Unable to set OnAction property of the Picture class
    Here is the picture code:

    Sub NewInsertMacro()
    
        Dim myPictureName As Variant
        Dim myPict As Picture
        Dim myRng As Range
        Dim myCurFolder As String
        Dim myNewFolder As String
    
        myCurFolder = CurDir
        UnProtectSheet
        YesNo = MsgBox("Do you want to delete the existing pic?", vbYesNo + vbCritical, "Hello")
        Select Case YesNo
        Case vbYes
            ActiveSheet.Pictures.Delete
        Case vbNo
            MsgBox "The next picture you select will overlap the existing pic"
        End Select
        On Error Resume Next
        ChDirNet myNewFolder
        If Err.Number <> 0 Then
            'what should happen
            MsgBox "Please change to your own folder"
            Err.Clear
        End If
        On Error GoTo 0
        myPictureName = Application.GetOpenFilename _
                        (filefilter:="PictureFiles,*.jpg;*.bmp;*.tif;*.gif")
    
        ChDirNet myCurFolder
    
        If myPictureName = False Then
            Exit Sub    'user hit cancel
        End If
        Range("BD17:CD42").Select
        Set myRng = Selection.Areas(1)
        Set myPict = myRng.Parent.Pictures.Insert(myPictureName)
        myPict.Top = myRng.Top
        myPict.Width = myRng.Width
        myPict.Height = myRng.Height
        myPict.Left = myRng.Left
        myPict.Placement = xlMoveAndSize
         myPict.OnAction = "'" & ThisWorkbook.Name & "'!NewInsertMacro"
        ProtectSheet
    End Sub
    I didn't write this code. On debug, it tells me this is the error code: myPict.OnAction = "'" & ThisWorkbook.Name & "'!NewInsertMacro"
    But like I said, it has worked for months, maybe even over a year without any issues, then suddenly when I download Visual Studios update since I upgraded from Express to the full version of Visual Studios, this code does not work and keep returning that error message. I tried selecting pictures from different areas, different type pictures, everything I can think of. I really don't have any idea what the problem might me. Any help much appreciated.

    BTW, I posted this on VBForums, but so far they can't figure it out.

  2. #2
    Forum Contributor
    Join Date
    08-29-2012
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    103

    Re: Set picture box to change pic and reset code to do it again on click

    I found the problem. It was actually super easy. I feel very stupid. I recently put my file inside a folder with brackets "[]" and apparently opening these types of files makes Excel have a heart attack. I figured this out when trying to assign manually re-assign my macro back to the pic only to get an error message about the formula being too complex, then looking that up on the net and finding out that meant my excel workbook was inside a folder with special symbols (which is was), at which point I moved it out and everything worked fine. I wonder if there is a way to check for error codes in VBA so I can catch this as an exception instead of entering Debug mode, which casual users would be totally stumped if they saw. But then again, casual users probably won't need to separate their folders with hash tags and brackets to identify them.

  3. #3
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Set picture box to change pic and reset code to do it again on click

    Hello Taemex,

    I wonder if there is a way to check for error codes in VBA so I can catch this as an exception instead of entering Debug mode
    Check out this site: http://www.consultdmw.com/excel-macr...r-handling.htm

    Please remember to mark your Thread as "Solved".
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1