我有以下代码:
Sub RunMerge()
Dim wd As Object
Dim wdocSource As Object
Dim Last_Row As Integer
Dim strWorkbookName As String
Last_Row = Worksheets("_Main").Cells(52, 6).Value
Const wdFormLetters = 0, wdOpenFormatAuto = 0
Const wdSendToNewDocument = 0, wdDefaultFirstRecord = 1, wdDefaultLastRecord = -16
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:\Users\eviera\Desktop\CertificateTemplate\CertificateTemplate_Re-entryV1.docx")
strWorkbookName = ThisWorkbook.Path & "\" & ThisWorkbook.Name
wdocSource.MailMerge.MainDocumentType = wdFormLetters
wdocSource.MailMerge.OpenDataSource _
Name:=strWorkbookName, _
AddToRecentFiles:=False, _
Revert:=False, _
Format:=wdOpenFormatAuto, _
Connection:="Data Source=" & strWorkbookName & ";Mode=Read", _
SQLStatement:="SELECT * FROM `_Main$`"
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
下面,我不能在不声明常量的情况下运行代码: wdDefaultFirstRecord = 1,wdDefaultLastRecord = -16必须保持恒定值
“我大约有50个插槽。当我从excel读取数据以与生成证书的Word文档合并时,我得到了很多空证书。我试图做的只是在可打印的Word文档上带有数据的证书。当我尝试打印时,它不允许我只说1-4。
With wdocSource.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
使用上面的代码,我试图强制最后一条记录从单元格中读取以确定最后一条记录。无济于事,它在顶部提供了一个空证书,然后在单词上仅显示一条记录。“
有人可以帮助我吗?我只希望程序读取带有数据的字段,而不是空插槽。读取空插槽会导致打印空证书。