meohenmiao
Thành viên mới

- Tham gia
- 28/4/09
- Bài viết
- 8
- Được thích
- 0
Em có file đính kèm bác nào giúp em nó sai ở đâu mà cứ báo lỗi hoài! Em cám ơn nhiều ạ!
Đây là code VBA
Trong file word thì có 1 bookmark tên là HO_TEN, và 2 Checkbox ten la NAM, NU
Đây là code VBA
Mã:
Sub Taofileword()
Dim wdApp As Object
Dim wdDoc As Object
Set wdApp = CreateObject("Word.Application")
Set wdDoc = wdApp.Documents.Add("C:\Users\GAMING\Desktop\TEST.doc") 'duong dan file word
FillBookmark wdDoc, "NGUYEN VAN A", "HO_TEN"
FillBookmark wdDoc, " ", "NAM"
wdApp.Visible = True
wdApp.Activate
End Sub
Sub FillBookmark(ByRef wdDoc As Object, _
ByVal vValue As Variant, _
ByVal sBmName As String, _
Optional sFormat As String)
Dim wdRng As Object
'store the bookmarks range
Set wdRng = wdDoc.Bookmarks(sBmName).Range
'Kiem tra book mask
If wdDoc.FormFields(sBmName).Type = wdFieldFormCheckBox Then
wdDoc.FormFields(sBmName).CheckBox.Value = True
Exit Sub ' bo qua
End If
'if the optional format wasn’t supplied
If Len(sFormat) = 0 Then
'replace the bookmark text
wdRng.Text = vValue
Else
'replace the bookmark text with formatted text
wdRng.Text = Format(vValue, sFormat)
End If
're-add the bookmark because the above destroyed it
wdRng.Bookmarks.Add sBmName, wdRng
End Sub
Trong file word thì có 1 bookmark tên là HO_TEN, và 2 Checkbox ten la NAM, NU