Const nNTNB = "\Nghiem thu noi bo", nNTNBdoc = nNTNB & ".DOC", nNTNBdot = nNTNB & ".DOT"
Const nYCNT = "\Yeu cau nghiem thu", nYCNTdoc = nYCNT & ".DOC", nYCNTdot = nYCNT & ".DOT"
Const nNTCV = "\Nghiem thu cong viec xay dung", nNTCVdoc = nNTCV & ".DOC", nNTCVdot = nNTCV & ".DOT"
Const nbDay = "pDay", nbDay1 = "pDay1", nbDay2 = "pDay2", nbDay3 = "pDay3"
Const nbMon = "pMon", nbMon1 = "pMon1", nbMon2 = "pMon2"
Const nbYear = "pYear", nbYear1 = "pYear1", nbYear2 = "pYear2"
Const nbHour1 = "Hour1", nbHour2 = "Hour2", nbWork = "nWork"
Dim ra, objW As Object, docW As Object 'objW As Word.Application, docW As Word.Document '
Dim hh, tt, D, M, Y
Dim pName As String
Private Sub setBookMark(nBB As String, ParamArray bMs())
Dim dd, mm
Set docW = objW.Documents.Add(pName & nBB)
With docW
.Bookmarks(nbWork).Range.Text = ra.Cells(1)
Select Case nBB
Case nNTNB
dd = Format(Day(D - 1), "0#")
.Bookmarks(nbDay).Range.Text = dd
.Bookmarks(nbDay1).Range.Text = dd
.Bookmarks(nbDay2).Range.Text = dd
.Bookmarks(nbMon).Range.Text = M
.Bookmarks(nbMon1).Range.Text = M
.Bookmarks(nbMon2).Range.Text = M
.Bookmarks(nbYear).Range.Text = Y
.Bookmarks(nbYear1).Range.Text = Y
.Bookmarks(nbYear2).Range.Text = Y
Do
dd = 8 + Int(Rnd() * 8)
mm = Int(Rnd() * 2) * 30
Loop While (dd = 11 And mm = 30) Or dd = 12 Or dd = 13
.Bookmarks(nbHour1).Range.Text = Format(dd, "0#") & "h" & Format(mm, "0#")
.Bookmarks(nbHour2).Range.Text = Format(dd + 1, "0#") & "h" & Format(mm, "0#")
Case nYCNT
.Bookmarks(nbDay).Range.Text = Format(D, "dd/mm/yyyy")
.Bookmarks(nbDay1).Range.Text = Format(D, "dd/mm/yyyy")
.Bookmarks(nbDay2).Range.Text = Format(D - 1, "dd/mm/yyyy")
.Bookmarks(nbDay3).Range.Text = Format(D - 1, "dd/mm/yyyy")
.Bookmarks(nbHour1).Range.Text = Format(hh, "0#") & "h" & Format(tt, "0#")
Case nNTCV
dd = Format(Day(D), "0#")
.Bookmarks(nbDay).Range.Text = dd
.Bookmarks(nbDay1).Range.Text = dd
.Bookmarks(nbDay2).Range.Text = dd
.Bookmarks(nbMon).Range.Text = M
.Bookmarks(nbMon1).Range.Text = M
.Bookmarks(nbMon2).Range.Text = M
.Bookmarks(nbYear).Range.Text = Y
.Bookmarks(nbYear1).Range.Text = Y
.Bookmarks(nbYear2).Range.Text = Y
.Bookmarks(nbHour1).Range.Text = Format(hh, "0#") & "h" & Format(tt, "0#")
.Bookmarks(nbHour2).Range.Text = Format(hh + 1, "0#") & "h" & Format(tt, "0#")
End Select
End With
docW.PrintPreview
'docW.SaveAs Filename:=ThisWorkbook.Path & nNTNB & ".DOC"
Set docW = Nothing
End Sub
Sub cDoc()
Dim Traloi
Set ra = Selection.Offset(0, 1).Resize(1, 2)
Traloi = MsgBox(Prompt:="Tao cac bien ban nghiem thu Y/N ? " & Chr(10) & Chr(13) & _
ra(1, 1), Buttons:=vbYesNo, Title:="KIEM TRA")
If Traloi = vbNo Then Exit Sub
'On Error Resume Next'
Set objW = CreateObject("Word.Application")
objW.Visible = True
pName = ThisWorkbook.Path 'Dieu chinh cho phu hop'
Randomize
D = ra.Cells(2): M = Format(Month(D), "0#"): Y = Year(D)
Do
hh = 8 + Int(Rnd() * 8)
tt = Int(Rnd() * 2) * 30
Loop While (hh = 11 And tt = 30) Or hh = 12 Or hh = 13
Call setBookMark(nNTNB)
Call setBookMark(nYCNT)
Call setBookMark(nNTCV)
Set objW = Nothing
End Sub