|
这几天玩代码,特别是SQL查询,从原来的一窍不通,到现在的有所了解,感慨颇多,WORD(OFFICE为我们提供了丰富的获取外部数据的功能),比如,邮件合并,AUTOMATION(自动化),ADO,DAO,DDE等等,总是让我们叹为观止,折服于软件的设计者的智慧!以下代码,是我自己琢磨的,供参考:'* +++++++++++++++++++++++++++++
'* Created By I Love You_Word!@ExcelHome 2005-4-28 10:36:25
'仅测试于System: Windows NT Word: 10.0 Language: 2052
'^The Code CopyIn [ThisDocument-ThisDocument]^'
'* -----------------------------Option Explicit
Sub TEST()
Dim intCount As Integer, MyString As String
Application.ScreenUpdating = False '关闭屏幕更新
With ActiveDocument.MailMerge '活动文档邮件合并功能
'打开数据源,设置查询语句
.OpenDataSource Name:="C:\Documents and Settings\My Documents\我的数据源\Hongda GID.xls", _
ReadOnly:=True, Connection:="Email", SQLStatement:="SELECT * FROM `Sheet1$` WHERE Empid>300411"
CommandBars("Mail Merge").Visible = False '关闭邮件合并工具栏
With .DataSource
.ActiveRecord = wdFirstRecord '设置活动数据记录
Do
intCount = intCount + 1 '计数
'获得指定列(域)的记录数据
MyString = MyString & .DataFields(3).Value & vbCrLf
.ActiveRecord = wdNextRecord '将数据源中下一条记录变成活动记录
Loop Until intCount >= .ActiveRecord
End With
.MainDocumentType = wdNotAMergeDocument '设置为普通文档
Selection.InsertAfter MyString '将变量文本在光标处插入
End With
Application.ScreenUpdating = True '恢复屏幕更新
End Sub
'---------------------- |
|