这段时间一直在设计报表输出,但参考资料太少了,我想好多人和我又同感经过几天摸索,现在终于能实现用Word输出报表了,现在把这段程序附上让大家参考
几点说明:
必须在"工程"下拉菜单"引用"中要选择"microsoft word..."项,否则要出错
'fgString为DataGrid1或MSHFlexGrid控件名
'tp_Que.sCount 在此是一个变量为fgString的列数
Public Sub AddOut_Word() ’定义的一个调用过程
Dim wdapp As Word.Application
Dim wddoc As Word.Document
Dim atable As Word.Table
Dim i As Integer, j As Integer
Dim s As Integer, t As Integer
On Error Resume Next
s = tp_Que.sCount + 1 '新表格行数
t = 4 '新表格列数
'创建Word应用程序
Set wdapp = CreateObject("Word.application")
'在Word中添加一个新文挡
Set wddoc = wdapp.Documents.Add
With wdapp
.Visible = True
.Activate
'在Word中增加一个表格
Set atable = .ActiveDocument.Tables.Add(.Selection.Range, s, t - 1)
For i = 0 To s - 1
For j = 1 To t - 1
fgString.Row = i
fgString.Col = j
atable.Cell(i + 1, j).Range.InsertAfter fgString.Text
Next j
Next i
End With
Set wdapp = Nothing
Set wddoc = Nothing
End Sub
[此贴子已经被作者于2006-3-27 11:17:02编辑过]