master90x
Thành viên mới
- Tham gia
- 7/1/13
- Bài viết
- 17
- Được thích
- 0
- Giới tính
- Nam
- Nghề nghiệp
- Bách Nghệ
Chào cả nhà! Hiện tại em có 1 file excel chứa dữ liệu và 1 file word để điền dữ liệu và in ra. Việc chèn này giống mail merge nhưng do mail merge có 1 só hạn chế nên e dùng VBA để code cho tiện theo ý mình. Việc chèn và xuất ra file word đã xong nhưng khi thực hiện lệnh in từ file word thì máy in chỉ in ra được 1 mặt mà không ra được 2 mặt (Máy in có thể in 2 mặt). Hì hục sửa code thì lại có kết quả in mỗi mặt 1 tờ =)). Mong các cao nhân chỉ giáo giúp em. Dưới đây là code VBA ạ. Tạm thời em chưa soát và tối ưu lại code. Ưu tiên chạy đc trước đã ạ
C#:
Sub make_contract()
Dim t, template As Object
Dim i, n, lastrow, lastcol As Long
Dim so_nguoi As Long
Dim x As String
Worksheets("TOTALOK").Activate
With ActiveSheet
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
lastcol = .Cells(2, .Columns.Count).End(xlToLeft).Column
End With
so_nguoi = lastrow - 2
With CreateObject("word.application")
.Visible = True
For i = 3 To 3
Set template = .Documents.Open(ThisWorkbook.Path & Application.PathSeparator & "form-hd-1.doc")
Set t = template.Content
For n = 1 To lastcol
Sheets("TOTALOK").Cells(2, n).Select
t.Find.Execute FindText:=Sheets("TOTALOK").Cells(2, n).Value, ReplaceWith:=Sheets("TOTALOK").Cells(i, n).Value, Replace:=wdReplaceAll
Next n
template.SaveAs Filename:=ThisWorkbook.Path & Application.PathSeparator & i & "-hd-test.doc"
''template.PrintOut Range:=wdPrintCurrentPage, ManualDuplexPrint:=False, Background:=False
template.PrintOut Range:=wdPrintAllDocument, _
Item:=wdPrintDocumentWithMarkup, _
Copies:=1, _
Pages:="1,2", _
PageType:=wdPrintAllPages, _
ManualDuplexPrint:=True, _
Collate:=True, _
Background:=False, _
PrintToFile:=False, _
PrintZoomColumn:=0, _
PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0
Next i
.Quit
End With
Set t = Nothing
Set template = Nothing
End Sub