|
VBA——Excel数据批量生成Word
一、示例代码
- Sub Excel批量生成Word文件()
- Dim exApp As Object
- Dim wb As Object
- Dim sht As Object
- Dim arr
- Dim row As Long
- Dim col As Long
- Dim doc As Document
- Dim newDoc As Document
- Dim cel As Word.Cell
- Dim p As String
-
- 'Set exApp = GetObject(, "Excel.Application")
-
-
- Set exApp = GetObject(Class:="Excel.Application")
- Set wb = exApp.Workbooks("安装地点1.xls")
- Set sht = wb.Sheets("序号")
- Set doc = ActiveDocument
-
- Set cel = doc.Tables(1).Cell(2, 2)
-
- 'Set cel = doc.Tables(1).Range.Cells(4)
-
- arr = sht.Range("A1").CurrentRegion.Value
-
- For row = LBound(arr, 1) + 1 To UBound(arr, 1)
- Debug.Print arr(row, 1), arr(row, 2)
- cel.Range.Text = arr(row, 2)
- p = ActiveDocument.Path & "\结果" & arr(row, 1) & ".docx"
- doc.SaveAs2 FileName:=p
- Next row
-
- Set exApp = Nothing
- Set wb = Nothing
- Set sht = Nothing
- Set doc = Nothing
- Set cel = Nothing
- End Sub
复制代码 二、注意事项
上述代码需要将Excel文件打开再运行
|
-
-
代码示例.zip
37.5 KB, 下载次数: 12
打开docm文件,运行模块测试
评分
-
1
查看全部评分
-
|