Updating an image in Word via VB6.

Posted on 2006-03-29
Last Modified: 2008-02-26

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.

Question by:anne0952
    LVL 15

    Expert Comment

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

    Expert Comment

    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)
        wdDoc.InlineShapes.AddPicture strFileName
    End Sub


    Author Comment

    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)
        '/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
            PictureToWord = True
        End If
        End Function
    LVL 76

    Accepted Solution

    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"
            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)
            '/Now you insert the image in specific position
            oDoc.InlineShapes.AddPicture strDir2
            MsgBox "Inline picture changed"
            If oDoc.Shapes.Count > 0 Then
                Set sh = oDoc.Shapes(1)
                oDoc.Shapes.AddPicture strDir2
                MsgBox "Floating picture changed"
                oDoc.InlineShapes.AddPicture strDir2
                MsgBox "Inline picture added"
            End If
        End If
        oWord.Visible = True 'leave Word application open and visible
        'oWord.Quit 'this would close the application
        PictureToWord = True
        Exit Function
        'If Not Err.Number = 0 Then
            'PictureToWord = False
            'PictureToWord = True
        'End If
        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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now