Insert Images to Excel Linked Word Table

Landshark24

New Member
Joined
Oct 27, 2019
Messages
6
I have a VBA code that copies a seelction of cells from Excel and creates a Word table that is linked to the original document. I need to be able to insert images into the created word table. The table will allow me to do this, but each time the link to excel is refreshed the images disappear. Is there a way to keep the images without dropping the link to the excel file?

Thank you
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,717
Office Version
365
Platform
Windows
try this method
- amend to integrate with your existing code

Code:
Sub LinkedChartInWordTable()
   [COLOR=#008080][I] 'ensure Microsoft Word is referenced: see VBA \ Tools \ references[/I][/COLOR]
    Dim W As Word.Application, wDoc As Word.Document, wRng As Word.Range, wTbl As Word.Table
    Set W = CreateObject("Word.Application")
    W.Visible = True
    Set wDoc = W.Documents.Add
    
    With wDoc
        Set wRng = .Range(Start:=0, End:=0)
        Set wTbl = .Tables.Add(Range:=wRng, NumRows:=2, NumColumns:=2)
       [COLOR=#006400] 'insert images in table[/COLOR]
[COLOR=#ff0000]            With ThisWorkbook
                .Activate
                .Sheets("NameofSheet").Activate
                .ActiveSheet.ChartObjects(1).Select
            End With
            ActiveChart.ChartArea.Copy
            wTbl.Cell(1, 1).Range.PasteSpecial link:=True
            'etc
   [/COLOR] End With
End Sub
 
Last edited:

Landshark24

New Member
Joined
Oct 27, 2019
Messages
6
@Yongle - thank you for your reply. I am assuming this code copies the picture from excel and pastes or inserts into the word table, but my excel doc doesnt have any images, I need to insert them directly to the word doc later, but the data from the excel doc still needs to update as changed even after the images have been inserted... Any other thoughts?
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,717
Office Version
365
Platform
Windows
Why are you updating links ?
What is being updated ?

Should the images in the table be static after being embedded ( ie the exact same image) ?
 

Landshark24

New Member
Joined
Oct 27, 2019
Messages
6
The data that is updated manually in the original excel file needs to update in the word doc. But yes the images once inserted need to remain static.
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,717
Office Version
365
Platform
Windows
Hard Code one line for each static image (as in example below) so that the images are re-introduced every time the table is created

Code:
Sub LinkedChartInWordTable()
    'ensure Microsoft Word is referenced: see VBA \ Tools \ references
    Dim W As Word.Application, wDoc As Word.Document, wRng As Word.Range, wTbl As Word.Table
    Set W = CreateObject("Word.Application")
    W.Visible = True
    Set wDoc = W.Documents.Add
    
    With wDoc
        Set wRng = .Range(Start:=0, End:=0)
        Set wTbl = .Tables.Add(Range:=wRng, NumRows:=2, NumColumns:=2)
        'insert images in table
            With ThisWorkbook
                .Activate
                .Sheets("NameofSheet").Activate
                .ActiveSheet.ChartObjects(1).Select
            End With

            [COLOR=#ff0000]wTbl.Cell(1, 2).Range.InlineShapes.AddPicture "C:\Test\subfolder\Pic09.jpg"[/COLOR]

    End With
End Sub
 
Last edited:

Forum statistics

Threads
1,082,585
Messages
5,366,466
Members
400,892
Latest member
lamarh755

Some videos you may like

This Week's Hot Topics

Top