Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

vba - Linked Table in MS Word

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: enter image description here

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.

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

EDIT - The copy and pasting is done as shown below: enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found my answer here - this code asks for the location of the new Excel file and updates all of the field codes of the Excel linked tables.

The code from that link is below.

Public Sub changeSource()
Dim dlgSelectFile As FileDialog 'FileDialog object '
Dim thisField As Field
Dim selectedFile As Variant
'must be Variant to contain filepath of selected item
Dim newFile As Variant
Dim fieldCount As Integer '
Dim x As Long
On Error GoTo LinkError
'create FileDialog object as File Picker dialog box
Set dlgSelectFile = Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
With dlgSelectFile
   .Filters.Clear 'clear filters
  .Filters.Add "Microsoft Excel Files", "*.xls, *.xlsb, *.xlsm, *.xlsx" 'filter for o    nly Excel files
  'use Show method to display File Picker dialog box and return user's action
  If .Show = -1 Then
 'step through each string in the FileDialogSelectedItems collection
    For Each selectedFile In .SelectedItems
         newFile = selectedFile 'gets new filepath
       Next selectedFile
     Else 'user clicked cancel
   Exit Sub
  End If
End With
Set dlgSelectFile = Nothing
'update fields
With ActiveDocument
  fieldCount = .Fields.Count
  For x = 1 To fieldCount
    With .Fields(x)
     'Debug.Print x '
     Debug.Print .Type
      If .Type = 56 Then
        'only update Excel links. Type 56 is an excel link
        .LinkFormat.SourceFullName = newFile '
        .Update
        .LinkFormat.AutoUpdate = False
        DoEvents
      End If
    End With
  Next x
End With
MsgBox "Source data has been successfully imported."
Exit Sub
LinkError:
Select Case Err.Number
  Case 5391 'could not find associated Range Name
    MsgBox "Could not find the associated Excel Range Name " & _
      "for one or more links in this document. " & _
      "Please be sure that you have selected a valid " & _
      "Quote Submission input file.", vbCritical
      Case Else
        MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical
    End Select
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...