|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Option Explicit
Sub TEST3()
Dim wdApp As Word.Application, strFileName$, strPath$
Dim ar, i&, j&, strSaveName$, Rng As Word.Range
strPath = ThisWorkbook.Path & "\"
strFileName = strPath & "党组织关系介绍信样式.doc"
If Dir(strFileName) = "" Then MsgBox "模板.docx文件不存在,请检查!", vbExclamation: Exit Sub
Application.ScreenUpdating = False
ar = [A1].CurrentRegion.Value
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err <> 0 Then
Set wdApp = New Word.Application
End If
With wdApp.Documents.Add
strSaveName = strPath & "汇总"
For i = 2 To UBound(ar)
With wdApp.Documents.Open(strFileName)
For j = 1 To UBound(ar, 2)
With .Content.Find
.Text = "数据" & Format(j + 1, "000")
If j = 4 Then ar(i, j) = Mid(ar(i, j), 1, Len(ar(i, j)) - 1)
.Replacement.Text = ar(i, j)
.Execute Replace:=wdReplaceAll
End With
Next j
With .Content.Find
.Text = "数据001"
.Replacement.Text = 10000 + i - 1
.Execute Replace:=wdReplaceAll
End With
With .Content.Find
.ClearFormatting
.Text = "男/女"
.Forward = True
Do While .Execute
With .Parent
If ar(i, 2) = "男" Then
Set Rng = wdApp.ActiveDocument.Range(.Start + 2, .End)
Else
Set Rng = wdApp.ActiveDocument.Range(.Start, .Start + 1)
End If
End With
Rng.Font.Strikethrough = True
Loop
End With
With .Content.Find
.ClearFormatting
.Text = "预备/正式"
.Forward = True
Do While .Execute
With .Parent
If Mid(ar(i, 5), 1, 2) = "正式" Then
Set Rng = wdApp.ActiveDocument.Range(.Start, .Start + 2)
Else
Set Rng = wdApp.ActiveDocument.Range(.Start + 3, .End)
End If
End With
Rng.Font.Strikethrough = True
Loop
End With
.Range(0, .Content.End - 1).Copy
.Close False
End With
If i <> 2 Then wdApp.Selection.InsertBreak (7)
wdApp.Selection.Paste
Next i
.SaveAs strSaveName: .Close
End With
If Err <> 0 Then wdApp.Quit
Set wdApp = Nothing
Application.ScreenUpdating = True
Beep
End Sub
|
评分
-
2
查看全部评分
-
|