Updating an image in Word via VB6.

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.

anne0952Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ComputronCommented:
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
0
GrahamSkanRetiredCommented:
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

0
anne0952Author 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
0
GrahamSkanRetiredCommented:
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


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.