VBA を使用してプログラムで Word テンプレート レポートの Excel テーブルを更新したいと考えています。テーブルは、Matlab のテンプレート Excel ファイルの複数のシートに書き込まれます。ファイル構造は次のようになります。
コードは、フォルダー構造をチェックして、Excel ファイルが最新のフォルダーから取り出されているかどうかを確認する必要があります。そうであれば、すべてのセルを更新するだけです。そうでない場合は、すべてのテーブルを削除し、前のテーブルがプルされたのと同じシートから新しいテーブルを挿入する必要があります。以下のアスタリスクの間のコードがわかりません。どんな助けでも大歓迎です。
Sub LinkToCurrentTableFolder()
'Get current folder by date
Dim clientTablesPath As Variant
filePath = ActiveDocument.Path & "\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 > 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 & "\" & 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
編集 - 以下に示すように、コピーと貼り付けが行われます。