先看一下实例再说!
两个注意事项:
一是WORD的宏安全性必须为低.如果为非低,请在工具/宏/宏安全性中设置为低后退出并重启WORD
二是我已将数据源中的随机列数据全部删除,由VBA自动产生随机数,可请下面的代码并进行你需要的修改.
ZLOUYVzA.rar
(21.05 KB, 下载次数: 18)
以下代码供参考:
'* +++++++++++++++++++++++++++++
'* Created By I Love You_Word!@ExcelHome 2005-4-14 5:38:42
'仅测试于System: Windows NT Word: 10.0 Language: 2052
'^The Code CopyIn [ThisDocument-ThisDocument]^'
'* -----------------------------
Dim MyApp As New EventClassModule
Private Sub Document_Open()
Set MyApp.App = Word.Application
End Sub
'----------------------
'* +++++++++++++++++++++++++++++
'* Created By I Love You_Word!@ExcelHome 2005-4-14 5:38:53
'仅测试于System: Windows NT Word: 10.0 Language: 2052
'^The Code CopyIn [类模块-EventClassModule]^'
'* -----------------------------
Public WithEvents App As Word.Application '在类模块中声明对应于事件的对象变量。
'编写指定事件的过程。
Private Sub App_MailMergeBeforeRecordMerge(ByVal Doc As Document, Cancel As Boolean)
Dim i As Byte
VBA.Randomize
With Doc.Tables(1)
For i = 1 To 8
.Cell(11, i + 4).Range = VBA.Round(IIf(Rnd > 0.5, Rnd * Rnd + 0.5, -(Rnd * Rnd + 0.5)), 1)
.Cell(12, i + 4).Range = VBA.Round((2 * Rnd + 2) * 0.01, 2)
.Cell(15, i + 4).Range = VBA.Round((2 * Rnd + 1) * 0.1 + 5, 1)
.Cell(16, i + 4).Range = VBA.Round((2 * Rnd + 1) * 0.1 + 10, 1)
.Cell(17, i + 4).Range = VBA.Round(2 * Rnd * 0.1 + 3, 1)
.Cell(18, i + 4).Range = VBA.Round((2 * Rnd + 6) * 0.1 + 3, 1)
Next
End With
End Sub
'----------------------
|