user1854628
user1854628

Reputation: 253

Linked Images and Tables to MS Word

I would like to automate the process of populating a report in Word. I have several hundred figures and tables in a ~400 page document. I use Matlab to process my data, save figures to organized folders, and populate preformatted tables in different tabs of an Excel file template. I am happy with this side of it, but populating the Word report with copy and paste is time intensive.

I will soon be doing a very similar report, and I would like to completely remove the C and P part of populating the report, both for the figures that are saved in the folders and the tables in their specific tab of the Summary sheet. It would be awesome if I could setup a template that automatically refreshes as sometimes the table and figure building process is iterative. I have a small amount of experience processing data in VBA, but nothing for this application. Where do I start? A bump in the right direction or links to similar questions would be much appreciated.

Upvotes: 3

Views: 999

Answers (1)

Automate This
Automate This

Reputation: 31364

What if you insert objects for the pictures that are linked to the file. This way they update automatically when the file name changes? This assumes that you always have the same number of pictures and the names don't change.

    Selection.InlineShapes.AddOLEObject ClassType:="Paint.Picture", FileName:= _
      "C:\Users\name\Pictures\test.bmp", LinkToFile:=True, DisplayAsIcon:= _
      False

Assuming you have a folder set up with a template word document that has image links to another folder and you want to make sure these images are linked to the most current folder that is named by a date such as 20131008. You can link the image to file for automatic updating but you can't change the source path programmatically due to it's read only property. An alternative method is to loop through each object in the word document, see if it's path is the current folder and if it's not, delete the original and insert a new one.

Below is code for a simple example. You might have to copy over positioning and formatting if you have made any enhancements to the image after it was inserted. I set up my folder structure as follows where each folder with the name as the date has images with the same name.

enter image description here


For OLE type links to .bmp images

Sub LinkToCurrentImageFolder()
    'Get current folder by date
    Dim clientFiguresPath As Variant
    filePath = ActiveDocument.Path & "\ClientFigures\"

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fld = fso.GetFolder(filePath)
    Dim currentFolder As Variant: currentFolder = ""
    For Each sf In fld.SUBFOLDERS
        'Look at name and get current date
        If currentFolder = "" Then
            currentFolder = sf.Path
        ElseIf sf.Path > currentFolder Then
            currentFolder = sf.Path
        End If
    Next

    ' Debug: display current figure folder path
    'MsgBox (currentFolder)

    'Loop through all shapes in document and check if path is current.
    'If path is not current delete current shape and add new because SourcePath is read-only

    Dim Ishape As InlineShape, Wdoc As Document
    MsgBox (ActiveDocument.InlineShapes.Count)

    For Each Ishape In ActiveDocument.InlineShapes
       If Not GetSourceInfo(Ishape) Then GoTo nextshape

        With Ishape
            currentPath = .LinkFormat.SourcePath

            If currentPath <> currentFolder Then
                cType = .OLEFormat.ClassType
                shpName = .LinkFormat.SourceName
                newPath = currentFolder & "\" & shpName

                'Delete existing image
                .Delete

                'Create new image
                Selection.InlineShapes.AddOLEObject ClassType:=cType, FileName:=newPath, LinkToFile:=True, DisplayAsIcon:=False
            End If
       End With
nextshape:
    Next Ishape
End Sub

Function GetSourceInfo(oShp As InlineShape) As Boolean
    On Error GoTo Error_GetSourceInfo
    Test = oShp.LinkFormat.SourceFullName
    GetSourceInfo = True
    Exit Function
Error_GetSourceInfo:
   GetSourceInfo = False
End Function

EDIT

I've changed this code to use images that are linked to file but not as OLE type. This assumes you are inserting images by this method:

enter image description here


Sub LinkToCurrentImageFolder()
    'Get current folder by date
    Dim clientFiguresPath As Variant
    filePath = ActiveDocument.Path & "\ClientFigures\"

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fld = fso.GetFolder(filePath)
    Dim currentFolder As Variant: currentFolder = ""
    For Each sf In fld.SUBFOLDERS
        'Look at folder name/date and get most current date
        If currentFolder = "" Then
            currentFolder = sf.Path
        ElseIf sf.Path > currentFolder Then
            currentFolder = sf.Path
        End If
    Next

    Dim Ishape As InlineShape

    For Each Ishape In ActiveDocument.InlineShapes
        If Ishape.Type = msoComment Then
            With Ishape
                currentPath = .LinkFormat.SourcePath

                If currentPath <> currentFolder Then
                    shpName = .LinkFormat.SourceName
                    newPath = currentFolder & "\" & shpName

                    'Delete existing image
                    .Delete

                    'Create new image
                    Selection.InlineShapes.AddPicture FileName:=newPath, LinkToFile:=True, SaveWithDocument:=True
                End If
           End With
        End If
    Next Ishape
End Sub

Upvotes: 2

Related Questions