View Single Post
 
Old 01-13-2018, 05:47 PM
puff puff is offline Windows 7 64bit Office 2013
Advanced Beginner
 
Join Date: Apr 2017
Posts: 60
puff is on a distinguished road
Question VBA to insert an image and centralize it (code included)

Hi all. My target is fairly simple and it's just like use alt+I to open the insert image dialog, choose an image to insert and then centralize the image. My code so far:

Code:
Sub InsertPic()
    Dim oDialog As Dialog
    Dim strFile As String
    Dim oImage As Object
        Set oDialog = Dialogs(wdDialogInsertPicture)
        With oDialog
            .Display
            If .Name <> "" Then
                strFile = .Name
            End If
        End With
        
        Set oImage = Selection.InlineShapes.AddPicture(strFile)
        With oImage
            .LockAspectRatio = msoTrue
            .Height = 0.5 * .Height
            .Width = 0.5 * .Width 
            Set oRng = .ConvertToShape
        End With
        
        With oRng
            oRng.Select
            Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
        End With
    Set oDialog = Nothing
    Set oImage = Nothing
    Set oRng = Nothing
End Sub
Questions:
1. The image inserted by my code has that blue anchor on the upper left corner and the centralizing part doesn't work. How can I get rid of that and make the image embedded in the texts to centralize the image? I think I messed up the difference between inline shape and shape objects.

2. Is that possible for me to set a default file location when the insert image dialog is open? For example, C:\Users\atom\Desktop\PDF

3. My code also includes a part where I want to resize the picture to 50% after insertion, but the resulting image is super small. Did I make a mistake there?

4. What changes do I need to make if I want to insert all the images from a file and process them as described above?

Thank you very much for your consideration. Any improvement of the original code is also welcomed.
Reply With Quote