Chào cả nhà,
Mình đang làm một số công việc cần trích xuất dữ liệu excel rất nhiều ra các biểu mẫu word. Mình có tìm hiểu trên mạng và lượm được 1 đoạn code theo video như sau. tuy nhiên khi mình chạy thử code chỉ tạo ra các biểu mấu mới chứ không điền dữ liệu vào được. công việc gấp và mình cũng chưa có kiến thức vba cơ bản, mong các bạn trong diễn đàn trợ giúp. Cảm ơn các bạn nhiều!
code:
Sub bbntcv()
Dim num_of_cust As Long
Dim num_of_column As Long
Dim i As Long, j As Long
Dim template As Object
Dim t As Object
num_of_column = 14
num_of_cust = Sheet6.Cells(Rows.Count, "A").End(xlUp).Row - 1
With CreateObject("word.application")
.Visible = True
For i = 1 To num_of_cust
Set template = .documents.Open("C:\Users\NGHIALT\Desktop\New folder\BBNTCV.doc")
Set t = template.Content
For j = 1 To num_of_column
t.Find.Execute _
FindText:=Sheet6.Cells(1, j).Value, _
ReplaceWith:=Sheet6.Cells(i + 1, j).Value, _
Replace:=wdReplaceAll
Next
template.SaveAs Filename:=ThisWorkbook.Path & Application.PathSeparator & i & "-BBNTCV.doc"
Next
.Quit
End With
Set t = Nothing
Set template = Nothing
End Sub
nguồn tham khảo code:
Mình đang làm một số công việc cần trích xuất dữ liệu excel rất nhiều ra các biểu mẫu word. Mình có tìm hiểu trên mạng và lượm được 1 đoạn code theo video như sau. tuy nhiên khi mình chạy thử code chỉ tạo ra các biểu mấu mới chứ không điền dữ liệu vào được. công việc gấp và mình cũng chưa có kiến thức vba cơ bản, mong các bạn trong diễn đàn trợ giúp. Cảm ơn các bạn nhiều!
code:
Sub bbntcv()
Dim num_of_cust As Long
Dim num_of_column As Long
Dim i As Long, j As Long
Dim template As Object
Dim t As Object
num_of_column = 14
num_of_cust = Sheet6.Cells(Rows.Count, "A").End(xlUp).Row - 1
With CreateObject("word.application")
.Visible = True
For i = 1 To num_of_cust
Set template = .documents.Open("C:\Users\NGHIALT\Desktop\New folder\BBNTCV.doc")
Set t = template.Content
For j = 1 To num_of_column
t.Find.Execute _
FindText:=Sheet6.Cells(1, j).Value, _
ReplaceWith:=Sheet6.Cells(i + 1, j).Value, _
Replace:=wdReplaceAll
Next
template.SaveAs Filename:=ThisWorkbook.Path & Application.PathSeparator & i & "-BBNTCV.doc"
Next
.Quit
End With
Set t = Nothing
Set template = Nothing
End Sub
nguồn tham khảo code: