使用 AddPicture Method word vba 调整图像属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20553813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:56:09  来源:igfitidea点击:

Adjust image properties with AddPicture Method word vba

vbams-wordword-vba

提问by user1783736

I need to edit the properties of an image inserted by AddPicture method.

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

1) I need to adjust the height to 0.5" and the width is variable (lock the aspect ratio).

1)我需要将高度调整为 0.5" 并且宽度是可变的(锁定纵横比)。

2) Wrap text = "In Front of Text"

2)Wrap text =“在文本前面”

Is this possile with this method? If so how do I add those properties? If not, what other method should I use and how?

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

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

回答by Kazimierz Jawor

I would do what you need in the following steps:

我会在以下步骤中做你需要的:

  1. instead of this line:

    Selection.InlineShapes.AddPicture FileName:=imageFullPath,  _
                               LinkToFile:=False, SaveWithDocument:=True
    
  2. I would do the same but using 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
    
  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