我要向word中导入表格式的报表,写下面一段代码,刚开始运行正常,后来不知什么原因,就出错了,红色部分出错,系统提示"至少一个参数没有被指定值“,求大师们帮忙看一下下面的代码,帮助修改一下!谢谢了!
Private Sub Command2_Click()
On Error GoTo 打印时间
Dim objApp As Object 'Word.Application
Dim objDoc As Object 'Word.Document
Dim objTable As Object 'Word.Table
Dim strTemplates As String '模板文件路径名
Dim strFileName As String '将数据导出到此文件
Dim I As Integer
strTemplates = CurrentProject.Path & "\模板\登记表.dotx"
'打开模板文件
Set objApp = CreateObject("Word.Application")
objApp.Visible = True
Set objDoc = objApp.Documents.Open(strTemplates, , True)
lngI = Nz(DCount("*", "打印查询"), 0)
Set objTable = objDoc.Tables(1)
For I = 1 To lngI - 1
Set rowNew = objTable.Rows.Add(beforeRow:=objTable.Rows(2))
Next
'向Word中对应表格单元格写入数据
I = 2
With objTable
Set rst = CurrentProject.Connection.Execute("select 名称,生产厂家,产品规格 from 打印查询")
Do While Not rst.EOF
.Cell(I, 1).Range = rst("名称")
.Cell(I, 2).Range = rst("生产厂家")
.Cell(I, 3).Range = rst("产品规格")
I = I + 1
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
End With
打印时间:
With objApp.Application.Selection
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
With .Find
.Text = "{单位}"
.Replacement.Text = Me.Text3
End With
.Find.Execute Replace:=wdReplaceAll
With .Find
.Text = "{时间}"
.Replacement.Text = Me.Text5
End With
.Find.Execute Replace:=wdReplaceAll
With .Find
.Text = "{经办人}"
.Replacement.Text = Me.Text7
End With
.Find.Execute Replace:=wdReplaceAll
End With
End Sub