今天继续讲讲和EXCEL VBA和其他应用程序交互吧
一、利用word的邮件功能生成多个页面
假设EXCEL中有这些内容,每一行数据都需要在word中单独生成一页
如果word需要一张一张做需要蛮久,还可能出错,利用Word的合并邮件功能,可以一次性把这些邮件全部发送,具体代码如下:
Sub wordMailMerge()
Dim wd As Word.Application
Dim wdDoc As Word.Document
Dim myRng As Excel.Range
Dim myCell As Excel.Range
Dim address As String
Dim topic As String
Dim content As String
Set wd = New Word.Application
Set wdDoc = wd.Documents.Add
wd.Visible = True
Set myRng = Sheets("Sheet1").Range("B2:B41")
For Each myCell In myRng.Cells
address = myCell.Value
topic = myCell.Offset(0, 1).Value
content = myCell.Offset(0, 2).Value
wd.Selection.InsertFile