【问题标题】:Insert Image into Word by using VBA使用 VBA 将图像插入 Word
【发布时间】:2019-10-14 15:13:10
【问题描述】:

Given 是图片的路径。如何使用VBA代码将图片添加到word文档中?

【问题讨论】:

  • 请给我们相关代码好吗?

标签: vba ms-access ms-word


【解决方案1】:

这就是将图片添加到word文档的概念

创建一个模板文档,比如说 c:\path\file.docx

在你喜欢的地方添加一张图片(这将是保存新图片的框架)

选择图像并插入一个书签并将其命名为“someBookmarkName”。

现在从访问中使用此代码

Sub insertImageToWord()
Dim Word As Object
Dim doc As Object
Dim filePath As String: filePath = "c:\path\file.docx"
Dim SHP As Object
Dim strTmp As String: strTmp = "someBookmarkName"
Dim strPath As String: strPath = "c:\path\image_file.png"

Set Word = CreateObject("Word.Application")
Set doc = Word.Documents.Open(filePath)
Set SHP = doc.Bookmarks(strTmp).Range.InlineShapes.AddPicture(Filename:=strPath, _
    LinkToFile:=False, _
    SaveWithDocument:=True)
With SHP
    'this will keep ratio
    '   .WrapFormat.type = 1  'wdWrapTight
    '   .WrapFormat.type = 7  'wdWrapInline
    .LockAspectRatio = -1    ' msoTrue
    'this will adjust width to 0.5 inch
    '.Width = wd.InchesToPoints(2.5)
    ' .Width = wd.CentimetersToPoints(2.66) * 2.5
    ' .Height = wd.CentimetersToPoints(3.27) * 2.5
    '   .ScaleHeight = 150
End With

结束子

【讨论】:

  • 你好,xShen。基本上你的代码是有效的。有一个问题,已添加书签的图片仍在新文档中。我将尝试增强解决方案,即使用书签图片的比例。
  • 嗨,开发者。我查看了使用这种技术的旧 word 文档,那里的框架是一个文本框。所以尝试从图片切换到文本框。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多