因工作需要,要把Access中的数据放到固定格式中Word中,本人从没学过VBA,但是想快点解决问题,于是从网上找了一段代码,想稍加修改后拿来用。但在运行中遇到一些问题,望好心人指点。 程序如下: Sub start() Dim i, j, k, m, nrecord, txtnumber As Integer Dim aname(1 To 20) As String Set md = DBEngine.OpenDatabase("F:\study\db1.mdb") Set rs = md.OpenRecordset("car") Set mydoc1 = ActiveDocument txtnumber = mydoc1.Characters.Count Set range1 = mydoc1.Range(start:=0, End:=txtnumber) range1.Copy mydoc2 = Documents.Add Selection.Paste Set range2 = mydoc2.Range(start:=0, End:=txtnumber) mydoc1.Activate On Error Resume Next rs.MoveLast nrecord = rs.RecordCount On Error GoTo doerror For k = 1 To 20 aname(k) = rs.Fields(k).Name Next k doerror: For m = 1 To nrecord If m = 1 Then rs.MoveFirst Else rs.MoveNext For k = 1 To 5 totalnumber = mydoc1.Bookmarks.Count '已存在的书签数 For i = totalnumber To 1 Step -1 bname = rs.Fields(aname(k)) If UCase$(mydoc1.Bookmarks(i).Name) = UCase$(aname(k)) Then mydoc1.Bookmarks(i).Select Selection.TypeText Text:=bname End If Next i Next k Selection.MoveDown Unit:=wdLine, Count:=4 If m < nrecord Then mydoc2.Activate range2.Copy mydoc1.Activate Selection.Paste End If Next m Documents(mydoc2).Close savechanges:=wdDoNotSaveChanges End Sub
运行到Set range2 = mydoc2.Range(start:=0, End:=txtnumber)时,提示“运行时错误424,要求对象”。 我先把这行删除,运行到bname = rs.Fields(aname(k))时,提示“运行时错误3265,这个集合中找不到此项目”。
请问如何解决?
原文中说他在Word97(2000)中调试运行,而我现在用的word和access都是2003版的,不知道有没有影响。
|