简体   繁体   English

使用 VBA 将图像插入 Word

[英]Insert Image into Word by using VBA

Given is the path of a picture.给定的是图片的路径。 How can I add the picture to a word document by using VBA code?如何使用 VBA 代码将图片添加到 word 文档中?

This is the concept of adding image to word document.这是将图像添加到word文档的概念

Create a template document,let say in c:\path\file.docx创建一个模板文档,比如说 c:\path\file.docx

add an image where ever you like (this will be the frame to hold the new image )在您喜欢的地方添加图像(这将是保存新图像的框架)

select the image and insert a bookmark and name it something like "someBookmarkName". select 图像并插入书签并将其命名为“someBookmarkName”。

And now from access use this code现在从访问中使用此代码

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

End Sub结束子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM