?
Solved

Updating an image in Word via VB6.

Posted on 2006-03-29
7
Medium Priority
?
291 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.

0
Comment
Question by:anne0952
  • 2
4 Comments
 
LVL 15

Expert Comment

by:Computron
ID: 16328919
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 16332463
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
 

Author Comment

by:anne0952
ID: 16335555
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
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 16340871
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

839 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