如何使用VB代码将数据准确的打印在word模版上,以报表输出?高分相求!!

lingxiang 2003-02-25 09:44:16
我做了一个数据库程序,但报表比较复杂,现想使用VB代码取出数据,准确的填在已经画好的word模版上,做报表打印输出,我该怎样编这段VB代码?高分相求!!
...全文
113 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
shux 2003-02-25
  • 打赏
  • 举报
回复
要想打印文件,请使用:

wdApp.PrintOut <参数>...
shux 2003-02-25
  • 打赏
  • 举报
回复
Option Explicit

' 宣告一个新的 Word 应用对象
' 要先引用:Microsoft Word x.xx Library
' 调用实例:GenerateDocument "<Name>,<Sex>", "张三|男", "C:\MyDot.dot", "C:\NewFile.doc"

Private Function GenerateDocument(sTags, sValues, sSourcePath, sDestPath) As Boolean
Dim wdApp As New Word.Application

On Local Error GoTo ErrHandler

Dim arrTags() As String, arrValues() As String, iLoop As Integer

wdApp.Documents.Open sSourcePath

arrTags = Split(sTags, ",")

arrValues = Split(sValues, "|")

With wdApp.ActiveDocument.Content.Find
For iLoop = 0 To UBound(arrTags)
.Execute arrTags(iLoop), , True, , , , , , , arrValues(iLoop), 2
Next iLoop
End With

'退出和释放之前关闭这个word文档对象:
If Len(Dir(sDestPath)) > 0 Then
Kill sDestPath
End If

wdApp.ActiveDocument.SaveAs sDestPath
wdApp.ActiveDocument.Close
wdApp.Quit
Set wdApp = Nothing

GenerateDocument = True

On Local Error GoTo 0
Exit Function

'这是一个错误处理程序。如果在上面的应用程序执行中遇到错误的话,它返回一个错误信息。
ErrHandler:
MsgBox Err.Description & Err.Number, vbExclamation, "错误"

' 退出并且释放 Word 文件对象
wdApp.Quit
Set wdApp = Nothing

On Local Error GoTo 0
Exit Function
End Function


lingxiang 2003-02-25
  • 打赏
  • 举报
回复
能给个例子吗?
shux 2003-02-25
  • 打赏
  • 举报
回复
建议如下:
在Word中设计模板时,将需要动态打印的数据字段用<>括起来,如:<Name>,然后,在VB中调用Word的Find/Replace功能,将:<Name>替换成你想要的数据就行了。
xiaole18 2003-02-25
  • 打赏
  • 举报
回复
UP 我现在也面临这样的问题!
88480 2003-02-25
  • 打赏
  • 举报
回复
在Word樣本中需動態加入數據庫資料的地方定義bookmark﹐然后在程序中操作word替換這些bookmark﹐用如下代碼實現
MyWord = CreateObject("Word.Application")
MyWord.Visible = False
MyWord.Documents.Open(Word樣本文件)
MyWord.ActiveDocument.Bookmarks.Item(你定義的BookMark).Select()﹔
MyWord.Selection.Text = "從資料庫取出的相應資料"
...
MyWord.Saveas(新Word文件)
bruce tao88 2003-02-25
  • 打赏
  • 举报
回复
思路是这样的,先用WORD做好报表,再录制宏,向报表中填数据,
将录制好的“宏程序”写入VB中,稍加改动就好了,
缺点是慢,但格式很灵活。

807

社区成员

发帖
与我相关
我的任务
社区描述
VB 多媒体
社区管理员
  • 多媒体
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧