Làm thế nào để chuyển Form dữ liệu từ Word sang Excel

Thảo luận trong 'Thủ thuật Windows - System' bắt đầu bởi CafeNgot, 20 Tháng năm 2014.

  1. Offline

    CafeNgot

    • Thành Viên Mới

    Số bài viết:
    76
    Đã được thích:
    1
    Điểm thành tích:
    0
    Trong bài viết Taimienphi hướng dẫn các bạn sẽ lấy ví dụ với bảng tính kết quả có 2 trường SchoolName và Address. Được lưu ở đường dẫn E:\Examples\Gradebook.xlsx
    [IMG]

    Bước 1: Thêm tab Developer cho Word. Chọn Office button Word options
    [IMG]

    Bước 2: Chọn tab Popular → tick chọn Show Developer tab in the Ribbon OK.
    [IMG]

    Bước 3: Chọn tab Developer Legacy Tools Text Form Field
    [IMG]

    Xuất hiện trên trang Word bây giờ là công cụ Text Form Field.
    [IMG]

    Bước 4: Double-click vào công cụ Text Form Field. Gõ txtSchooName vào ô BookmarkOK.
    [IMG]

    Bước 5: Tạo thêm 1 công cụ Text Form Field nữa cho trường Address.
    [IMG]

    Bước 6: txtAddress vào ô Bookmark OK.
    [IMG]

    Bước 7: Bấm tổ hợp phím Alt + F11 để khởi động VBE (Visual Basic Editor).
    [IMG]

    Bước 8: Vào Insert Module
    [IMG]

    Bước 9: Copy đoạn code sau vào cửa sổ module. Sau đó lưu và trở về Word.
    Sub TransferToExcel()
    'Transfer a single record from the form fields to an Excel workbook.
    Dim doc As Document
    Dim strSchoolName As String
    Dim strPhone As String
    Dim strSQL As String
    Dim cnn As ADODB.Connection
    'Get data.
    Set doc = ThisDocument
    On Error GoTo ErrHandler
    strSchoolName = Chr(39) & doc.FormFields("txtSchoolName").Result & Chr(39)
    strPhone = Chr(39) & doc.FormFields("txtPhone").Result & Chr(39)
    'Define sql string used to insert each record in the destination workbook.
    'Don't omit the $ in the sheet identifier.
    strSQL = "INSERT INTO [PhoneList$]" _
    & " (SchoolName, Phone)" _
    & " VALUES (" _
    & strSchoolName & ", " _
    & strPhone _
    & ")"
    Debug.Print strSQL
    ‘Define connection string and open connection to destination workbook file.
    Set cnn = New ADODB.Connection
    With cnn
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "Data Source=E:\Examples\Gradebook.xlsx;" & _
    -"Extended Properties=Excel 8.0;"
    .Open
    Transfer data
    Execute strSQL
    End With
    Set doc = Nothing
    Set cnn = Nothing
    Exit Sub
    ErrHandler:
    MsgBox Err.Number & ": " & Err.Description, _
    -vbOKOnly, "Error"
    On Error GoTo 0
    On Error Resume Next
    cnn.Close
    Set doc = Nothing
    Set cnn = Nothing
    End Sub

    [IMG]

    Bước 10: Double-click vào Text Form Field của trường Address. Chọn TransferToExcel ở ô Exit OK.
    [IMG]

    Bây giờ, khi bạn bấm phím Tab trên bàn phím thì trường Address sẽ sao chép dữ liệu từ hai trường SchoolName và Address đến tập tin Gradebook.xlsx với đường dẫn E:\Examples\Gradebook.xlsx. Nếu như file excel và đường dẫn đến file của bạn khác thì bạn hãy sửa lại trong đoạn code cho đúng theo tên file và đường dẫn đến file excel của bạn.
    Nắm được dữ liệu chính là chìa khóa để chuyển đổi mỗi bản ghi mới mà không nảy sinh vấn đề nghiêm trọng gì. Đoạn mã ví dụ chứa đựng các kĩ năng cần thiết giúp bạn bắt đầu. Bạn cần phải nâng cao kỹ thuật để điều chỉnh dữ liệu và một số các yêu cầu khác.

Chia sẻ trang này

Advertising: Linux system admin | nukeviet | nukeviet 4 | Upload ảnh miễn phí