hungyen2010
Thành viên mới

- Tham gia
- 18/10/10
- Bài viết
- 10
- Được thích
- 0
Mình viết chương trình in khi chọn vào listbox sẽ in file word thông tin có trong sheet1, mình làm bằng mergmail ở file word nhưng ở phần kết nối csdl sang file excel thì không chạy mong người chỉ giúp
code
Private Sub cmdPrint_Click()
Dim wd As Object
Dim wdocSource As Object
Dim wexcelSource As Object
Dim strWorkbookName As String
On Error Resume Next
Set wd = GetObject(, "Word.Application")
If wd Is Nothing Then
Set wd = CreateObject("Word.Application")
End If
On Error GoTo 0
Set wdocSource = wd.documents.Open("c:\temp\vba_excel\tsdb.doc")
wdocSource.mailmerge.MainDocumentType = wdFormLetters
Set wexcelSource = wd.documents.Open("C:\temp\vba_excel\mergemai.xls")
'khong ket noi duoc
wdocSource.mailmerge.opendatasource _
Name:="C:\temp\vba_excel\mergemai.xls", _
AddToRecentFiles:=False, _
Revert:=False, _
Format:=wdOpenFormatAuto, _
Connection:="Data Source=" & "C:\temp\vba_excel\mergemai.xls" & ";Mode=Read", _
SQLStatement:="SELECT * FROM `Sheet1$`"
With wdocSource.mailmerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
wd.Visible = True
wdocSource.Close SaveChanges:=False
Set wdocSource = Nothing
Set wd = Nothing
End Sub
code
Private Sub cmdPrint_Click()
Dim wd As Object
Dim wdocSource As Object
Dim wexcelSource As Object
Dim strWorkbookName As String
On Error Resume Next
Set wd = GetObject(, "Word.Application")
If wd Is Nothing Then
Set wd = CreateObject("Word.Application")
End If
On Error GoTo 0
Set wdocSource = wd.documents.Open("c:\temp\vba_excel\tsdb.doc")
wdocSource.mailmerge.MainDocumentType = wdFormLetters
Set wexcelSource = wd.documents.Open("C:\temp\vba_excel\mergemai.xls")
'khong ket noi duoc
wdocSource.mailmerge.opendatasource _
Name:="C:\temp\vba_excel\mergemai.xls", _
AddToRecentFiles:=False, _
Revert:=False, _
Format:=wdOpenFormatAuto, _
Connection:="Data Source=" & "C:\temp\vba_excel\mergemai.xls" & ";Mode=Read", _
SQLStatement:="SELECT * FROM `Sheet1$`"
With wdocSource.mailmerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
wd.Visible = True
wdocSource.Close SaveChanges:=False
Set wdocSource = Nothing
Set wd = Nothing
End Sub
