Chào mọi người, hiện em mới nhận việc mới, gặp vấn đề về word và excel. Em có nhiều file word với nội dung khác nhau, nhưng có một vài câu lặp đi lặp lại giữa các file word, em muốn link các nội dung đó từ 1 file excel sang word, khi thay đổi file excel thì cập nhật trên các file word. Em dùng copy paste link & keep source fomatting nhưng lại bị lỗi xuống dòng.
Bạn có thể sử dụng tính năng "Mail Merge" trong Word để liên kết dữ liệu từ Excel vào Word. Bạn cần tạo một bảng tính trong Excel chứa các nội dung cần liên kết, sau đó trong Word, chọn "Mail Merge" và chọn nguồn dữ liệu là Excel. Bạn có thể sử dụng các trường trong Excel để liên kết với các vị trí trong Word. Khi dữ liệu trong Excel thay đổi, các nội dung tương ứng trong Word cũng sẽ được cập nhật tương tự.
xin phép bạn truong124, mình cũng đang làm file giống như cách của bạn truong124
, hiện tại mình đã cập nhật được bên word khi thay đổi bên excel, giờ mình muốn thay đổi đường dẫn của toàn bộ link trong file word thì phải làm như thế nào? mong mọi người giúp đỡ.
nếu thay đổi từng cái 1 thì rất mất thời gian.
xin phép bạn truong124, mình cũng đang làm file giống như cách của bạn truong124
, hiện tại mình đã cập nhật được bên word khi thay đổi bên excel, giờ mình muốn thay đổi đường dẫn của toàn bộ link trong file word thì phải làm như thế nào? mong mọi người giúp đỡ.
nếu thay đổi từng cái 1 thì rất mất thời gian.
Cách bạn chỉ mình đã làm rồi và cần nhập đi nhập lại nhiều lần cho từng link nên không khả thi. Hay do mình làm không đúng cách. Bạn có thể chỉ giúp mình chi tiết hơn được không?
cảm ơn bạn hoangtuan, rất mong nhận được sự giúp đỡ từ bạn.
Cách bạn chỉ mình đã làm rồi và cần nhập đi nhập lại nhiều lần cho từng link nên không khả thi. Hay do mình làm không đúng cách. Bạn có thể chỉ giúp mình chi tiết hơn được không?
cảm ơn bạn hoangtuan, rất mong nhận được sự giúp đỡ từ bạn.
Sub Update_Link()
Dim fieldCount As Integer, x As Long
Dim dlgSelectFile As FileDialog
Dim selectedFile As Variant
Dim W As Document
Application.DisplayAlerts = wdAlertsNone
Set dlgSelectFile = Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
With dlgSelectFile
.Filters.Add "Word file", "*.doc?", 1
If .Show = -1 Then
For Each selectedFile In .SelectedItems
Set W = Documents.Open(selectedFile, False)
With W
fieldCount = .Fields.Count
For x = 1 To fieldCount
With .Fields(x)
If .Type = 56 Then
'only update Excel links. Type 56 is an excel link
.LinkFormat.SourceFullName = "C:\Users\Administrator\Downloads\Data1.xlsx"
.Update
.LinkFormat.AutoUpdate = True
DoEvents
End If
End With
Next x
W.Close True
End With
Next selectedFile
End If
End With
Set dlgSelectFile = Nothing
Application.DisplayAlerts = wdAlertsAll
End Sub
Sub Update_Link()
Dim fieldCount As Integer, x As Long
Dim dlgSelectFile As FileDialog
Dim selectedFile As Variant
Dim W As Document
Application.DisplayAlerts = wdAlertsNone
Set dlgSelectFile = Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
With dlgSelectFile
.Filters.Add "Word file", "*.doc?", 1
If .Show = -1 Then
For Each selectedFile In .SelectedItems
Set W = Documents.Open(selectedFile, False)
With W
fieldCount = .Fields.Count
For x = 1 To fieldCount
With .Fields(x)
If .Type = 56 Then
'only update Excel links. Type 56 is an excel link
.LinkFormat.SourceFullName = "C:\Users\Administrator\Downloads\Data1.xlsx"
.Update
.LinkFormat.AutoUpdate = True
DoEvents
End If
End With
Next x
W.Close True
End With
Next selectedFile
End If
End With
Set dlgSelectFile = Nothing
Application.DisplayAlerts = wdAlertsAll
End Sub
Sub Update_Link()
Dim fieldCount As Integer, x As Long
Dim dlgSelectFile As FileDialog
Dim selectedFile As Variant
Dim W As Document
Application.DisplayAlerts = wdAlertsNone
Set dlgSelectFile = Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
With dlgSelectFile
.Filters.Add "Word file", "*.doc?", 1
If .Show = -1 Then
For Each selectedFile In .SelectedItems
Set W = Documents.Open(selectedFile, False)
With W
fieldCount = .Fields.Count
For x = 1 To fieldCount
With .Fields(x)
If .Type = 56 Then
'only update Excel links. Type 56 is an excel link
.LinkFormat.SourceFullName = "C:\Users\Administrator\Downloads\Data1.xlsx"
.Update
.LinkFormat.AutoUpdate = True
DoEvents
End If
End With
Next x
W.Close True
End With
Next selectedFile
End If
End With
Set dlgSelectFile = Nothing
Application.DisplayAlerts = wdAlertsAll
End Sub
Sub Update_Link()
Dim fieldCount As Integer, x As Long
Dim dlgSelectFile As FileDialog
Dim selectedFile As Variant
Dim W As Document
Application.DisplayAlerts = wdAlertsNone
Set dlgSelectFile = Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
With dlgSelectFile
.Filters.Add "Word file", "*.doc?", 1
If .Show = -1 Then
For Each selectedFile In .SelectedItems
Set W = Documents.Open(selectedFile, False)
With W
fieldCount = .Fields.Count
For x = 1 To fieldCount
With .Fields(x)
If .Type = 56 Then
'only update Excel links. Type 56 is an excel link
.LinkFormat.SourceFullName = "C:\Users\Administrator\Downloads\Data1.xlsx"
.Update
.LinkFormat.AutoUpdate = True
DoEvents
End If
End With
Next x
W.Close True
End With
Next selectedFile
End If
End With
Set dlgSelectFile = Nothing
Application.DisplayAlerts = wdAlertsAll
End Sub
Sub Update_Link()
Dim fieldCount As Integer, x As Long
Dim dlgSelectFile As FileDialog
Dim selectedFile As Variant
Dim W As Document
Application.DisplayAlerts = wdAlertsNone
Set dlgSelectFile = Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
With dlgSelectFile
.Filters.Add "Word file", "*.doc?", 1
If .Show = -1 Then
For Each selectedFile In .SelectedItems
Set W = Documents.Open(selectedFile, False)
With W
fieldCount = .Fields.Count
For x = 1 To fieldCount
With .Fields(x)
If .Type = 56 Then
'only update Excel links. Type 56 is an excel link
.LinkFormat.SourceFullName = "C:\Users\Administrator\Downloads\Data1.xlsx"
.Update
.LinkFormat.AutoUpdate = True
DoEvents
End If
End With
Next x
W.Close True
End With
Next selectedFile
End If
End With
Set dlgSelectFile = Nothing
Application.DisplayAlerts = wdAlertsAll
End Sub
Đoạn mã này là một macro để cập nhật các liên kết giữa các tài liệu Word và các tệp Excel. Nó sử dụng đối tượng FileDialog để chọn một hoặc nhiều tệp Word, sau đó lặp qua các trường trong mỗi tài liệu và thay đổi nguồn của bất kỳ liên kết Excel nào thành một đường dẫn tệp được chỉ định. Nó cũng đặt định dạng liên kết để tự động cập nhật và đóng tài liệu sau khi lưu các thay đổi.
Macro này có thể hữu ích nếu bạn muốn thay đổi nguồn dữ liệu của nhiều tài liệu Word có các bảng hoặc biểu đồ Excel được liên kết. Tuy nhiên, bạn có thể muốn sửa đổi mã để phù hợp với nhu cầu của bạn, chẳng hạn như sử dụng một biến cho đường dẫn tệp mới, hoặc kiểm tra loại liên kết trước khi cập nhật nó.
Mình không rõ Vba có nhận rõ tên file bằng tiếng Trung/Nhật không nên bạn cứ chuyển tên file excel thành chữ cái abc, sau đó thay đường dẫn của file excel vào đường dẫn trong vba là được nhé!
Đoạn mã này là một macro để cập nhật các liên kết giữa các tài liệu Word và các tệp Excel. Nó sử dụng đối tượng FileDialog để chọn một hoặc nhiều tệp Word, sau đó lặp qua các trường trong mỗi tài liệu và thay đổi nguồn của bất kỳ liên kết Excel nào thành một đường dẫn tệp được chỉ định. Nó cũng đặt định dạng liên kết để tự động cập nhật và đóng tài liệu sau khi lưu các thay đổi.
Macro này có thể hữu ích nếu bạn muốn thay đổi nguồn dữ liệu của nhiều tài liệu Word có các bảng hoặc biểu đồ Excel được liên kết. Tuy nhiên, bạn có thể muốn sửa đổi mã để phù hợp với nhu cầu của bạn, chẳng hạn như sử dụng một biến cho đường dẫn tệp mới, hoặc kiểm tra loại liên kết trước khi cập nhật nó.
Để sử dụng bạn làm như hình sau:
Bài đã được tự động gộp:
Mình không rõ Vba có nhận rõ tên file bằng tiếng Trung/Nhật không nên bạn cứ chuyển tên file excel thành chữ cái abc, sau đó thay đường dẫn của file excel vào đường dẫn trong vba là được nhé!