• Demo_Code Xuất Lý lịch 2C sang Word (Full)
  • RE: Demo_Code Xuất Lý lịch 2C sang Word (Full)

    nguyencaothong > 08-02-25, 04:50 PM

    Bác cho em xin bản có pass mở để xem code nghiên cứu ạ, 
    email: nguyencaothong@gmail.com
    em cảm ơn bác


    (15-01-21, 03:11 PM)maidinhdan Đã viết: Vài hôm trước có bạn hỏi về xuất dữ liệu Access sang word thông qua chương trình có Lý lịch/ hợp đồng
    Ở bài viết này, có thể xem là những kiểu xuất được tập hợp thông qua phiếu lý lịch viên chức.

    Trong đoạn code bao gồm:
    1. Code Xuất Phiếu Lý lịch, gồm:
    + Xuất Ảnh sang Word
    + Xuất Dữ liệu của nhiều Table trong Access sang Nhiều Table trong Word ( trong word table được kẻ đầu tiên được mặt định là table(0))
    + Tạo dòng mới cho table trong Word
    + Chỉnh kích thước, In đậm, In nghiêm.... trong word khi xuất
    + Xuất sang word lưu với tên mặc định/ có thể lựa chọn theo kiểu chỉnh mở (Add lên file mẫu)
    + Hàm hiển thị thông báo tiếng việt.

    * Lưu ý: những dòng màu xanh không dùng trong đoạn code, để lại cho các bạn tham khảo tùy vào mục đích sử dụng của các bạn.

    2. Hàm loại bỏ dấu tiếng việt.
    Video minh họa


    Code
    Mã PHP:
    Sub XuatLyLich()
    On Error GoTo Loi
    ' Khoi tao Muon
    Dim oApp As Object, oDoc As Object

    Ket noi som
    'Dim oApp As Word.Application
    '
    Dim oDoc As Word.Document

    Dim LinkMau2c 
    As String
    Dim Dong 
    As LongCot As Long

    LinkMau2c 
    CurrentProject.Path "\Mau\MauLyLich2C.dot"  ' Noi luu file Mau

    Khoi tao Word
    'Set oApp = New Word.Application
    Set oApp = CreateObject("Word.Application")
    oApp.Visible = True ' 
    An file Word

    ' Khoi tao Mau 2C moi
    Set oDoc = oApp.Documents.Open(LinkMau2c, , True) '
    Chi xem:  Neu dung (LinkMau2c, , Falsethi nhan mo lan 2 se hoi la Read Only:
    'Set Doc = oApp.Documents.Add(LinkMau2c)        ' Khoi tao Mau 2C moi
    With oDoc

    'Ket noi tung Field rieng le, ' Chep thong tin vao file Word
    '.FormFields("Madonvi").Result = Me.Madonvi
    .FormFields("Socmnd").Result = Nz(Me.Socmnd)
    Dim NC As String: NC = Format(Me.NgaycapCMND, "dd/MM/yyyy")
    .FormFields("NgaycapCMND").Result = NC
    .FormFields("Noicap").Result = Me.Noicap
    .FormFields("Sobhxh").Result = Nz(Me.Sobhxh)
    '
    .FormFields("Machucvus").Result Me.Machucvus
    .FormFields("Mangachs").Result Me.Mangachs.Column(0)
    .
    FormFields("Tenngach").Result Me.Mangachs.Column(1)
    '.FormFields("TenPhong").Result = Me.TenPhong
    '
    .FormFields("Masothue").Result Me.Masothue
    .FormFields("Hoten").Result Me.Hoten
    .FormFields("Hoten2").Result Me.Hoten
    .FormFields("Ngaysinh").Result Format(Day(Me.Ngaysinh), "00")
    .
    FormFields("Thangsinh").Result Format(Month(Me.Ngaysinh), "00")
    .
    FormFields("Namsinh").Result Year(Me.Ngaysinh)

    ' Dien ngay thang nam
    .FormFields("Ngay").Result = Format(Day(Date), "00")
    .FormFields("Thang").Result = Format(Month(Date), "00")
    .FormFields("Nam").Result = Year(Date)

    .FormFields("Gioitinh").Result = Me.Gioitinh
    .FormFields("Noisinh").Result = Me.Noisinh
    .FormFields("Quequan").Result = Me.Quequan
    .FormFields("Noiohiennay").Result = Me.Noiohiennay
    .FormFields("Noiohiennay2").Result = Me.Noiohiennay
    .FormFields("Dantoc").Result = Me.Dantoc
    .FormFields("Tongiao").Result = Me.Tongiao
    .FormFields("Nghekhituyendung").Result = Me.Nghenghiepkhituyendung.Value
    Dim NTD As String: NTD = Format(Me.Ngaytuyendung, "dd/MM/yyyy")
    .FormFields("Ngaytuyendung").Result = NTD
    .FormFields("Coquantuyendung").Result = Nz(Me.Coquantuyendung)
    .FormFields("Chucdanhhientai").Result = Nz(Me.Chucdanhhientai)
    .FormFields("Totnghieplopmay").Result = Nz(Me.Totnghieplopmay)
    .FormFields("Trinhdochuyenmon").Result = Nz(Me.Trinhdochuyenmon)
    .FormFields("Chuyennganh").Result = Nz(Me.Chuyennganh)
    '
    .FormFields("Chuyenkhoa").Result Me.Chuyenkhoa
    '.FormFields("Thanhphanxuathan").Result = Me.Thanhphanxuathan
    '
    .FormFields("Loaihopdong").Result Me.Loaihopdong
    Dim NVD 
    As StringNVD Format(Me.NgayvaoDang"dd/MM/yyyy")
    .
    FormFields("NgayvaoDang").Result Nz(NVD)
    Dim NCT As StringNCT Format(DateAdd("YYYY"1Me.NgayvaoDang), "dd/MM/yyyy")
    .
    FormFields("NgayChinhThuc").Result Nz(NCT)
    .
    FormFields("Lyluanchinhtri").Result Nz(Me.Lyluanchinhtri)
    .
    FormFields("Quanlynhanuoc").Result Nz(Me.Quanlynhanuoc)
    Dim NVDoan As StringNVDoan Format(Me.NgayvaoDoan"dd/MM/yyyy")
    .
    FormFields("NgayvaoDoan").Result Nz(NVDoan)
    .
    FormFields("Bacluong").Result Nz(Me.Bacluong)
    .
    FormFields("Heso").Result Nz(Me.Heso)
    Dim NH As StringNH Format(Me.Ngayhuong"dd/MM/yyyy")
    .
    FormFields("Ngayhuong").Result Nz(NH)
    .
    FormFields("Congviecduocgiao").Result Nz(Me.Congviecduocgiao)
    .
    FormFields("Chieucao").Result Nz(Me.Chieucao)
    .
    FormFields("Cangnang").Result Nz(Me.Cangnang)
    .
    FormFields("Nhommau").Result Nz(Me.Nhommau)
    .
    FormFields("Tenngoaingu").Result Nz(Me.Tenngoaingu)
    .
    FormFields("Trinhdongoaingu").Result Nz(Me.Trinhdongoaingu)
    .
    FormFields("Trinhdotinhoc").Result Nz(Me.Trinhdotinhoc)
    .
    FormFields("Ngoaingukhac").Result Nz(Me.Ngoaingukhac)
    .
    FormFields("pccv").Result Me.pccv
    '.FormFields("pckv").Result = Me.pckv
    '
    .FormFields("vk").Result Me.vk
    '.FormFields("hsvk").Result = Me.hsvk
    '
    .FormFields("pcth").Result Me.pcth
    '.FormFields("pctn").Result = Me.pctn
    .FormFields("pcud").Result = Me.pcud
    .FormFields("pcdh").Result = Me.pcdh
    '
    .FormFields("pcbl").Result Me.pcbl
    '.FormFields("NgaythamgiaBHXH").Result = Me.NgaythamgiaBHXH
    '
    .FormFields("BHXH").Result Me.BHXH
    '.FormFields("BHYT").Result = Me.BHYT
    '
    .FormFields("BHTN").Result Me.BHTN
    '.FormFields("Giadinhchinhsach").Result = Me.Giadinhchinhsach
    '
    .FormFields("Sotaikhoannganhang").Result Me.Sotaikhoannganhang
    '.FormFields("Dangkytainganhang").Result = Me.Dangkytainganhang
    '
    .FormFields("Hinhthe").Result Me.Hinhthe
    '.FormFields("Thannhannuocngoai").Result = Me.Thannhannuocngoai
    '
    .FormFields("Davehuu").Result Me.Davehuu
    '.FormFields("Ngayvehuu").Result = Me.Ngayvehuu
    '
    .FormFields("Ghichukhac").Result Me.Ghichukhac

    ' Chen Anh vao file Word bang link anh
        .Activate
    '
        Set oDoc ActiveDocument
        
    .Tables(1).Cell(11).Range.InlineShapes.AddPicture (CurrentProject.Path "\Hinh\" & Me.Hinhthe)        ' Chen hinh
        .Tables(1).Cell(1, 1).Range.InlineShapes(1).ScaleHeight = 13
        .Tables(1).Cell(1, 1).Range.InlineShapes(1).ScaleWidth = 13
        .Tables(1).Cell(1, 1).Width = 13
        .Tables(1).Cell(1, 1).Height = 13
    ' Ket noi den cac Table
        Dong = 2
        ' Nap Table 2 trong Word: Dao tao boi duong
        Dim rs As Recordset
        Dim sql As String
        sql = "
    Select from tbdaotaotaphuan where Socmnd='" & Me.Socmnd & "'"
        Set rs = CurrentDb.OpenRecordset(sql, dbOpenDynaset)  ' hoac dbOpenTable
        If rs.EOF Then
        Else
            rs.MoveFirst
            Do Until rs.EOF
                    .Tables(2).Rows.Add  ' Tao dong moi
                    .Tables(2).Cell(Dong, 1).Range.Text = rs.Fields("
    Tentruongdaotao")
                    .Tables(2).Cell(Dong, 2).Range.Text = rs.Fields("
    Tenkhoahoc")
                    .Tables(2).Cell(Dong, 3).Range.Text = rs.Fields("
    TungayDenNgay")
                    .Tables(2).Cell(Dong, 4).Range.Text = rs.Fields("
    Hinhthucdaotao")
                    .Tables(2).Cell(Dong, 5).Range.Text = rs.Fields("
    Vanbangchungchi")
                    Dong = Dong + 1    ' Tang len 1 dong
            rs.MoveNext
            Loop
                    .Tables(2).Rows(Dong).Delete  ' Xoa dong cuoi
        End If
            rs.Close
            Set rs = Nothing
        ' Nap Table 3 trong Word: Tom tat qua trinh cong tac
        Dong = 2
        Dim Rs2 As Recordset
        Dim Sql2 As String
        Sql2 = "
    Select from tbquatrinhcongtac where Socmnd='" & Me.Socmnd & "'"
        Set Rs2 = CurrentDb.OpenRecordset(Sql2, dbOpenDynaset)  ' hoac dbOpenTable
        If Rs2.EOF Then
        Else
            Rs2.MoveFirst
            Do Until Rs2.EOF
                    .Tables(3).Rows.Add  ' Tao dong moi
                    .Tables(3).Cell(Dong, 1).Range.Text = Rs2.Fields("
    Tudenthangnam")
                    .Tables(3).Cell(Dong, 2).Range.Text = Rs2.Fields("
    Chitiet")
                    Dong = Dong + 1    ' Tang len 1 dong
            Rs2.MoveNext
            Loop
            
                            .Tables(3).Rows(Dong).Delete  ' Xoa dong cuoi
        End If
            Rs2.Close
            Set Rs2 = Nothing
        ' Nap Table 4 trong Word: Bang quan he nhan than cua Minh
        Dong = 2
        Dim Rs3 As Recordset
        Dim Sql3 As String
        Sql3 = "
    Select from tbnhanthan where Socmnd='" & Me.Socmnd & "' AND CuaBanthanhayBenVoChong=Yes"
        Set Rs3 = CurrentDb.OpenRecordset(Sql3, dbOpenDynaset)  ' hoac dbOpenTable
        If Rs3.EOF Then
        Else
            Rs3.MoveFirst
            Do Until Rs3.EOF
                    .Tables(4).Rows.Add  ' Tao dong moi
                    .Tables(4).Cell(Dong, 1).Range.Text = Rs3.Fields("
    Moiquanhe")
                    .Tables(4).Cell(Dong, 2).Range.Text = Rs3.Fields("
    Hoten")
                    .Tables(4).Cell(Dong, 3).Range.Text = Rs3.Fields("
    Namsinh")
                    .Tables(4).Cell(Dong, 4).Range.Text = Rs3.Fields("
    Chitiet")
                    Dong = Dong + 1    ' Tang len 1 dong
            Rs3.MoveNext
            Loop
                            .Tables(4).Rows(Dong).Delete  ' Xoa dong cuoi
        End If
            Rs3.Close
            Set Rs3 = Nothing
        ' Nap Table 5 trong Word: Bang quan he nhan than cua Vo minh (Neu co)
        Dong = 2
        Dim Rs4 As Recordset
        Dim Sql4 As String
        Sql4 = "
    Select from tbnhanthan where Socmnd='" & Me.Socmnd & "' AND CuaBanthanhayBenVoChong=No"
        Set Rs4 = CurrentDb.OpenRecordset(Sql4, dbOpenDynaset)  ' hoac dbOpenTable
        If Rs4.EOF Then
        Else
            Rs4.MoveFirst
            Do Until Rs4.EOF
                    .Tables(5).Rows.Add  ' Tao dong moi
                    .Tables(5).Cell(Dong, 1).Range.Text = Rs4.Fields("
    Moiquanhe")
                    .Tables(5).Cell(Dong, 2).Range.Text = Rs4.Fields("
    Hoten")
                    .Tables(5).Cell(Dong, 3).Range.Text = Rs4.Fields("
    Namsinh")
                    .Tables(5).Cell(Dong, 4).Range.Text = Rs4.Fields("
    Chitiet")
                    Dong = Dong + 1    ' Tang len 1 dong
            Rs4.MoveNext
            Loop
                            .Tables(5).Rows(Dong).Delete  ' Xoa dong cuoi
        End If
            Rs4.Close
            Set Rs4 = Nothing

    ' Nap Table 6 trong Word: Bang Qua trinh Luong
        Cot = 2
        Dim Rs5 As Recordset
        Dim Sql5 As String
        Dim SoLanNangLuong As Byte, i As Byte, n As Byte
        Sql5 = "
    SELECT FROM tblichsunangluong WHERE Hoten='" & Me.Hoten & "' ORDER BY Hesoselen"
    '    Sql5 = "
    SELECT TOP 9 FROM tblichsunangluong WHERE Hoten='" & Me.Hoten & "' ORDER BY Hesoselen"
        Set Rs5 = CurrentDb.OpenRecordset(Sql5, dbOpenDynaset)  ' hoac dbOpenTable
        If Rs5.EOF Then
        Else
        'xac dinh de lay 9 dong cuoi cung
        Rs5.MoveLast    ' dich chuyen con tro den dong cuoi cung va dem
        SoLanNangLuong = Rs5.RecordCount
        Rs5.MoveFirst
        
        If SoLanNangLuong > 9 Then
        n = SoLanNangLuong - 9
            For i = 1 To n
                Rs5.MoveNext    'dich chuyen con tro len tren 1 buoc
            Next i
        End If
            Do Until Rs5.EOF
    '                .Tables(6).Column(Cot).Select
    '                .Selection.InsertColumnsRight  ' Tao dong moi
                    .Tables(6).Cell(1, Cot).Range.Text = Format(Month(Rs5.Fields("
    Ngaynangluong")), "00") & "/" & Format(Year(Rs5.Fields("Ngaynangluong")), "0000")
                    .Tables(6).Cell(2, Cot).Range.Text = Rs5.Fields("
    Mangach") & "/" & Rs5.Fields("Baclen")
                    .Tables(6).Cell(3, Cot).Range.Text = Rs5.Fields("
    Hesoselen")
                    Cot = Cot + 1    ' Tang len 1 cot
            Rs5.MoveNext
            Loop
        End If
            Rs5.Close
            Set Rs5 = Nothing
    End With




    ' Luu voi ten moi
    oDoc.SaveAs FileName:=CurrentProject.Path & "
    \Mau\" & Replace(LoaibodauTiengviet(Me.Hoten) & "_" & Me.Socmnd & "_" & Format(Now(), "YYYY_MM_hh_ss"), " ", "_") & ".doc"


    '    ' Dong file vua Luu
    '    oDoc.Close
    '    ' Thoat khoi file Word
    '    oApp.Quit

    ' Xoa bien khoi tao
    Set oApp = Nothing
        MsgBox "
    Xu" & ChrW(7845) & "t lý l" & ChrW(7883) & "ch 2c hoàn t" & ChrW(7845) & "t", vbInformation, "Thông báo"

    Exit_Loi:
        Exit Sub
    Loi:
        MsgBox "
    Nh" & ChrW(7853) & "p thông tin Lý l" & ChrW(7883) & "ch 2C ch" & ChrW(432) & "" & ChrW(273) & ChrW(7847) & "" & ChrW(273) & ChrW(7911) & "nên không th" & ChrW(7875) & " th" & ChrW(7921) & "c hi" & ChrW(7879) & "n", vbInformation, "Thông báo"
        Resume Exit_Loi
    End Sub

    Function LoaibodauTiengviet(ByVal ChuTiengVietCoDau As String) As String

      Dim i As Long
      Dim intCode As Long
      Dim sChar As String
      Dim sConvert As String

      LoaibodauTiengviet = AscW(ChuTiengVietCoDau)
      For i = 1 To Len(ChuTiengVietCoDau)
          sChar = Mid(ChuTiengVietCoDau, i, 1)
          If sChar <> "" Then
              intCode = AscW(sChar)
          End If
          Select Case intCode
          Case 273
              sConvert = sConvert & "
    d"
          Case 272
              sConvert = sConvert & "
    D"
          Case 224, 225, 226, 227, 259, 7841, 7843, 7845, 7847, 7849, 7851, 7853, 7855, 7857, 7859, 7861, 7863
              sConvert = sConvert & "
    a"
          Case 192, 193, 194, 195, 258, 7840, 7842, 7844, 7846, 7848, 7850, 7852, 7854, 7856, 7858, 7860, 7862
              sConvert = sConvert & "
    A"
          Case 232, 233, 234, 7865, 7867, 7869, 7871, 7873, 7875, 7877, 7879
              sConvert = sConvert & "
    e"
          Case 200, 201, 202, 7864, 7866, 7868, 7870, 7872, 7874, 7876, 7878
              sConvert = sConvert & "
    E"
          Case 236, 237, 297, 7881, 7883
              sConvert = sConvert & "
    i"
          Case 204, 205, 296, 7880, 7882
              sConvert = sConvert & "
    I"
          Case 242, 243, 244, 245, 417, 7885, 7887, 7889, 7891, 7893, 7895, 7897, 7899, 7901, 7903, 7905, 7907
              sConvert = sConvert & "
    o"
          Case 210, 211, 212, 213, 416, 7884, 7886, 7888, 7890, 7892, 7894, 7896, 7898, 7900, 7902, 7904, 7906
              sConvert = sConvert & "
    O"
          Case 249, 250, 361, 432, 7909, 7911, 7913, 7915, 7917, 7919, 7921
              sConvert = sConvert & "
    u"
          Case 217, 218, 360, 431, 7908, 7910, 7912, 7914, 7916, 7918, 7920
              sConvert = sConvert & "
    U"
          Case 253, 7923, 7925, 7927, 7929
              sConvert = sConvert & "
    y"
          Case 221, 7922, 7924, 7926, 7928
              sConvert = sConvert & "
    Y"
          Case Else
              sConvert = sConvert & sChar
          End Select
      Next

      LoaibodauTiengviet = sConvert

    End Function