我正在尝试使用以下代码从MS-Access的Visual basic6在Excel中构建文档,该代码调用在数据库中执行查询的函数TraeDatosCorteAnterior,,生成Excel应用程序并添加工作表。但是,通过继续从函数** ConstruyeTablayCampo **生成文档,应用程序会出现错误424,并显示以下消息**“需要一个对象”**
** Excel对象声明**
Option Compare Database
Dim ba As Excel.Application
Initial Funcion
Public Sub TraeDatosCorteAnterior()
RstName = "SELECT conc.CP, conc.etapa, conc.total FROM t_conclusion as conc;"
Dim dbs As Database
Set dbs = CurrentDb
Dim iRec As Long
Dim i As Long
Dim RstOrig As Recordset
Dim nTotalReg As Integer
Dim nCP As Integer
Dim nEtapa As Integer
Dim nTotal As Integer
Set RstOrig = CurrentDb.OpenRecordset(RstName, dbOpenDynaset)
RstOrig.Sort = fldName
Dim RstSorted As Recordset
Set RstSorted = RstOrig.OpenRecordset()
RstOrig.Sort = fldName
Dim RstSorted As Recordset
Set RstSorted = RstOrig.OpenRecordset()
RstSorted.MoveLast
RstSorted.MoveFirst
nTotalReg = RstSorted.RecordCount
nCP = RstSorted("CP")
nEtapa = IIf(IsNull(RstSorted("etapa")) = True, 0, RstSorted("etapa"))
nTotal = IIf(IsNull(RstSorted("Total")) = True, 0, RstSorted("Total"))
RstSorted.MoveNext
Set ba = New Excel.Application
Set xlwbook = ba.Workbooks.Add
ba.Visible = True
ConstruyeTablayCampo "Cuadro I", "Cuenta Pública", "A", "A", ba, False, 1
End Sub
**构建文档的函数**,从第一行开始标记错误。
** Excel对象声明**
Sub ConstruyeTablayCampo(ByRef sHoja As String, ByRef sTexto As String, ByRef col1 As String, ByRef col2 As String, ByRef ba As Excel.Application, Optional bCreaHoja As Boolean, Optional nHoja As Integer)
Set xlsheet = xlwbook.Sheets.Item(nHoja)
xlwbook.Sheets.Item(nHoja).Name = sHoja
xlsheet.Cells.Range("A11:A11") = sTexto
Worksheets("Cuadro I").Range("B:B,D:D,H:H,J:J,N:N,Q:Q").ColumnWidth = 1
With xlsheet.Cells.Range(col1 & "11:" & col2 & "12")
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Select
.Font.Bold = True
.MergeCells = True
.Interior.Color = RGB(244, 244, 244)
End With
End Sub
提前感谢,希望你能帮助我!
发布于 2019-05-30 09:03:47
最后我解决了这个问题,解决方案是这样做:
首先声明此
Option Compare Database
Dim ba As Excel.Application
Dim xlwsheet As Excel.Worksheet
第二,初始化第二行中的对象:
Sub ConstruyeTablayCampo(ByRef sHoja As String, ByRef sTexto As String, ByRef col1 As String, ByRef sRango1 As String, ByRef col2 As String, ByRef sRango2 As String, ByRef ba As Excel.Application, ByRef color1 As String, ByRef color2 As String, ByRef color3 As String, ByRef nHoja As Integer)
Set xlwsheet = ba.Worksheets.Item(nHoja)
xlwsheet.Name = sHoja
xlwsheet.Range("B:B,D:D,H:H,J:J,N:N,Q:Q").ColumnWidth = 1
With xlwsheet.Cells.Range(col1 & "11" & ":" & col2 & "12")
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Select
.Font.Bold = True
.MergeCells = True
.Interior.Color = RGB(color1, color2, color3)
End With
xlwsheet.Range(col1 & "11:" & col2 & "12").Cells = sTexto
End Sub
谢谢你的帮助!
https://stackoverflow.com/questions/56367466
复制相似问题