VB访问word书签。
'实现代码如下
Dim cn As New ADODB.Connection
Dim AdoRs As New ADODB.Recordset
Dim WordTemps As New Word.Application
Private Sub Form_Load()
If cn.State = 1 Then
cn.Close
End If
cn.CursorLocation = adUseClient
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb"
End Sub
'开始导出数据
Private Sub Command1_Click()
Dim strSQl As String
Dim REC As Integer
Dim i As Integer
WordTemps.Documents.Add App.Path + "\货物合同.doc", False
WordTemps.Selection.GoTo wdGoToBookmark, , , "合同标题"
WordTemps.Selection.TypeText "关于冬季货物的成交合同"
WordTemps.Selection.GoTo wdGoToBookmark, , , "合同编号"
WordTemps.Selection.TypeText "2004000001"
WordTemps.Selection.GoTo wdGoToBookmark, , , "签约单位"
WordTemps.Selection.TypeText "宏大科技公司,天天科技公司"
WordTemps.Selection.GoTo wdGoToBookmark, , , "签约地址"
WordTemps.Selection.TypeText "北京中关村大厦"
WordTemps.Selection.GoTo wdGoToBookmark, , , "签约时间"
WordTemps.Selection.TypeText fromat(Now, "yyyy-mm-dd")
strSQl = "select * from Matrixs"
AdoRs.Open strSQl, cn, adOpenKeyset, adLockOptimistic
REC = AdoRs.RecordCount
If REC < 1 Then
MsgBox "无商品记录!", vbOKOnly, "提示"
AdoRs.Close
Exit Sub
Else
AdoRs.MoveFirst
WordTemps.Selection.GoTo wdGoToBookmark, , , "货物清单"
For i = 1 To REC
WordTemps.Selection.TypeText AdoRs!名称
WordTemps.Selection.MoveRight unit:=wdCharacter, Count:=1 '右移一格
WordTemps.Selection.TypeText AdoRs!数量
WordTemps.Selection.MoveRight unit:=wdCharacter, Count:=1 '右移一格
WordTemps.Selection.TypeText AdoRs!规格
AdoRs.MoveNext
If AdoRs.EOF = False Then
WordTemps.Selection.InsertRowsBelow 1 '表格换行
End If
Next i
AdoRs.Close
WordTemps.Visible = True '显示WORD窗口
End If
End Sub
'实现代码如下
Dim cn As New ADODB.Connection
Dim AdoRs As New ADODB.Recordset
Dim WordTemps As New Word.Application
Private Sub Form_Load()
If cn.State = 1 Then
cn.Close
End If
cn.CursorLocation = adUseClient
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb"
End Sub
'开始导出数据
Private Sub Command1_Click()
Dim strSQl As String
Dim REC As Integer
Dim i As Integer
WordTemps.Documents.Add App.Path + "\货物合同.doc", False
WordTemps.Selection.GoTo wdGoToBookmark, , , "合同标题"
WordTemps.Selection.TypeText "关于冬季货物的成交合同"
WordTemps.Selection.GoTo wdGoToBookmark, , , "合同编号"
WordTemps.Selection.TypeText "2004000001"
WordTemps.Selection.GoTo wdGoToBookmark, , , "签约单位"
WordTemps.Selection.TypeText "宏大科技公司,天天科技公司"
WordTemps.Selection.GoTo wdGoToBookmark, , , "签约地址"
WordTemps.Selection.TypeText "北京中关村大厦"
WordTemps.Selection.GoTo wdGoToBookmark, , , "签约时间"
WordTemps.Selection.TypeText fromat(Now, "yyyy-mm-dd")
strSQl = "select * from Matrixs"
AdoRs.Open strSQl, cn, adOpenKeyset, adLockOptimistic
REC = AdoRs.RecordCount
If REC < 1 Then
MsgBox "无商品记录!", vbOKOnly, "提示"
AdoRs.Close
Exit Sub
Else
AdoRs.MoveFirst
WordTemps.Selection.GoTo wdGoToBookmark, , , "货物清单"
For i = 1 To REC
WordTemps.Selection.TypeText AdoRs!名称
WordTemps.Selection.MoveRight unit:=wdCharacter, Count:=1 '右移一格
WordTemps.Selection.TypeText AdoRs!数量
WordTemps.Selection.MoveRight unit:=wdCharacter, Count:=1 '右移一格
WordTemps.Selection.TypeText AdoRs!规格
AdoRs.MoveNext
If AdoRs.EOF = False Then
WordTemps.Selection.InsertRowsBelow 1 '表格换行
End If
Next i
AdoRs.Close
WordTemps.Visible = True '显示WORD窗口
End If
End Sub