前段时间在守柔版主的指点下我在excel表中编写了一段代码如下:
目的:利用当前excel中活动单元格所在行的内容生成固定格式的word文档!
问题:1、目前代码可以使用,但我觉得有点冗长,能不能简化?
2、将excel表中数据写入word时是以回车符为标志,如果文档比较大的话将会很麻烦请问有没有别的解决办法?
Private Sub CommandButton2_Click()
Dim appWD As Word.Application, MyDoc As Word.Document, myRange As Word.Range, wd As Word.Application
Dim TF As Boolean, I As Byte, myString As String, mc As String
Dim ra%
On Error Resume Next
Set appWD = GetObject(, "Word.Application")
ra = ActiveCell.Row
mc = Cells(ra, 1)
If Err.Number <> 0 Then
Err.Clear
Set appWD = CreateObject("Word.Application")
TF = True
End If
Set MyDoc = appWD.Documents.Open(Filename:=ThisWorkbook.Path & "\文书\协议书.doc", Visible:=False)
With MyDoc
For I = 1 To 11
Select Case I
Case 1
myString = ""
Case 2
myString = ""
Case 3
myString = ""
Case 4
myString = ""
Case 5
myString = ""
Case 6
myString = Cells(ra, 2) '名称
Case 7
myString = ""
Case 8
myString = ""
Case 9
myString = Cells(ra, 1) '代码
Case 10
myString = ""
Case 11
myString = Cells(ra, 7) '电话
End Select
Set myRange = .Range(.Paragraphs(I).Range.Start, .Paragraphs(I).Range.End - 1)
myRange.InsertAfter myString
Next
.SaveAs ThisWorkbook.Path & "\文书\协议书存档\" & Cells(ra, 1)
.Close
If TF = True Then appWD.Quit: Set appWD = Nothing
End With
Set wd = CreateObject("word.application")
wd.Visible = True
wd.Documents.Open ("\文书\协议书存档\" & mc)
End Sub
[em04] |