|
Option Explicit
Sub TEST3()
Dim wdApp As Word.Application, strFileName$, strPath$
Dim ar, i&, j&, f$, strSavePath$, 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
strSavePath = ThisWorkbook.Path & "\生成文件\"
If Dir(strSavePath, vbDirectory) = "" Then MkDir strSavePath
For i = 2 To UBound(ar)
With wdApp.documents.Open(strFileName)
f = strSavePath & ar(i, 1)
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
.Select
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.Select
Rng.Font.Strikethrough = True
Loop
End With
With .Content.Find
.ClearFormatting
.Text = "预备/正式"
.Forward = True
Do While .Execute
With .Parent
.Select
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.Select
Rng.Font.Strikethrough = True
Loop
End With
.SaveAs f: .Close
End With
Next i
If Err <> 0 Then wdApp.Quit
Set wdApp = Nothing
Application.ScreenUpdating = True
Beep
End Sub
|
评分
-
1
查看全部评分
-
|