Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Macro To Zoom A Picture

This is a discussion on Macro To Zoom A Picture within the Excel Questions forums, part of the Question Forums category; I found this macro that will zoom a picture; the problem is it never returns to the original size. Can ...

  1. #1
    Board Regular
    Join Date
    Dec 2010
    Location
    Ohio Someplace
    Posts
    957

    Default Macro To Zoom A Picture

    I found this macro that will zoom a picture; the problem is it never returns to the original size. Can this or a different macro zoom a picture, and then return the image to the original size after you click off? Or zoom if you hover over an image. I also may have the sheet protected, will it still work then? Thanks for any advice.

    Code:
    Sub Picture_Click()
    Set Shp = ActiveSheet.Shapes(Application.Caller)
    Shp.Select
    ActiveWindow.Zoom = 75
    End Sub

  2. #2
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    14,505

    Default Re: Macro To Zoom A Picture

    How about something like this? When you click on your picture, its size increases 50% (or some other chosen number). And when you again click on your picture, it returns to its original size. If so, insert the following code in the sheet module for the sheet containing the picture (right-click the sheet tab, and select 'View Code')...

    Code:
    Option Explicit
    
    Private Sub Picture_Click()
    
        Static Dict As Dictionary
        Static MyPics() As Variant
        Static Cnt As Long
        Static c As Long
        
        Dim Shp As Shape
        
        Cnt = Cnt + 1
        
        If Cnt = 1 Then
            Set Dict = CreateObject("Scripting.Dictionary")
        End If
        
        Set Shp = Me.Shapes(Application.Caller)
        
        If Not Dict.Exists(Shp.Name) Then
            c = c + 1
            Dict.Add Shp.Name, c
            ReDim Preserve MyPics(1 To 2, 1 To c)
            MyPics(1, c) = Shp.Name
            MyPics(2, c) = True
        End If
        
        If MyPics(2, Dict.Item(Shp.Name)) = True Then
            MyPics(2, Dict.Item(Shp.Name)) = False
            Shp.ScaleHeight 1.5, msoTrue 'increase height by 50%
            Shp.ScaleWidth 1.5, msoTrue 'increase width by 50%
        Else
           MyPics(2, Dict.Item(Shp.Name)) = True
            Shp.ScaleHeight 1, msoTrue 'scale to original height
            Shp.ScaleWidth 1, msoTrue 'scale to original width
        End If
    
    End Sub
    
    
    Now, assign 'Picture_Click' as the macro for your picture (right-click the picture, select 'Assign Macro', and select 'Picture_Click'). If you have more than one picture that you'd like to re-size, assign each picture with the macro 'Picture_Click'. If you have many pictures, you can use the following macro that needs to be place in a regular module to assign 'Picture_Click' to all pictures on your worksheet...

    Code:
    Sub AssignMacro()
        Dim Shp As Shape
        For Each Shp In Sheet1.Shapes 'change the sheet name accordingly
            If Shp.Type = 13 Then 'picture
                Shp.OnAction = "Sheet1.Picture_Click"
            End If
        Next Shp
    End Sub
    
    Lastly, in case you or some user saves the workbook when your picture or pictures are at an increased size, the following will ensure that they're re-sized to their original sizes when the workbook is opened...

    In a regular module:

    Code:
    Sub ResetToOriginalSize()
        Dim Shp As Shape
        For Each Shp In Sheet1.Shapes
            If Shp.Type = 13 Then 'picture
                Shp.ScaleHeight 1, msoTrue 'scale to original height
                Shp.ScaleWidth 1, msoTrue 'scale to original width
            End If
        Next Shp
    End Sub
    
    In the code module for 'ThisWorkbook' (double-click the icon for 'ThisWorkbook' in the Project Explorer window)...

    Code:
    Private Sub Workbook_Open()
        Call ResetToOriginalSize
    End Sub
    
    Domenic Tamburino
    Microsoft MVP - Excel
    xl-central.com - "For Your Microsoft Excel Solutions"

  3. #3
    New Member
    Join Date
    May 2013
    Posts
    1

    Default Re: Macro To Zoom A Picture

    most image software will return to the original size when you click the zoomed pictures. you just need to add some c# codes in it.

  4. #4
    New Member
    Join Date
    Apr 2014
    Posts
    1

    Default Re: Macro To Zoom A Picture

    Quote Originally Posted by aeimage View Post
    most image software will return to the original size when you click the zoomed pictures. you just need to add some c# codes in it.
    yes, you can build a project in Visual Studio and try the above sample codes to reduce and increase image size. hope it work.

  5. #5
    New Member
    Join Date
    May 2014
    Posts
    3

    Default Re: Macro To Zoom A Picture

    Domenic - I'm using Excel for Mac 2011. I tried this, but I'm getting an error when I click the picture. First, I don't have a "view code" when I right click the sheet tab so I went to Tools/Macro/Visual Basic Editor and pasted your code for "this workbook". I was testing with just your first block of code. The error I get is "Compile error. User defined type not defined." and its highlighting the line in the code "Static Dict As Dictionary". Any ideas how I can fix this?

  6. #6
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    14,505

    Default Re: Macro To Zoom A Picture

    Quote Originally Posted by gpadnos View Post
    Domenic - I'm using Excel for Mac 2011. I tried this, but I'm getting an error when I click the picture. First, I don't have a "view code" when I right click the sheet tab so I went to Tools/Macro/Visual Basic Editor and pasted your code for "this workbook". I was testing with just your first block of code. The error I get is "Compile error. User defined type not defined." and its highlighting the line in the code "Static Dict As Dictionary". Any ideas how I can fix this?
    That's probably because you'll need to set a reference. Actually, it looks like I neglected to mention it in my original post. So see if this helps...

    Code:
    Open the Visual Basic Editor (Alt+F11), if it's not already opened.
    Select Tools
    Select References
    Select/check Microsoft Scripting Runtime
    Select OK
    Quit the Visual Basic Editor (Alt+Q)
    Does this help
    Domenic Tamburino
    Microsoft MVP - Excel
    xl-central.com - "For Your Microsoft Excel Solutions"

  7. #7
    New Member
    Join Date
    May 2014
    Posts
    3

    Default Re: Macro To Zoom A Picture

    Thanks, that helped, but I'm still not quite there. Clearly I know nothing about Visual Basic.

    I'm trying on a PC with Excel 2003. On my Mac I don't have "Microsoft Scripting Runtime" and it seems its not available on a Mac. So this may just not work on a Mac unless I can find a Mac specific function.

    Now I'm getting past the original error point and getting the error "Compile Error: Invalid use of Me keyword". Its stopped on the line "Set Shp = Me.Shapes(Application.Caller)". Any suggestions for this one? I can try Excel 2007 at another computer if that's an issue.

  8. #8
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    14,505

    Default Re: Macro To Zoom A Picture

    Yeah, I don't use the Mac, so it may not be available. The code should be placed in the sheet module for the sheet containing the picture (right-click the sheet tab, select 'View Code', and paste your code there). Does this help?
    Domenic Tamburino
    Microsoft MVP - Excel
    xl-central.com - "For Your Microsoft Excel Solutions"

  9. #9
    New Member
    Join Date
    May 2014
    Posts
    3

    Default Re: Macro To Zoom A Picture

    I got on Excel 2007 and I'm having the same error (Invalid use of Me keyword). If I just right click the sheet name, choose View Code, paste your code, and assign the macro, I get an error that says "Cannot run the macro 'Book1!Picture1_Click'. So I go to Macros and create a Macro called "Picture_Click" and paste your code again (The top of this page says "Book1-Module1 (Code)" if that helps), it runs, but I get the error above.

  10. #10
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    14,505

    Default Re: Macro To Zoom A Picture

    I get an error that says "Cannot run the macro 'Book1!Picture1_Click'.
    You'll need to qualify the reference with the code name of the sheet. So, as before, right-click the sheet tab, select 'View Code', and enter the code in the sheet module. Then you'll notice the code name for the sheet at the top of the window. Let's say that it's 'Sheet1'. Now, when you right-click your picture to assign the macro, enter the macro name as...

    Sheet1.Picture1_Click
    Domenic Tamburino
    Microsoft MVP - Excel
    xl-central.com - "For Your Microsoft Excel Solutions"

Page 1 of 2 12 LastLast

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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


DMCA.com