vba 使用 Microsoft Word 宏插入调整大小和重新定位图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9809475/
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-08 12:48:17  来源:igfitidea点击:

Insert resize and relocate image using a microsoft word macro

vbams-word

提问by ?????????s

I'm trying to write a very basic macro in VB for Microsoft Word, but I don't have the required knowledge.

我正在尝试在 VB 中为 Microsoft Word 编写一个非常基本的宏,但我没有所需的知识。

I simply need to do two things:

我只需要做两件事:

  1. Insert a picture from file
  2. Relocate it to the top right corner and resize it
  1. 从文件插入图片
  2. 将其重新定位到右上角并调整其大小

I can do the first task via the record new macro feature but I am unable to select move a picture while in recording mode so I need some VB code for this.

我可以通过录制新宏功能完成第一个任务,但我无法在录制模式下选择移动图片,因此我需要一些 VB 代码。

I already have this, so how do I move/resize the image?

我已经有了这个,那么如何移动/调整图像大小?

    Selection.InlineShapes.AddPicture FileName:= _
    "C:\Users\***\Pictures\**.jpg" _
    , LinkToFile:=False, SaveWithDocument:=True

回答by Denys Wessels

The AddPicture function has a number of parameters which include width and height which you can use to resize the image to the desired size.

AddPicture 函数有许多参数,包括宽度和高度,您可以使用它们将图像调整为所需的大小。

Please see an example below:

请看下面的例子:

Sub InsertImage()

    Dim imagePath As String
    imagePath = "C:\picture.jpg"

    ActiveDocument.Shapes.AddPicture FileName:=imagePath, _
    LinkToFile:=False, _
    SaveWithDocument:=True, _
    Left:=-5, _
    Top:=5, _
    Anchor:=Selection.Range, _
    Width:=20, _
    Height:=20

End Sub

Additionally, have a look at thismsdn article for an explanation of the AddPicture() function as well as the list of available parameters which you can pass to it.

此外,请查看这篇msdn 文章,了解 AddPicture() 函数以及您可以传递给它的可用参数列表。