将图像添加到 Word 文档并使用 VBA 对其进行缩放 [英] Add an Image to Word Document and Scale it using VBA

查看:23
本文介绍了将图像添加到 Word 文档并使用 VBA 对其进行缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用 VBA 以编程方式将图像添加到 Word 文档中.

how do i programatically add an image using VBA to a word document.

我已经尝试向 Word 文档添加书签并尝试添加图像,但它总是添加到表单的顶部而不是书签区域.我应该坚持使用书签还是有其他方法可以添加图片?

I've tried adding a bookmark to the word document and tried adding the image, but it always adds to the top of the form rather than the bookmark area. Should i persevere with the bookmark or is there another way to add the image?

请参阅下面的代码:

Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")

Dim objWdRange As Word.Range
Dim GraphImage As String
Dim shortString As String 

shortString = Range("short").Value

GraphImage = "http://xxx.xxxxx.com/xxx/xxx.png?instrument=Image.png"

wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("C:Program FilesMy Dropboxdailystrategy.doc")

Set objWdRange = wrdDoc.Content

With wrdDoc


    If wrdDoc.Bookmarks.Exists("shortString ") Then
        wrdDoc.Bookmarks("shortString ").Range.Text = shortString 
    End If      

     If wrdDoc.Bookmarks.Exists("GraphImage") Then
        wrdDoc.Bookmarks("GraphImage").Range.InlineShapes.AddPicture Filename:=GraphImage, LinkToFile:=False, SaveWithDocument:=True
     End If


    wrdDoc.SaveAs "c:	emp	est.doc"

  ' close the document
    Set wrdDoc = Nothing
    Set wrdApp = Nothing
End With

问候

推荐答案

好吧,首先我们需要稍微清理一下您的代码,如下所示.这在我的网站上运行良好 - 它将图像放在 GraphicImage 书签的前面,而不是文档的顶部 - 但也许你的图像太大以至于延伸到顶部?

Well, first we need to clean up your code a bit, like below. This runs fine on my site - it places the image right at the front of the GraphicImage bookmark, not at the top of the document - but maybe your image is so large it extends to the top?

Dim objWdRange As Word.Range
Dim GraphImage As String
Dim shortString As String
shortString = Range("short").Value '? don't know what this is for
GraphImage = "http://xxx.xxxxx.com/xxx/xxx.png?instrument=Image.png"
wrdApp.Visible = True
    Set wrdDoc = wrdApp.Documents.Open("C:Program FilesMy Dropboxdailystrategy.doc")
    Set objWdRange = wrdDoc.Content '? don't know what this is for
    With wrdDoc
        If .Bookmarks.Exists("shortString ") Then
           .Bookmarks("shortString ").Range.Text = shortString
        End If
     If .Bookmarks.Exists("GraphImage") Then
         Dim wrdPic As Word.InlineShape
         Set wrdPic = .Bookmarks("GraphImage").Range.InlineShapes.AddPicture(FileName:=GraphImage, LinkToFile:=False, SaveWithDocument:=True)
         wrdPic.ScaleHeight = 50
         wrdPic.ScaleWidth = 50
     End If
       .SaveAs "c:	emp	est.doc"
    End With
    wrdDoc.Close
    Set wrdDoc = Nothing
    wrdApp.Quit
    Set wrdApp = Nothing

<小时>

2010 年 1 月 11 日上面的代码改为include


Jan 11, 2010 The above code was changed to include

 If .Bookmarks.Exists("GraphImage") Then
 Dim wrdPic As Word.InlineShape
 Set wrdPic = .Bookmarks("GraphImage").Range.InlineShapes.AddPicture(FileName:=GraphImage, LinkToFile:=False, SaveWithDocument:=True)
    wrdPic.ScaleHeight = 50
    wrdPic.ScaleWidth = 50
 End If

这将图片设置为对象,然后使用缩放方法 ScaleHeightScaleWidth 使其高度和宽度都缩小 50%.

This sets the picture as an object and then uses the scaling methods ScaleHeight and ScaleWidth to make it 50% smaller in both height and width.

这篇关于将图像添加到 Word 文档并使用 VBA 对其进行缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆