doandinhtam > 12-11-21, 07:45 AM
ongke0711 > 12-11-21, 08:23 AM
doandinhtam > 16-11-21, 04:25 PM
ongke0711 > 16-11-21, 05:55 PM
doandinhtam > 25-11-21, 10:47 AM
ongke0711 > 25-11-21, 12:37 PM
(25-11-21, 10:47 AM)doandinhtam Đã viết: https://drive.google.com/drive/folders/1...sp=sharing
mình không biết đính kèm file nên gửi tạm folder google drive ạ. Trong đó có file word mẫu, mình muốn xuất danh sách từ access sang theo như file mẫu. mong bạn giúp đỡ
Public Function XuatDSHSWord()
Dim db As DAO.Database, rs As DAO.Recordset
Dim oApp As Object, Doc As Object, oWordTbl As Object
Dim strSQL As String, strDocName As String
Dim tblNoInDoc As Integer, iCols As Integer, iRecCount As Integer, iFldCount As Integer, i As Integer
On Error GoTo EH
Set oApp = CreateObject("Word.Application")
'oApp.Visible = True
On Error Resume Next
strDocName = """" & CurrentProject.Path & "\DanhSachHocSinhLop.dotx" & """"
Set Doc = oApp.Documents.Add(strDocName)
Doc.FormFields("fldMaLop").Result = Me.txtMALOP
Doc.FormFields("fldNamHoc").Result = Me.txtNAMHOC
Doc.FormFields("fldChuNhiem").Result = Me.txtCHUNHIEM
'Mo table can export qua file Doc'
Set db = CurrentDb
Set rs = Me.sfmHocSinh.Form.RecordsetClone
With rs
If .RecordCount <> 0 Then
.MoveLast 'De lay chính xác sô dòng
iRecCount = .RecordCount 'Sô dòng
.MoveFirst
iFldCount = .Fields.Count - 1 'So Field trong table cân xuat
tblNoInDoc = 1 'So thu tu cua table trong file Doc'
Set oWordTbl = Doc.Tables(tblNoInDoc)
'Them cot'
With Doc.Tables(tblNoInDoc)
.Select
Do Until .Columns.Count = iFldCount - 1
.Columns.Add
.Columns.AutoFit
Loop
End With
'Dat ten cho dòng tiêu dê
oWordTbl.Cell(1, 1) = "Stt"
oWordTbl.Cell(1, 2) = "H" & ChrW(7885) & " T" & ChrW(234) & "n"
oWordTbl.Cell(1, 3) = "N" & ChrW(259) & "m Sinh"
oWordTbl.Cell(1, 4) = "Gi" & ChrW(7899) & "i T" & ChrW(237) & "nh"
'Build our data rows'
For i = 1 To iRecCount
Doc.Tables(tblNoInDoc).Rows.SetLeftIndent LeftIndent:=10, RulerStyle:=2
Doc.Tables(tblNoInDoc).Rows.Add
oWordTbl.Cell(i + 1, 1) = Nz(rs.Fields("ID").Value, "")
oWordTbl.Cell(i + 1, 1).Range.ParagraphFormat.Alignment = 1 '0 la can trai
oWordTbl.Cell(i + 1, 1).Range.Cells.VerticalAlignment = 1
oWordTbl.Cell(i + 1, 2) = Nz(rs.Fields("HoVaTen").Value, "")
oWordTbl.Cell(i + 1, 2).Range.ParagraphFormat.Alignment = 0
oWordTbl.Cell(i + 1, 2).Range.Cells.VerticalAlignment = 1
oWordTbl.Cell(i + 1, 3) = Nz(rs.Fields("NamSinh").Value, "")
oWordTbl.Cell(i + 1, 3).Range.ParagraphFormat.Alignment = 1
oWordTbl.Cell(i + 1, 3).Range.Cells.VerticalAlignment = 1
oWordTbl.Cell(i + 1, 4) = Nz(rs.Fields("GioiTinh").Value, "")
oWordTbl.Cell(i + 1, 4).Range.ParagraphFormat.Alignment = 1
oWordTbl.Cell(i + 1, 4).Range.Cells.VerticalAlignment = 1
.MoveNext
Next i
'Dinh dang dòng tieu de
With Doc.Tables(tblNoInDoc).Rows(1).Range
.Font.Bold = True
.ParagraphFormat.Alignment = 1
.Cells.VerticalAlignment = 1
.Shading.BackgroundPatternColor = RGB(225, 225, 225) '16
End With
Doc.Tables(tblNoInDoc).Rows(1).Height = 22
'Thiet lap do rong tung côt
Doc.Tables(tblNoInDoc).Columns(1).Width = 0.7 * 72
Doc.Tables(tblNoInDoc).Columns(2).Width = 3 * 72
Doc.Tables(tblNoInDoc).Columns(3).Width = 1.5 * 72
Doc.Tables(tblNoInDoc).Columns(4).Width = 1.5 * 72
End If
End With
rs.Close
Set rs = Nothing
Set oWordTbl = Nothing
oApp.Visible = True
Set oApp = Nothing
MsgBox "Xuat Word hoan tat."
EH_Exit:
Exit Function
EH:
MsgBox Err.Description
Resume EH_Exit
End Function
doandinhtam > 26-11-21, 11:43 AM
doandinhtam > 26-11-21, 04:51 PM
ongke0711 > 26-11-21, 05:18 PM
(26-11-21, 04:51 PM)doandinhtam Đã viết: BÁC CHO EM HỎI ĐẶT TÊN TABLE TRONG WORD NHƯ NÀO Ạ? E THẤY CODE BÁC ĐỂ TABLE NAME LÀ tblNoInDocÀ