|
- Sub test()
- Dim r%, i%
- Dim arr, brr
- Dim mypath$, myname$
- Dim wordapp As Object
- Dim mydoc As Object
- Dim flg As Boolean
- vs = [{"<学生>",2;"<班主任>",12}]
- If Dir(ThisWorkbook.Path & "\通知书(模板).doc") = "" Then
- MsgBox ThisWorkbook.Path & "\通知书(模板).doc不存在!"
- Exit Sub
- End If
- With Worksheets("数据")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- arr = .Range("a2:l" & r)
- End With
- On Error Resume Next
- Set wordapp = GetObject(, "word.application")
- If Err Then
- flg = True
- Set wordapp = CreateObject("word.application")
- End If
- On Error GoTo 0
- wordapp.Visible = True
- If Dir(ThisWorkbook.Path & "\通知书", vbDirectory) = "" Then
- MkDir ThisWorkbook.Path & "\通知书"
- End If
- For i = 1 To UBound(arr)
- FileCopy ThisWorkbook.Path & "\通知书(模板).doc", ThisWorkbook.Path & "\通知书" & arr(i, 11) & "_" & arr(i, 2) & ".doc"
- Set mydoc = wordapp.Documents.Open(Filename:=ThisWorkbook.Path & "\通知书" & arr(i, 11) & "_" & arr(i, 2) & ".doc")
- With mydoc
- .Activate
- With .Shapes(1).TextFrame.TextRange
- With .Find
- .ClearFormatting
- .Replacement.ClearFormatting
- For k = 1 To UBound(vs)
- .Text = vs(k, 1)
- .Replacement.Text = arr(i, vs(k, 2))
- .Execute Replace:=2, Forward:=True, Wrap:=1
- Next
- End With
- With .Tables(1)
- For j = 3 To 9
- .Cell(2, j - 1).Range.Text = arr(i, j)
- Next
- .Cell(3, 2).Range.Text = arr(i, 10)
- With .Cell(3, 2).Range.ParagraphFormat
- .CharacterUnitFirstLineIndent = 2
- End With
- End With
- End With
- .Close True
- End With
- Next
- If flg Then
- wordapp.Quit
- End If
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|