|
本帖最后由 weiyingde 于 2017-8-21 23:16 编辑
要求:答案从Excel分别回灌到word文档相对位置‘
问题:错误地将Excel所有答案回灌word文档第一个位置。
代码如下:
Sub 文档回灌()
Dim xlapp As New Excel.Application, arr
n = 0 '给题和题枝干加别加题号编号
With ActiveDocument
For Each par In .Paragraphs
If Len(par) > 1 Then n = n + 1
Next
For i = 2 To n
If (i - 2) Mod 5 = 0 Then
tx1 = .Paragraphs(i).Range.Text
.Paragraphs(i).Range.Text = 1 + Int((i - 2) / 5) & "." & tx1
Else
ys = (i - 2) Mod 5
tx2 = .Paragraphs(i).Range.Text
.Paragraphs(i).Range.Text = Chr(64 + ys) & "、" & tx2
End If
Next
With xlapp.Workbooks.Open(Left(.FullName, Len(.FullName) - 4) & "xlsx").ActiveSheet
arr = .Range("G1:G" & .Cells(65536, 7).End(xlUp).Row)
End With
xlapp.Quit
.Range(0, Len(.Paragraphs(1).Range.Text) - 1) = Left(.Name, Len(.Name) - 5)
End With
ActiveDocument.Content.Find.Execute "([\((])[" & ChrW(12288) & ChrW(32) & ChrW(160) & "]{1,}([)\)])", , , 1, , , , , , "\1\2", 2
With ActiveDocument.Content.Find '回灌答案
Do While .Execute("\][\((]", , , 1)
txt$ = .Parent.Text
k = k + 1
.Parent.Text = txt$ & arr(k , 1)
Loop
End With
End Sub
注意,附件代码中红字部分笔误了,特此提醒。
|
|