|
Sub 生成word()
Application.ScreenUpdating = False
Dim i%, ar, myPath$, wdApp, wdD
myPath = ThisWorkbook.Path & "\"
Call daochutubiao
Set wdApp = CreateObject("word.application")
wdApp.Visible = True
t = Timer
With Sheets("sheet1")
r = .Cells(Rows.Count, 2).End(xlUp).Row
ar = .Range("b7:e" & r)
rr = Array(.[b2], .[b3])
End With
Set wdD = wdApp.Documents.Open(myPath & "模板.doc")
With wdD.Tables(1)
hs = UBound(ar)
If hs > 15 Then
ys = hs - 15
For i = 1 To ys
Set oRow = .Rows(2)
'在第2行前面插入行
.Rows.Add oRow
Next i
End If
For i = 1 To UBound(ar)
For j = 1 To 4
.Cell(i, j).Range.Text = ar(i, j)
Next j
Next i
End With
Set mt = wdD.Tables(2)
tp = Format(Date, "yyyymmdd") & ".jpg"
Set oSP = mt.Cell(1, 1).Range.InlineShapes.AddPicture(Filename:=ThisWorkbook.Path & "\" & tp, LinkToFile:=False, SaveWithDocument:=True)
oSP.Width = mt.Cell(1, 1).Width
oSP.Height = mt.Cell(1, 1).Height
For j = 1 To 2
wdApp.Selection.HomeKey unit:=6 '光标置于文件首
If wdApp.Selection.Find.Execute("数据" & j) Then '查找到指定字符串
wdApp.Selection.Font.Color = wdColorAutomatic '字符为自动颜色
wdApp.Selection.Text = rr(j - 1) '替换字符串
End If
Next j
wdD.SaveAs Filename:=myPath & "实验数据" & Format(Date, "yyyymmdd") & ".doc"
'wdD.Close
wdApp.Quit
Set wdD = Nothing
Set wdApp = Nothing
Application.ScreenUpdating = False
MsgBox "耗时:" & Format(Timer - t, "0.00") & "秒!"
End Sub
|
|