代码花费了我近3个小时,一杯咖啡从早上喝到现在还没喝完。其中,遭遇了一个“Runtime-error '9'”: Subscript out of range
,就耽误了很多时间。问题代码:With workbooks(fileName).VBProject
For Each objVbc In .VBComponents
VB访问
word
书签。 '实现代码如下 Dim cn As New ADODB.Connection Dim AdoRs As New ADODB.Recordset Dim
Word
Temps 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
Word
Temps.Documents.Add App.Path + "\货物合同.doc", False
Word
Temps.Selection.GoTo wdGoToBookmark, , , "合同标题"
Word
Temps.Selection.TypeText "关于冬季货物的成交合同"
Word
Temps.Selection.GoTo wdGoToBookmark, , , "合同编号"
Word
Temps.Selection.TypeText "2004000001"
Word
Temps.Selection.GoTo wdGoToBookmark, , , "签约单位"
Word
Temps.Selection.TypeText "宏大科技公司,天天科技公司"
Word
Temps.Selection.GoTo wdGoToBookmark, , , "签约地址"
Word
Temps.Selection.TypeText "北京中关村大厦"
Word
Temps.Selection.GoTo wdGoToBookmark, , , "签约时间"
Word
Temps.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
Word
Temps.Selection.GoTo wdGoToBookmark, , , "货物清单" For i = 1 To REC
Word
Temps.Selection.TypeText AdoRs!名称
Word
Temps.Selection.MoveRight unit:=wdCharacter, Count:=1 '右移一格
Word
Temps.Selection.TypeText AdoRs!数量
Word
Temps.Selection.MoveRight unit:=wdCharacter, Count:=1 '右移一格
Word
Temps.Selection.TypeText AdoRs!规格 AdoRs.MoveNext If AdoRs.EOF = False Then
Word
Temps.Selection.InsertRowsBelow 1 '表格换行 End If Next i AdoRs.Close
Word
Temps.Visible = True '显示
WORD
窗口 End If End Sub '实现代码如下 Dim cn As New ADODB.Connection Dim AdoRs As New ADODB.Recordset Dim
Word
Temps 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
Word
Temps.Documents.Add App.Path + "\货物合同.doc", False
Word
Temps.Selection.GoTo wdGoToBookmark, , , "合同标题"
Word
Temps.Selection.TypeText "关于冬季货物的成交合同"
Word
Temps.Selection.GoTo wdGoToBookmark, , , "合同编号"
Word
Temps.Selection.TypeText "2004000001"
Word
Temps.Selection.GoTo wdGoToBookmark, , , "签约单位"
Word
Temps.Selection.TypeText "宏大科技公司,天天科技公司"
Word
Temps.Selection.GoTo wdGoToBookmark, , , "签约地址"
Word
Temps.Selection.TypeText "北京中关村大厦"
Word
Temps.Selection.GoTo wdGoToBookmark, , , "签约时间"
Word
Temps.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
Word
Temps.Selection.GoTo wdGoToBookmark, , , "货物清单" For i = 1 To REC
Word
Temps.Selection.TypeText AdoRs!名称
Word
Temps.Selection.MoveRight unit:=wdCharacter, Count:=1 '右移一格
Word
Temps.Selection.TypeText AdoRs!数量
Word
Temps.Selection.MoveRight unit:=wdCharacter, Count:=1 '右移一格
Word
Temps.Selection.TypeText AdoRs!规格 AdoRs.MoveNext If AdoRs.EOF = False Then
Word
Temps.Selection.InsertRowsBelow 1 '表格换行 End If Next i AdoRs.Close
Word
Temps.Visible = True '显示
WORD
窗口 End If End Sub
前后期绑定Excel/
Word
对象
的应用
Set W = CreateObject(“
word
.application”) '这种是后期绑定
'注意,运行到这里是这里把程序重置以后,不管visible=true/false这里的
WORD
实例
对象
还是存在,
'记的EXCEL如果是这种情况下是会在进程中自动关掉的(见下方描述). 此处不管Public W或是dim W均是这样
W.Visible = True
VB6,Set E = CreateObject(“excel.application”)这句发生后,