We help IT Professionals succeed at work.

Updating an image in Word via VB6.

anne0952
anne0952 asked
on
Medium Priority
310 Views
Last Modified: 2008-02-26
Hi,

I have to programatically update an image contained in a word document. (Link to picture not working) Any suggestions will help.
PS. This word document will contain nothing else but this image but the image is a graph that is downloaded from the internet(have code to do this) and changes constantly. Please help. Will addpicture work, or do I need to first delete the old image in other to add the new image.

Thanks Anne.

Comment
Watch Question

One way might be to place an image control on the document and just change the image controls loadpicture property so it references a different image. Let me know if this is of interest.

This may help as well
http://www.vb-helper.com/howto_word_paste_picture.html
GrahamSkanRetired
CERTIFIED EXPERT
Top Expert 2012

Commented:
The exact code would depend on whether the picture in Inline or floating.

This code will delete and replace an Inline picture

Sub RefreshPicture(wdDoc As Word.Document, strFileName As String)
    Dim ilsh As Word.InlineShape
    Set ilsh = wdDoc.InlineShapes(1)
    ilsh.Delete
    wdDoc.InlineShapes.AddPicture strFileName
End Sub

Author

Commented:
Thank you for your speedy reply. I have the following code. I believe my image is an inline image, but I am not getting anything. The image is not being deleted or updated. Please help Anne


Private Function PictureToWord(strDoc As String, strDir2 As String) As Boolean

   
    On Error Resume Next
   
    Dim oWord As Word.Application
    Dim oDoc As Word.Document
    Dim ilsh As Word.InlineShape
    strDir2 = Combo2.Text
    strDoc = Combo3.Text
    oWord.Visible = False
   
    'Open a new word document for which you will insert the image file
   
    Set oDoc = oWord.Documents.Open(strDoc)
    Set ilsh = oDoc.InlineShapes(1)
    ilsh.Delete
   
    '/Now you insert the image in specific position
     
    oDoc.InlineShapes.AddPicture strDir2
   
    oDoc.SaveAs (strDoc)
   
    oWord.Visible = True

 


If Not Err.Number = 0 Then
        PictureToWord = False
    Else
        PictureToWord = True
    End If
   
    End Function
Retired
CERTIFIED EXPERT
Top Expert 2012
Commented:
Your code looks more-or-less OK, but you may be getting an error - which you are supressing and only checking at the end of the function, and even then you aren't testing.

I strongly suggest that you do not supress errors. You will learn more if they are just left unhandled. If you do want to cater for unavoidable errors or to have a graceful end to the program, then you can use a proper handling routine.

Also you didn't seem to be instantiating the Word Application. This code does use early binding, which means that you need to set a reference to the Word object library. However failing to do that would cause a compile-time error, so I guess that's already done.

I have put a test in the code below, to see if the existing document has an inline, a floating or no  picture. Obviously, you won't need that when your code is mature.

As an illustration, there is an error handling procedure, but it probably isn't needed.

I have squashed it up a bit vertically, so more code can be seen at once. Hopefully you won't find it unreadable.


Option Explicit

Private Sub Command1_Click()
    Dim strDocument As String
    Dim strImage As String
    'strImage = Combo2.Text
    'strDocument = Combo3.Text

'hard code the files to test
    strImage = "C:\Documents and Settings\User Name\My Documents\image002.jpg"
    strDocument = "C:\Documents and Settings\User Name\My Documents\Picture.doc"

    If PictureToWord(strDocument, strImage) Then
        MsgBox "Success"
    Else
        MsgBox "Failure"
    End If
End Sub
Private Function PictureToWord(strDoc As String, strDir2 As String) As Boolean
    Dim oWord As Word.application
    Dim oDoc As Word.Document
    Dim ilsh As Word.InlineShape
    Dim sh As Word.Shape
   
    'On Error Resume Next
    'strDir2 = Combo2.Text 'these values are passed in, so should be set in the calling procedure
    'strDoc = Combo3.Text
    On Error GoTo PictureToWordError
    Set oWord = New Word.application
    oWord.Visible = True ' set word visible until all developement bugs eliminated. Otherwise you can leave invisible instances of WinWord running
   
    'Open THE word document for which you will insert the image file
    Set oDoc = oWord.Documents.Open(strDoc)
    If oDoc.InlineShapes.Count > 0 Then
        Set ilsh = oDoc.InlineShapes(1)
        ilsh.Delete
        '/Now you insert the image in specific position
        oDoc.InlineShapes.AddPicture strDir2
        MsgBox "Inline picture changed"
    Else
        If oDoc.Shapes.Count > 0 Then
            Set sh = oDoc.Shapes(1)
            sh.Delete
            oDoc.Shapes.AddPicture strDir2
            MsgBox "Floating picture changed"
        Else
            oDoc.InlineShapes.AddPicture strDir2
            MsgBox "Inline picture added"
        End If
    End If
    oDoc.Save
    oDoc.Close
    oWord.Visible = True 'leave Word application open and visible
    'oWord.Quit 'this would close the application
    PictureToWord = True
PictureToWordExit:
    Exit Function
   
    'If Not Err.Number = 0 Then
        'PictureToWord = False
    'Else
        'PictureToWord = True
    'End If
PictureToWordError:
    Select Case Err.Number
        'Case 1234 'some known, but unavoidable error
            'Resume Next
        Case Else
            MsgBox "Error in PictureToWordExit function." & vbCrLf & Err.Description
            Resume PictureToWordExit
    End Select
End Function


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.