[备忘]PB通过OLE技术嵌入Word文档的方法
创建模板文件
首先,我们建立一个报表样式的模板文件,取名为“用户清单模板.dot”。根据需要录入静态文本,并在需要插入动态数据的地方插入“书签”。“书签”名字与PB数据源中列的名字一致,
Constant Integer ppLayoutBlank = 12
OLEObject ole_object
ole_object = Create OLEObject
Integer li_ret
//建立与Word的连接
li_ret = ole_object.ConnectToObject(“Word.application”)
If li_ret <> 0 Then
//如果Word还没有打开,则新建一个Word
li_ret = ole_object.ConnectToNewObject(“Word.application”)
If li_ret <> 0 Then
MessageBox(‘OLE错误’,‘OLE无法连接! 错误号 :’ + String(li_ret))
Return
End If
ole_object.Visible = False
End If
String ls_modelfilename
ls_modelfilename = sle_1.Text
String ls_savefilename
ls_savefilename = sle_2.Text
ole_object.Documents.Open(ls_modelfilename)
Constant Long wdWord9TableBehavior = 1
Constant Long wdAutoFitFixed = 0
Constant Long wdCell = 12
Long i,j,k,ll_colnum,ll_rownum
String ls_value,ls_colname
//输出清单表头
ll_colnum = Long(dw_1.unt)
dw_1.SetRedraw(False)
For j = 1 To ll_colnum
dw_1.SetColumn(j)object to
ls_value = dw_1.GetText()
ls_colname = dw_1.Describe(‘#’ + String(j) + “.name”)
If ls_colname = “name” Then ls_savefilename + = “\用户 ” + ls_value + “ 话费清单.doc”
ole_object.selection.Goto(True,0,0,ls_colname)
ole_object.selection.TypeText(ls_value)
For k = 1 To f_getcnnum(ls_value)
ole_object.selection.TypeBackspace()
Next
Next
dw_1.SetRedraw(True)
//输出清单
ll_colnum = Long(dw_2.unt)
ll_rownum = dw_2.RowCount() + 1
ole_object.selection.Goto(True,0,0,‘list’)
ole_object.ActiveDocument.Tables.Add(ole_object.selection.Range, ll_rownum, ll_colnum, wdWord9TableBehavior, wdAutoFitFixed)
ole_object.selection.MoveLeft(wdCell)
For i = 1 To ll_colnum
//得到标题头的名字
ls_colname = dw_2.Describe(‘#’ + String(i) +“.name”) +“_t”
ls_value = dw_2.Describe(ls_colname + “.Text”)
ole_object.selection.TypeText(ls_value)
For k = 1 To f_getcnnum(ls_value)
ole_object.selection.TypeBackspace()
Next
ole_object.selection.MoveRight(wdCell)
Next
//输出清单数据
dw_2.SetRedraw(False)
ole_object.selection.MoveLeft(wdCell)
For i = 2 To ll_rownum
For j = 1 To ll_colnum
dw_2.ScrollToRow(i - 1)
dw_2.SetColumn(j)
ls_value = dw_2. Get
Text()
ole_object.selection.MoveRight(wdCell)
ole_object.selection.TypeText(ls_value)
For k = 1 To f_getcnnum(ls_value)
ole_object.selection.TypeBackspace()
Next
Next
Next
dw_2.SetRedraw(True)
Constant Long wdFormatDocument = 0
/
/保存用户详细话单
ole_object.ActiveDocument.SaveAs(ls_savefilename,1)
ole_object.application.Quit()
ole_object.DisConnectObject() //断开OLE连接
Destroy ole_object
MessageBox(“提示”,“用户详细话单Word文档已经生成!”)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论