Using VBA to change Picture

后端 未结 10 1002
庸人自扰
庸人自扰 2020-12-10 12:41

I am trying to use VBA to automate the Change Picture function when you right click a Shape in Excel/Word/Powerpoint.

However, I am not able to find any reference, c

相关标签:
10条回答
  • 2020-12-10 12:49

    In Word 2010 VBA it helps to change the .visible option for that picture element you want to change.

    1. set the .visible to false
    2. change the picture
    3. set the .visilbe to true

    that worked for me.

    0 讨论(0)
  • 2020-12-10 12:50

    I tried to imitate the original function of 'Change Picture' with VBA in PowerPoinT(PPT)

    The code below tries to recover following properties of the original picture: - .Left, .Top, .Width, .Height - zOrder - Shape Name - HyperLink/ Action Settings - Animation Effects

    Option Explicit
    
    Sub ChangePicture()
    
        Dim sld As Slide
        Dim pic As Shape, shp As Shape
        Dim x As Single, y As Single, w As Single, h As Single
        Dim PrevName As String
        Dim z As Long
        Dim actions As ActionSettings
        Dim HasAnim As Boolean
        Dim PictureFile As String
        Dim i As Long
    
        On Error GoTo ErrExit:
        If ActiveWindow.Selection.Type <> ppSelectionShapes Then MsgBox "Select a picture first": Exit Sub
        Set pic = ActiveWindow.Selection.ShapeRange(1)
        On Error GoTo 0
    
        'Open FileDialog
        With Application.FileDialog(msoFileDialogFilePicker)
            .AllowMultiSelect = False
            .Filters.Clear
            .Filters.Add "Picture File", "*.emf;*.jpg;*.png;*.gif;*.bmp"
            .InitialFileName = ActivePresentation.Path & "\"
            If .Show Then PictureFile = .SelectedItems(1) Else Exit Sub
        End With
    
        'save some properties of the original picture
        x = pic.Left
        y = pic.Top
        w = pic.Width
        h = pic.Height
        PrevName = pic.Name
        z = pic.ZOrderPosition
        Set actions = pic.ActionSettings    'Hyperlink and action settings
        Set sld = pic.Parent
        If Not sld.TimeLine.MainSequence.FindFirstAnimationFor(pic) Is Nothing Then
            pic.PickupAnimation 'animation effect <- only supported in ver 2010 above
            HasAnim = True
        End If
    
        'insert new picture on the slide
        Set shp = sld.Shapes.AddPicture(PictureFile, False, True, x, y)
    
        'recover original property
        With shp
            .Name = "Copied_ " & PrevName
    
            .LockAspectRatio = False
            .Width = w
            .Height = h
    
            If HasAnim Then .ApplyAnimation 'recover animation effects
    
            'recover shape order
            .ZOrder msoSendToBack
            While .ZOrderPosition < z
                .ZOrder msoBringForward
            Wend
    
            'recover actions
            For i = 1 To actions.Count
                .ActionSettings(i).action = actions(i).action
                .ActionSettings(i).Run = actions(i).Run
                .ActionSettings(i).Hyperlink.Address = actions(i).Hyperlink.Address
                .ActionSettings(i).Hyperlink.SubAddress = actions(i).Hyperlink.SubAddress
            Next i
    
        End With
    
        'delete the old one
        pic.Delete
        shp.Name = Mid(shp.Name, 8)  'recover name
    
    ErrExit:
        Set shp = Nothing
        Set pic = Nothing
        Set sld = Nothing
    
    End Sub
    

    How to use: I suggest you to add this macro into the Quick Access Toolbar list. (Goto Option or Right-click on the Ribbon menu)) First, select a Picture on the slide which you want to change. Then, if the FileDialog window opens, choose a new picture. It's done. By using this method, you can bypass the 'Bing Search and One-Drive Window' in ver 2016 when you want to change a picture.

    In the code, there might(or should) be some mistakes or something missing. I'd appreciate it if somebody or any moderator correct those errors in the code. But mostly, I found that it works fine. Also, I admit that there are still more properties of the original shape to recover - like the line property of the shape, transparency, pictureformat and so on. I think this can be a beginning for people who want to duplicate those TOO MANY properties of a shape. I hope this is helpful to somebody.

    0 讨论(0)
  • 2020-12-10 12:52

    So far as I know you can't change the source of a picture, you need to delete the old one and insert a new one

    Here's a start

    strPic ="Picture Name"
    Set shp = ws.Shapes(strPic)
    
    'Capture properties of exisitng picture such as location and size
    With shp
        t = .Top
        l = .Left
        h = .Height
        w = .Width
    End With
    
    ws.Shapes(strPic).Delete
    
    Set shp = ws.Shapes.AddPicture("Y:\our\Picture\Path\And\File.Name", msoFalse, msoTrue, l, t, w, h)
    shp.Name = strPic
    shp.ScaleHeight Factor:=1, RelativeToOriginalSize:=msoTrue
    shp.ScaleWidth Factor:=1, RelativeToOriginalSize:=msoTrue
    
    0 讨论(0)
  • 2020-12-10 12:52
    'change picture without change image size
    Sub change_picture()
    strPic = "Picture 1"
    Set shp = Worksheets(1).Shapes(strPic)
    
    'Capture properties of exisitng picture such as location and size
    With shp
        t = .Top
        l = .Left
        h = .Height
        w = .Width
    End With
    
    Worksheets(1).Shapes(strPic).Delete
    
    Set shp = Worksheets(1).Shapes.AddPicture("d:\pic\1.png", msoFalse, msoTrue, l, t, w, h)
    shp.Name = strPic
    
    End Sub
    
    0 讨论(0)
  • 2020-12-10 12:53

    What I've done in the past is create several image controls on the form and lay them on top of each other. Then you programmatically set all images .visible = false except the one you want to show.

    0 讨论(0)
  • 2020-12-10 12:54

    what I do is lay both images on top of eachother, and assign the macro below to both images. Obviously i've named the images "lighton" and "lightoff", so make sure you change that to your images.

    Sub lightonoff()
    
    If ActiveSheet.Shapes.Range(Array("lighton")).Visible = False Then
        ActiveSheet.Shapes.Range(Array("lighton")).Visible = True
            Else
        ActiveSheet.Shapes.Range(Array("lighton")).Visible = False
        End If
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题