|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 182197315 于 2017-9-27 19:50 编辑
帮你写了一个,代码如下:
Private Sub CommandButton生成Word文件_Click()
Dim WordApp As New Word.Application
Dim i%, arr
FileCopy ThisWorkbook.Path & "\资本市场周报(模板).docx", ThisWorkbook.Path & "\资本市场周报.docx"
arr = Array(Cells(5, 2), Cells(5, 5), Cells(6, 5), Cells(7, 5), Cells(8, 5), Cells(11, 2), Cells(11, 5), Cells(12, 2), Cells(12, 5), Cells(13, 2), Cells(13, 5), Cells(14, 2), Cells(14, 5), Cells(15, 2), Cells(15, 5))
With WordApp
.Documents.Open ThisWorkbook.Path & "\资本市场周报.docx"
.Visible = True
For i = 1 To 15
.Selection.HomeKey Unit:=wdStory '光标置于文件首
If .Selection.Find.Execute("数据" & i) Then
.Selection.Text = arr(i - 1)
.Selection.Font.Color = wdColorAutomatic
End If
Next i
End With
WordApp.Documents.Save
WordApp.Quit
Set WordApp = Nothing
End Sub
|
|