【问题标题】:Add an Image to Word Document and Scale it using VBA将图像添加到 Word 文档并使用 VBA 对其进行缩放
【发布时间】:2011-01-03 00:53:20
【问题描述】:

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

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

请参阅下面的代码:

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

问候

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    好吧,首先我们需要稍微清理一下您的代码,如下所示。这在我的网站上运行良好 - 它将图像放在 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
    

    编辑: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
    

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

    【讨论】:

    • 您好,谢谢您,您是对的,图像被插入到正确的位置,但是它的尺寸对于页面来说太大了。是否可以使用 VBA 以编程方式设置图像尺寸?
    • 是的,你可以。您可以使用内联形状的高度/宽度属性,也可以使用 ScaleHeight/ScaleWidth,因为我已经更新了上面的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多