|
本帖最后由 182197315 于 2017-11-13 22:46 编辑
Sub 生成Word文件()
Dim wdapp As new Word.Application
Dim arr, brr(1 To 3), myPath$, i%, k%, sr$
myPath = ThisWorkbook.Path & "\"
arr = Range("A1").CurrentRegion
brr(1) = arr(1, 1)
'brr(2) = WorksheetFunction.Now()
brr(2) = "2017"
FileCopy myPath & "XX方剂 (模板).docx", myPath & arr(1, 1) & ".docx"
For i = 3 To UBound(arr)
sr = sr & arr(i, 2) & arr(i, 8) & arr(i, 9) & ","
Next
brr(3) = Left(sr, Len(sr) - 1)
With wdapp
.Documents.Open myPath & arr(1, 1) & ".docx"
.Visible = True
For k = 1 To 3
.Selection.HomeKey Unit:=wdStory '光标置于文件首
If .Selection.Find.Execute("数据" & k) Then '查找到指定字符串
.Selection.Font.Color = wdColorAutomatic '字符为自动颜色
.Selection.Text = brr(k) '替换字符串
End If
Next
End With
wdapp.Quit
End Sub
|
|