2013-10-31 154 views
2

我想使用VBA以編程方式刷新Word模板報表中的Excel表格。這些表格被寫入Matlab中的模板Excel文件中的幾張表格中。文件結構將如下所示:enter image description hereMS Word中的鏈接表

代碼將不得不檢查文件夾結構以查看它是否將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 

編輯如下圖所示,複製和粘貼完成: enter image description here

+0

你有什麼錯誤嗎?什麼類型和在哪一行? –

+0

我更新了代碼,因此如果Excel文件位於最新的文件夾中,它只會更新該鏈接的Excel文件中的表格。如果Excel文件不在最新的文件夾中,那就是問題所在。它必須找到舊的鏈接表來自的工作表並插入正確的行。它會在編譯錯誤時導致shpName = .LinkFormat.SourceName中斷 - 無效或不合格的引用。我不確定如何抓住舊的工作表並將其指向正確的行和列。有什麼幫助嗎? – user1854628

回答

2

我發現我的答案here - 此代碼詢問新的Excel文件和更新的位置所有的域代碼Excel鏈接表。

該鏈接的代碼如下。

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 
+0

很高興你找到答案。我只是在處理這個問題,無法從表格本身公開鏈接。這樣做是有道理的。感謝您的答案更新。 –