[VBA] Sắp xếp Page file Word theo nội dung file Excel

Liên hệ QC

hoanggo123

Thành viên mới
Tham gia
2/11/18
Bài viết
14
Được thích
2
Mình có 1 file EXCEL, 1 file WORD1. Cần tạo ra 1 file WORD2 như cách thức bên dưới. Có thể sử dụng VBA của EXCEL để làm việc trên không? o_O
Cao nhân nào giúp mình với. thanks :D:D

INPUT

INPUT

OUTPUT

EXCEL

WORD1

WORD2

CELL1: ABC
CELL2: 123
CELL3: 456

PAGE1: 11111111456111111111
PAGE2: ...................ABC.....................
PAGE3: ZZZZZZZ123ZZZZZZZZ

PAGE1: ...................ABC.....................
PAGE2: ZZZZZZZ123ZZZZZZZZ
PAGE3: 11111111456111111111
 
Lần chỉnh sửa cuối:
Chưa biết có ai có hứng không nhưng muốn nhờ thì nhiều khi phải đính kèm tập tin, không phải ảnh. Trong trường hợp này thì có lẽ là: tập tin Excel, tập tin Word với nội dung nguồn, tập tin word với kết quả mong đơi.
Đính kèm tập tin không phải là điều kiện đủ nhưng thường là điều kiện cần.
 
Upvote 0
Thử code sau. Tập tin Excel và Word phải cùng thư mục. Nếu đặt khác nhau thì tự sửa code. Tập tin word nguồn và đích tên là word1.docx và word2.docx. Nếu cho tên khác thì tự sửa Const wordfile1 và wordfile2 trong code.

Mở tập tin Excel -> Alt + F11 -> menu Insert -> Module -> dán code sau vào module
Mã:
Private Const wdPropertyPages = 14
Private Const wdGoToPage = 1
Private Const wdGoToAbsolute = 1
Private Const wdParagraph = 4

Sub sort_word_pages()
Const wordfile1 = "WORD1.docx"
Const wordfile2 = "WORD2.docx"
Dim k As Long, r As Long, count As Long, text As String, Arr()
Dim WordApp As Object, doc As Object, doc2 As Object
Dim nPagesCount As Long, pages_range As Object
    With ThisWorkbook.Worksheets("Sheet1")
        Arr = .Range("A1:A" & .Cells(Rows.count, "A").End(xlUp).Row + 1).Value
    End With
    ReDim Preserve Arr(1 To UBound(Arr), 1 To 2)
   
'    khoi dong server WORD
    Set WordApp = CreateObject("Word.Application")
'    WordApp.Visible = True
    Set doc = WordApp.documents.Open(ThisWorkbook.Path & "\" & wordfile1)
'    so trang trong Document nguon
    nPagesCount = doc.BuiltinDocumentProperties(wdPropertyPages)
    For k = 1 To nPagesCount
'        nhay toi cac trang lien tiep
        WordApp.Selection.GoTo wdGoToPage, wdGoToAbsolute, k
'        mo rong chon ra ca paragraph dau tien
        WordApp.Selection.Expand wdParagraph
'        noi dung cua paragraph dau tien
        text = WordApp.Selection.Range.text
        For r = 1 To UBound(Arr) - 1
'            tim tung gia tri trong cot A cua Excel xem co trong noi dung hien hanh khong.
'            Neu co thi ghi chi so trang vao cot thu 2 va thoat
            If InStr(1, text, Arr(r, 1), vbTextCompare) Then
                Arr(r, 2) = k
                count = count + 1
                Exit For
            End If
        Next r
        If count = UBound(Arr) - 1 Then Exit For
    Next k
'    chi di tiep khi co it nhat 1 trang khop voi muc trong cot A cua Excel
    If count Then
        Set doc2 = WordApp.documents.Add
        count = 0
        For r = 1 To UBound(Arr) - 1
'            doc ra vi tri cua trang
            k = Arr(r, 2)
'            neu vi tri > 0 thi co nghia la trang o vi tri do khop voi muc hien hanh o cot 1 cua Arr
            If k Then
                count = count + 1
'                kich hoat tap tin nguon
                doc.Activate
                WordApp.Selection.End = 0
'                nhay toi trang o vi tri k
                WordApp.Selection.GoTo wdGoToPage, wdGoToAbsolute, k
'                doc noi dung cua trang va copy vao clipboard
                Set pages_range = WordApp.Selection.Range
                pages_range.End = WordApp.Selection.Bookmarks("\Page").Range.End
                pages_range.Copy
'                kich hoat tap tin ket qua
                doc2.Activate
'                neu so trang hien hanh nho hon count (so trang can co) thi them 1 trang moi
                If doc2.BuiltinDocumentProperties(wdPropertyPages) < count Then WordApp.Selection.InsertNewPage
'                nhay toi trang cuoi cung hien hanh
                WordApp.Selection.GoTo wdGoToPage, wdGoToAbsolute, count
'                dan tu clipboard
                WordApp.Selection.Paste
            End If
        Next r
        doc2.Saveas2 ThisWorkbook.Path & "\" & wordfile2
        doc.Saved = True
    Else
        doc.Saveas2 ThisWorkbook.Path & "\" & wordfile2
    End If
    WordApp.Quit
    Set WordApp = Nothing
    Set doc = Nothing
    Set doc2 = Nothing
End Sub
 
Upvote 0
Dear Batman1,
Thank u. Mình test thử code bạn chạy ok. Mình chỉ chỉnh một số cái phù hợp với việc mình của như code dưới.
Mình có 1 vài ý kiến như sau: (Mình dùng bản excel, word 2007)
1. nPagesCount = doc.BuiltinDocumentProperties(wdPropertyPages): trả về giá trị luôn là 3 dù có nhiều page hơn.
Mình sửa thành nPagesCount = doc.Range.Information(4) thì ra đúng số page.
2. Arr() của bạn là mảng 2 chiều à, sao mình convert sang 1 chiều nó vẫn là 2 chiều nhỉ?
ReDim Preserve Arr(1 To UBound(Arr), 1 To 1)

Cảm ơn bạn nhiều nhé.


Mã:
Private Const wdPropertyPages = 14
Private Const wdGoToPage = 1
Private Const wdGoToAbsolute = 1
Private Const wdParagraph = 4


Sub sort_word_pages()
Const wordfile1 = "WORD1.docx"
Const wordfile2 = "WORD2.docx"
Dim k As Long, r As Long, count As Long, text As String, Arr(), Arr2()
Dim WordApp As Object, doc As Object, doc2 As Object
Dim nPagesCount As Long, pages_range As Object
    With ThisWorkbook.Worksheets("Sheet1")
        Arr = .Range("A1:A" & .Cells(Rows.count, "A").End(xlUp).Row + 1).Value
    End With
   ReDim Preserve Arr(1 To UBound(Arr), 1 To 1)
   ReDim Arr2(1)
   Arr2(1) = 0
'    khoi dong server WORD
    Set WordApp = CreateObject("Word.Application")
    WordApp.Visible = True
    Set doc = WordApp.documents.Open(ThisWorkbook.Path & "\" & wordfile1)
'    so trang trong Document nguon
    nPagesCount = doc.Range.Information(4)
    For r = 1 To UBound(Arr) - 1
    For k = 1 To nPagesCount
'        nhay toi cac trang lien tiep
        WordApp.Selection.End = 0
        WordApp.Selection.GoTo wdGoToPage, wdGoToAbsolute, k
'        doc noi dung cua trang
        Set pages_range = WordApp.Selection.Range
        pages_range.End = WordApp.Selection.Bookmarks("\Page").Range.End
'        noi dung cua trang
        text = pages_range
            If InStr(1, text, Arr(r, 1), vbTextCompare) Then
                ReDim Preserve Arr2(UBound(Arr2) + 1)
                Debug.Print k & "'" & UBound(Arr2) & "'" & Arr(r, 1) & "'" & r
                Arr2(UBound(Arr2)) = k
            End If
    Next k
    Next r
        Set doc2 = WordApp.documents.Add
        count = 0
        For r = 1 To UBound(Arr2)
'            doc ra vi tri cua trang
            k = Arr2(r)
'            neu vi tri > 0 thi co nghia la trang o vi tri do khop voi muc hien hanh o cot 1 cua Arr
            If k Then
                count = count + 1
'                kich hoat tap tin nguon
                doc.Activate
                WordApp.Selection.End = 0
'                nhay toi trang o vi tri k
                WordApp.Selection.GoTo wdGoToPage, wdGoToAbsolute, k
'                doc noi dung cua trang va copy vao clipboard
                Set pages_range = WordApp.Selection.Range
                pages_range.End = WordApp.Selection.Bookmarks("\Page").Range.End
                pages_range.Copy
'                kich hoat tap tin ket qua
                doc2.Activate
'                neu so trang hien hanh nho hon count (so trang can co) thi them 1 trang moi
                If doc2.BuiltinDocumentProperties(wdPropertyPages) < count Then WordApp.Selection.InsertNewPage
'                nhay toi trang cuoi cung hien hanh
                WordApp.Selection.GoTo wdGoToPage, wdGoToAbsolute, count
'                dan tu clipboard
                WordApp.Selection.Paste
            End If
        Next r
        doc2.SaveAs ThisWorkbook.Path & "\" & wordfile2
'       doc.Saved = True
'    WordApp.Quit
    Set WordApp = Nothing
    Set doc = Nothing
    Set doc2 = Nothing
End Sub
 
Upvote 0
Hi Batman1,
Sao mình dùng code trên, lúc header và footer, lúc lại không có vậy?.
copy theo thứ tự thì có, mà không theo thứ tự page lại không có??
 
Lần chỉnh sửa cuối:
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom