|
热浪老师,这代码 循环第一次正常 之后的就在mysel = ActiveDocument.Tables(2).Cell(2, 1)报出自动化错误是怎么回事呢 求指教。
Private Sub CommandButton输出通知到Word文件_Click()
Dim Word对象 As New Word.Application, 当前路径, 导出文件名, 导出路径文件名, i, mysel
Dim Str1, Str2
当前路径 = ThisWorkbook.Path
最后行号 = Sheets("数据").Range("B65536").End(xlUp).Row
判断 = 0
For i = 2 To 最后行号
导出文件名 = Sheets("数据").Range("E" & i)
FileCopy 当前路径 & "\报告模板.doc", 当前路径 & "\" & 导出文件名 & "M000.doc"
导出路径文件名 = 当前路径 & "\" & 导出文件名 & "M000.doc"
With Word对象
.Documents.Open 导出路径文件名
.Visible = False
'填写文字数据
Str1 = "SJ" & "001"
Str2 = Sheets("数据").Cells(i, 5)
.Selection.HomeKey Unit:=wdStory '光标置于文件首
If .Selection.Find.Execute(Str1) Then '查找到指定字符串
.Selection.Font.Color = wdColorAutomatic '字符为自动颜色
.Selection.Text = Str2 '替换字符串
End If
Str1 = "SJ" & "002"
Str2 = Sheets("数据2").Cells(2, 2)
.Selection.HomeKey Unit:=wdStory '光标置于文件首
If .Selection.Find.Execute(Str1) Then '查找到指定字符串
.Selection.Font.Color = wdColorAutomatic '字符为自动颜色
.Selection.Text = Str2 '替换字符串
End If
Str1 = "SJ" & "003"
Str2 = Sheets("数据2").Cells(2, 1)
.Selection.HomeKey Unit:=wdStory '光标置于文件首
If .Selection.Find.Execute(Str1) Then '查找到指定字符串
.Selection.Font.Color = wdColorAutomatic '字符为自动颜色
.Selection.Text = Str2 '替换字符串
End If
Str1 = "SJ" & "006"
Str2 = Sheets("数据2").Cells(2, 2)
.Selection.HomeKey Unit:=wdStory '光标置于文件首
If .Selection.Find.Execute(Str1) Then '查找到指定字符串
.Selection.Font.Color = wdColorAutomatic '字符为自动颜色
.Selection.Text = Str2 '替换字符串
End If
mysel = ActiveDocument.Tables(2).Cell(2, 1)
mysel.InlineShapes.AddPicture Filename:="C:\正面.png", LinkToFile:=True, SaveWithDocument:=True
.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader '设置位置在页眉
Str1 = "SJ011"
Str2 = Sheets("数据").Cells(i, 5)
.Selection.HomeKey Unit:=wdStory '光标置于文件首
If .Selection.Find.Execute(Str1) Then '查找到指定字符串
.Selection.Font.Color = wdColorAutomatic '字符为自动颜色
.Selection.Text = Str2 '替换字符串
End If
End With
Word对象.Documents.Save
Word对象.Quit
Set Word对象 = Nothing
Next i
If 判断 = 0 Then
i = MsgBox("已输出到 Word 文件!", 0 + 48 + 256 + 0, "提示:")
End If
End Sub
|
|