Board logo

標題: [分享] VBA-一次更新你WORD裡面,所有連動EXCEL儲存格連結! [打印本頁]

作者: edmondsforum    時間: 2020-9-16 15:29     標題: VBA-一次更新你WORD裡面,所有連動EXCEL儲存格連結!

小弟做報告書的習慣就偏好連動EXCEL裡面特定儲存格。
例如
{ LINK Excel.SheetMacroEnabled.12 "D:\\案件\\開發案\\水理計算.xlsm" 初期設定!R4C4 \t }

礙於裡面有存在很多連結到別的WORD書籤,導致最早做法都是在"檔案"->"資訊"->"編輯檔案連結" 慢慢改

所以上網找了以下的CODE
  1. Public Sub changeSource()
  2. Dim dlgSelectFile As FileDialog 'FileDialog object '
  3. Dim thisField As Field
  4. Dim selectedFile As Variant
  5. 'must be Variant to contain filepath of selected item
  6. Dim newFile As Variant
  7. Dim fieldCount As Integer '
  8. Dim x As Long
  9. On Error GoTo LinkError
  10. 'create FileDialog object as File Picker dialog box
  11. Set dlgSelectFile = Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
  12. With dlgSelectFile
  13.    .Filters.Clear 'clear filters
  14.   .Filters.Add "Microsoft Excel Files", "*.xls, *.xlsb, *.xlsm, *.xlsx" 'filter for o    nly Excel files
  15.   'use Show method to display File Picker dialog box and return user's action
  16.   If .Show = -1 Then
  17. 'step through each string in the FileDialogSelectedItems collection
  18.     For Each selectedFile In .SelectedItems
  19.          newFile = selectedFile 'gets new filepath
  20.        Next selectedFile
  21.      Else 'user clicked cancel
  22.    Exit Sub
  23.   End If
  24. End With
  25. Set dlgSelectFile = Nothing
  26. 'update fields
  27. With ActiveDocument
  28.   fieldCount = .Fields.Count
  29.   For x = 1 To fieldCount
  30.     With .Fields(x)
  31.      'Debug.Print x '
  32.      Debug.Print .Type
  33.       If .Type = 56 Then
  34.         'only update Excel links. Type 56 is an excel link
  35.         .LinkFormat.SourceFullName = newFile '
  36.         .Update
  37.         .LinkFormat.AutoUpdate = False
  38.         DoEvents
  39.       End If
  40.     End With
  41.   Next x
  42. End With
  43. MsgBox "!更新完成!"
  44. Exit Sub
  45. LinkError:
  46. Select Case Err.Number
  47.   Case 5391 'could not find associated Range Name
  48.     MsgBox "Could not find the associated Excel Range Name " & _
  49.       "for one or more links in this document. " & _
  50.       "Please be sure that you have selected a valid " & _
  51.       "Quote Submission input file.", vbCritical
  52.       Case Else
  53.         MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical
  54.     End Select
  55. End Sub
複製代碼
For x = 1 To fieldCount
在這裡我是會改成 1 To 25,不確定這什麼意思....哈
起初我文件內有100多頁,可能每10頁就會有個EXCEL連結,導致再刷新時...會等很久...
所以我後來的作法就是在第一頁設定表格並加入書籤,裡面都是EXCEL連結,假設裡面有25個是連結excel的....
好像就真的會讀取這25個....就會跑比較快

另外我有而外設定,每次開啟WORD 就會執行這代碼
  1. Sub Document_Open()  
  2. changeSource
  3. End Sub
複製代碼
另外再分享一個好,如果你想要參考別的WORD檔案的內容,例如A檔案連結B檔案,可以先在B檔案裡面,你想要的文字設定書籤
例如 麻辣家族討論版
此時你可以在A檔案:
標題:麻辣家族討論版
{ INCLUDETEXT  " {FILENAME \p}\\..\\"B檔案" "麻辣家族討論版" \* MERGEFORMAT }
{}顏色粉色這個,一定要用ctrl+p哦!!!

這樣不管你放在哪裡,都不會出現錯誤嚕,只是這兩個檔案一定要放在旁哦
作者: abbay262    時間: 2021-1-6 17:32

請問可以有附檔嗎
作者: edmondsforum    時間: 2021-8-20 14:51

[attach]33926[/attach]

你在試用看看嚕




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)