Phil Filippak
Phil Filippak

Reputation: 673

How to add picture in Word document and adjust its size and position?

The setup is: an Excel table with some usernames and filenames (as files are photos in some directory). The aim is to create a Word document based on data from selected row by changing variables in template to real username and adding photo to it. The trouble is in positioning and setting properties of that photo. I used Selection.InlineShapes.AddPicture method due to Selection.Shapes.AddPicture method returned error (Run-time error '438': Object doesn't support this property or method) to me. So, the following is my actual code, and I hope someone could help me. Thanks in advance!

Option Explicit

Sub CreateDocs()

    Const wdReplaceAll = 2

    Dim user_name As String, user_surname As String, user_patronymic As String
    Dim user_type As String, user_type_num As Integer, user_country As String
    Dim user_pic As String, pic As Object
    Dim wrd As Object, doc As Object
    Dim length As Integer
    Dim ind As Integer

    Dim pict As Object

    ind = ActiveCell.Row

    With Sheets("SHEET_NAME")
        user_name = .Cells(ind, 4)
        user_surname = .Cells(ind, 3)
        user_type = .Cells(ind, 22)
        user_pic = .Cells(ind, 25)
    End With

    Set wrd = CreateObject("Word.Application")
    wrd.Visible = True

    Set doc = wrd.Documents.Add(ThisWorkbook.Path & "\SUBPATH\TMPL.dotx")

    Set pic = wrd.Selection.InlineShapes.AddPicture( _
        Filename:=ThisWorkbook.Path & "\SUBPATH\" & user_pic, _
        LinkToFile:=False, _
        SaveWithDocument:=True _
    )
    pic.ConvertToShape
      ' THE NEXT 4 CODE LINES DOESN'T WORK AT ALL
      ' I have the same error here:
      ' Run-time error '438': Object doesn't support this property or method
    pic.LockAspectRatio = msoTrue
    pic.Left = 197
    pic.Top = 191
    pic.Width = 179

    With wrd.Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "%user_name%"
        .Replacement.Text = user_name
        .Execute Replace:=wdReplaceAll
        .Text = "%user_surname%"
        .Replacement.Text = user_surname
        .Execute Replace:=wdReplaceAll
        .Text = "%user_type%"
        .Replacement.Text = user_type
        .Execute Replace:=wdReplaceAll
    End With

    doc.SaveAs ThisWorkbook.Path & "\SUBPATH\" & user_name & ".docx"

    doc.Close False
    Set doc = Nothing

    wrd.Quit False
    Set wrd = Nothing

End Sub

Upvotes: 1

Views: 4369

Answers (1)

Christina
Christina

Reputation: 1379

Try creating another variable (let's call it picShape) and setting it to the result of ConverttoShape. So,

Dim picShape As Object
.....
Set picShape = pic.ConvertToShape
picShape.LockAspectRatio = msoTrue
picShape.Left = 197
picShape.Top = 191
picShape.Width = 179

I wish I could provide a fuller explanation for this, but I rarely work with late-binding. From the looks of the Local window, it doesn't seem like pic.ConvertToShape actually changes the underlying type of pic (although it does change the actual picture from an inlineshape to a shape). So, either you can't change the type at that point, or this method does not affect the variable to which it is applied in the way you might expect.

Upvotes: 2

Related Questions