Note that there are some explanatory texts on larger screens.

plurals
  1. POLinked Table in MS Word
    primarykey
    data
    text
    <p>I would like to refresh Excel tables in a Word template report programmatically using VBA. The tables are written to several sheets in a template Excel file from Matlab. The file structure will look like this: <img src="https://i.stack.imgur.com/Cn3QQ.png" alt="enter image description here"></p> <p>The code would have to check the folder structure to see if it is pulling the Excel file out of the newest folder. If it was, it would just refresh all of the cells. If not, it would have to delete all of the tables and insert new ones from the same sheets as the previous ones were pulled. I am unsure of the code between the asterisks below. Any help would be much appreciated.</p> <pre><code>Sub LinkToCurrentTableFolder() 'Get current folder by date Dim clientTablesPath As Variant filePath = ActiveDocument.Path &amp; "\ClientTables\" 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 &gt; currentFolder Then currentFolder = sf.Path End If Next '*** 'Debug: display current Excel folder path 'MsgBox (currentFolder) If currentPath = currentFolder Then 'Loop through all tables in document and refresh 'If path is not current delete current table Dim tbTemp As Table Dim cellTemp As Cell For Each tbTemp In ActiveDocument.Tables For Each cellTemp In tbTemp.Range.Cells cellTemp.Range.Fields.Update Next Next Else 'Locate same file name in new folder shpName = .LinkFormat.SourceName NewPath = currentFolder &amp; "\" &amp; shpName 'Delete existing table (???) Not sure .Delete 'Create new table (???) Not sure - must be from same location and same size as previous one Selection.Table.AddOLEObject ClassType:=cType, FileName:=NewPath, LinkToFile:=True, DisplayAsIcon:=False End If '*** End Sub </code></pre> <p>EDIT - The copy and pasting is done as shown below: <img src="https://i.stack.imgur.com/mshtz.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload