将图像添加到 Word 文档并使用 VBA 对其进行缩放

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

Add an Image to Word Document and Scale it using VBA

vbams-wordword-vba

提问by Kojof

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

如何使用 VBA 以编程方式将图像添加到 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?

我试过在 word 文档中添加书签并尝试添加图像,但它总是添加到表单的顶部而不是书签区域。我应该坚持使用书签还是有另一种方法来添加图像?

See my code below:

请参阅下面的代码:

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 Files\My Dropbox\dailystrategy.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:\temp\test.doc"

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

regards

问候

采纳答案by Todd Main

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?

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

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 Files\My Dropbox\dailystrategy.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:\temp\test.doc"
    End With
    wrdDoc.Close
    Set wrdDoc = Nothing
    wrdApp.Quit
    Set wrdApp = Nothing


EDIT: Jan 11, 2010 The above code was changed to include

编辑:2010 年 1 月 11 日以上代码已更改为包括

 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

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

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