【发布时间】:2019-10-14 15:13:10
【问题描述】:
Given 是图片的路径。如何使用VBA代码将图片添加到word文档中?
【问题讨论】:
-
请给我们相关代码好吗?
Given 是图片的路径。如何使用VBA代码将图片添加到word文档中?
【问题讨论】:
这就是将图片添加到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
结束子
【讨论】: