Results 1 to 2 of 2

Thread: Replacing a selected image with clipboard contents

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2024
    Posts
    1

    Replacing a selected image with clipboard contents

    I have a document with hundreds of image placeholders that I need to replace with images from a source on the web. After doing "Copy image" on the web, I need to come back to MS Word, right-click the image, select "Change picture", then "From clipboard". I found the VB line below, but all it does is knock out the existing graphic and paste in the new, which does not retain the original dimensions. I need the original image properties/dimensions, and be able to just replace the contents with the clipboard. Ideally, I'd like to be able to assign this to a keyboard shortcut, so I don't have to go through multiple clicks over and over.

    Sub ChangePic()
    Selection.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, Placement:=wdInLine, DisplayAsIcon:=False
    End Sub

    I'd greatly appreciate any help.
    Last edited by geckoz100; Jul 24th, 2024 at 01:50 PM.

  2. #2
    Hyperactive Member
    Join Date
    Jul 2022
    Posts
    383

    Re: Replacing a selected image with clipboard contents

    This is likely not the best way to handle this:

    I added a module to a document and in it I have this code:

    Code:
    Public Sub ChangePic()
        
        If Selection.Type = wdSelectionInlineShape Then
            Dim currentImageHeight As Integer
            Dim currentImageWidth As Integer
            Dim inlineName As String
            Dim currentInlShape As InlineShape
            Dim newInlShape As InlineShape
                    
            ' get the size of the image
            Set currentInlShape = Selection.InlineShapes(1)
            currentImageHeight = currentInlShape.Height
            currentImageWidth = currentInlShape.Width
            
            ' paste the clipboard image
            Selection.PasteSpecial Link:=False, Placement:=wdInLine
            
            currentInlShape.Delete
            
            ' size the pasted image to what the other image was
            Set newInlShape = ThisDocument.InlineShapes(ThisDocument.InlineShapes.Count)
            newInlShape.Height = currentImageHeight
            newInlShape.Width = currentImageWidth
            Set newInlShape = Nothing
        End If
        
    End Sub
    I assigned the macro to SHIFT+ALT+V. Using these steps https://software-solutions-online.co...-key-to-macro/

    My document is super simple, but I hope this gives you a starting point. Don't forget to add error catching code to it.
    Attached Images Attached Images    

Posting Permissions

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



Click Here to Expand Forum to Full Width