使用AddPicture方法字vba调整图像属性

时间:2013-12-12 20:42:21

标签: vba ms-word word-vba

我需要编辑AddPicture方法插入的图像的属性。

1)我需要将高度调整为0.5“,宽度可变(锁定纵横比)。

2)换行文字=“在文字前面”

这种方法有可能吗?如果是这样,我该如何添加这些属性?如果没有,我应该使用什么其他方法以及如何使用?

Sub replaceWithImage()

Dim imageFullPath As String
Dim FindText As String
imageFullPath = "C:\Logo.jpg"
FindText = "PlaceHolder"

'Application.ScreenUpdating = False
With Selection
    .HomeKey Unit:=wdStory

    With .Find
        .ClearFormatting
        .text = FindText
        ' Loop until Word can no longer
        ' find the search string, inserting the specified image at each location
        Do While .Execute
            Selection.MoveRight
            Selection.InlineShapes.AddPicture FileName:=imageFullPath, LinkToFile:=False, SaveWithDocument:=True
        Loop

    End With
End With


    End Sub

1 个答案:

答案 0 :(得分:4)

我会按照以下步骤做你需要的事情:

  1. 而不是这一行:

    Selection.InlineShapes.AddPicture FileName:=imageFullPath,  _
                               LinkToFile:=False, SaveWithDocument:=True
    
  2. 我会这样做,但使用Object Variable

    'a) create new shape as object variable
    Dim SHP 'As InlineShape/As Shape
    Set SHP = Selection.InlineShapes.AddPicture(FileName:=imageFullPath, _
                                LinkToFile:=False, _
                                SaveWithDocument:=True)
    'b) changes made according to SHP varialbe:
    With SHP
        'this will convert to 'in front of text'
        .ConvertToShape
        'this will keep ratio
        .LockAspectRatio = msoTrue
        'this will adjust width to 0.5 inch
        .Width = InchesToPoints(0.5)
    End With