Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add an Image to Word Document and Scale it using VBA

Tags:

ms-word

vba

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

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?

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

like image 807
Kojof Avatar asked Jan 08 '10 18:01

Kojof


People also ask

How do you scale an image in Word?

To resize a picture, on the Picture Tools Format tab, in the Size group, enter the new measurements into the Height and Width boxes. To resize a shape or other object, on the Drawing Tools Format tab, in the Size group, enter the measurements you want into the Height and Width boxes.

How do I automatically update a picture in Word?

To insert your image into any Word document, go to the Insert tab on the Ribbon and then open the Quick Parts function. A drop-down will appear, where you will select Field. Step 3: Upon clicking Field, a new window populates with all the things you can insert. Select the Include Picture field.


1 Answers

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 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

 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 ScaleHeight and ScaleWidth to make it 50% smaller in both height and width.

like image 100
Todd Main Avatar answered Nov 30 '22 06:11

Todd Main