|
代码如下: 注意引用:word
- PATH1 = ThisWorkbook.Path & "\当前模板.docx"
- Path3 = ThisWorkbook.Path & "\结果"
- FIRSTROW = 4 '//标题所在行
- Set SH0 = Sheets("FFS#1")
- LASTROW = SH0.Range("B65536").End(3).Row
- Set FSO = CreateObject("Scripting.FileSystemObject")
- If FSO.FolderExists(Path3) = False Then
- MkDir Path3 '//创建文件夹
- End If
- For IROW = FIRSTROW + 1 To LASTROW
- Application.StatusBar = "总数:" & LASTROW - FIRSTROW & " 个 当前是第:" & IROW - FIRSTROW & " 个 当前序号名是:" & SH0.Cells(IROW, 1).Value
- DoEvents
- Set WordDoc = WordApp.Documents.Open(PATH1) '//打开word模板
- WordApp.Visible = False '关闭word文档的显示
- WordApp.Selection.HomeKey unit:=wdStory '光标置于文件首部
- Rem 单个替换
- STR1 = "|设备编号|"
- STR2 = SH0.Cells(3, 4).Value
- Rem 多次替换
- For I = 0 To 3
- Do While WordApp.Selection.Find.Execute(STR1) = True '查找到指定字符串
- WordApp.Selection.Text = STR2 '替换字符串
- WordApp.Selection.HomeKey unit:=wdStory '光标置于文件
- Loop
- Next
- For ICOL = 1 To SH0.Range("HZ" & FIRSTROW).End(xlToLeft).Column
- Rem 第一行
- WordApp.Selection.HomeKey unit:=wdStory '光标置于文件首部
- Rem 成行替换
- STR1 = "|" & Replace(Replace(SH0.Cells(FIRSTROW, ICOL).Value, " ", ""), Chr(10), "") & "|"
- If InStr(SH0.Cells(FIRSTROW, ICOL).Value, "日期") + InStr(SH0.Cells(FIRSTROW, ICOL).Value, "时间") > 0 Then
- Rem 日期格式
- STR2 = Format(SH0.Cells(IROW, ICOL).Value, "yyyy年MM月dd日")
- Else
- STR2 = SH0.Cells(IROW, ICOL).Value
- End If
- Rem 多次替换
- For I = 0 To 3
- Do While WordApp.Selection.Find.Execute(STR1) = True '查找到指定字符串
- WordApp.Selection.Text = STR2 '替换字符串
- WordApp.Selection.HomeKey unit:=wdStory '光标置于文件
- Loop
- Next
- Next ICOL
- WordApp.Visible = True '弹出word框
- WordDoc.SaveAs Path3 & "" & SH0.Cells(IROW, 1).Value & ".doc"
- WordDoc.Close True
- Next
- WordApp.Quit
- Set WordDoc = Nothing
复制代码 |
评分
-
1
查看全部评分
-
|