|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub 邮件合并()
- Dim wdApp As Word.Application, wdDoc As Word.Document, strFileName$, strPath$
- Dim Arr, i&, j&, f$, fdName$
- strPath = ThisWorkbook.Path & ""
- strFileName = strPath & "模板.docx"
- If Dir(strFileName) = "" Then MsgBox "模板.docx文件不存在,请检查!", vbExclamation: Exit Sub
- fdName = ThisWorkbook.Path & "\生成的表格"
- If Dir(fdName, vbDirectory) = "" Then MkDir (fdName)
- Application.ScreenUpdating = False
- Arr = Sheet1.Range("A1").CurrentRegion
- On Error Resume Next
- Set wdApp = GetObject(, "Word.Application")
- If Err <> 0 Then
- Set wdApp = New Word.Application
- End If
- For i = 3 To UBound(Arr)
- With wdApp.Documents.Open(ThisWorkbook.Path & "\模板.docx")
- f = fdName & Arr(i, 1)
- For j = 1 To UBound(Arr, 2)
- With .Content.Find
- .Text = "数据" & Format(j, "00")
- .Replacement.Text = Arr(i, j)
- .Execute Replace:=wdReplaceAll
- End With
- Next j
- .SaveAs f: wdDoc.Close
- End With
- Next i
- wdApp.Quit
- Set wdApp = Nothing: Set wdDoc = Nothing
- Application.ScreenUpdating = True
- MsgBox "完成"
- End Sub
复制代码
|
|