Office中国论坛/Access中国论坛

标题: Access导出excel修改标题无法修改 [打印本页]

作者: 盗到稻    时间: 2007-11-28 17:31
标题: Access导出excel修改标题无法修改

老是说91错误,好像偶有几次也能通过,百思不解,恳请帮助
作者: 盗到稻    时间: 2007-11-28 19:56
原代码如下:
Dim i As Integer, lblName As String, j As Integer
Dim TP(15) As String

        Dim xlApp As Excel.Application '定义EXCEL类
    Dim xlBook As Excel.Workbook '定义工件簿类
    Dim xlSheet As Excel.Worksheet '定义工作表类
   

For i = 1 To 15
    lblName = "Label" & Format(i, "00")
    If Me.Child0.Form(lblName).Visible = True Then
        TP(i) = Me.Child0.Form(lblName).Caption
        j = i
    Else      
        Exit For
    End If
Next i
'导出到Excel
    Dim stDocName As String
    stDocName = ChrW(23548) & ChrW(20986) & ChrW(69) & ChrW(120) & ChrW(99) & ChrW(101) & ChrW(108)
    DoCmd.OutputTo acForm, "StarShow", "MicrosoftExcelBiff8(*.xls)", "", True, "", 0
'导出的数据标题不直观
'修改Excel中第一列的标题
    Set xlSheet = ActiveSheet
   
    xlSheet.Cells(1, 1) = "姓 名"        '这里就出错了
    xlSheet.Cells(1, 2) = "义工编号"
    xlSheet.Cells(1, 3) = "合 计"
   
    i = 1
    For i = 1 To j
   
        xlSheet.Cells(1, i + 3) = TP(i)
        
    Next i
   
xlBook.Save  '保存

Set xlSheet = Nothing
Set xlApp = Nothing
Set xlBook = Nothing

Erase TP  '回收数组

[ 本帖最后由 盗到稻 于 2007-11-28 19:57 编辑 ]
作者: tmtony    时间: 2007-11-29 09:44
中间代码修改如下:
    Dim xlApp As Excel.Application '定义EXCEL类
    Dim xlBook As Excel.Workbook '定义工件簿类
    Dim xlSheet As Excel.Worksheet '定义工作表类
   

'For i = 1 To 15
'    lblName = "Label" & Format(i, "00")
'    If Me.Child0.Form(lblName).Visible = True Then
'        TP(i) = Me.Child0.Form(lblName).Caption
'        j = i
'    Else
        
'        Exit For
'    End If
'Next i

    Dim stDocName As String

    stDocName = ChrW(23548) & ChrW(20986) & ChrW(69) & ChrW(120) & ChrW(99) & ChrW(101) & ChrW(108)
    DoCmd.OutputTo acForm, "StarShow", "MicrosoftExcelBiff8(*.xls)", "", True, "", 0
    'DoCmd.RunMacro stDocName
    'DoCmd.OutputTo acOutputForm, stDocName, acFormatXLS, , True
   
   
    'AppActivate "Microsoft Excel"
    Set xlApp = GetObject(, "Excel.Application")
    'Set xlBook = GetObject("StarShow.xls")
    'Debug.Print ActiveWindow.Caption
    Set xlBook = xlApp.ActiveWorkbook
    Set xlSheet = xlApp.ActiveSheet
    'xlSheet.Activate
    'xlApp.Visible = True
   
   
    xlSheet.Cells(1, 1) = "编 号"
    xlSheet.Cells(1, 2) = "姓 名"
    xlSheet.Cells(1, 3) = "手 机"
    xlSheet.Cells(1, 4) = "电 话"
就可以了,因为你对未获得Excel对象,所以出错
作者: tmtony    时间: 2007-11-29 09:45
另外,输入文件可能还未完成,所以在 Set xlApp = GetObject(, "Excel.Application")
之前要 用程序 sleep 几秒钟
作者: 盗到稻    时间: 2007-11-29 13:19
感谢王站,
作者: 盗到稻    时间: 2007-11-30 16:17
如果Excel打开了,就不会出错,如果Excel已经打开了会出现429的错误
所以加入了一个判断Excel是否打开的代码:

Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As Long) As Long

    '判断Excel是已经打开
    Dim hWnd As Long
        hWnd = FindWindow("XLMAIN", 0)
        If hWnd = 0 Then
               Set xlApp = New Excel.Application
         End If

另外王站你说的Sleep函数是不是延时功能的?是不是类似下面的?
Private Declare Function timeGetTime Lib "winmm.dll" () As Long   '
Public Function atSleep(WaitDate As Integer)
Dim Savetime As Double
Savetime = timeGetTime '记下开始时的时间

While timeGetTime < Savetime + WaitDate
    DoEvents
Wend

End Function

VBA中没有找到Sleep函数,不过好像用不上。
作者: kokolc    时间: 2008-3-10 13:13
学习学习学习学习!!
作者: yori2007    时间: 2008-3-24 08:22
学习学习啊 !!!!!!!!!!!!!
作者: phoenix_xh    时间: 2009-3-16 08:57
原代码是不是有问题
作者: phoenix_xh    时间: 2009-3-16 11:36
楼主能不能把修改过的代码再共享一下,特别是判断excel是不是打开的那一段代码,我放入程序后出错:
"编译错误
在end sub、end function 或end 属性后面只能出现注释
"
我想用这个代码,请楼主帮下忙.
作者: sunwrsun    时间: 2009-7-25 19:57

作者: CIB    时间: 2009-8-5 11:09
我一步一步来,要好好学习!
作者: leijiqiang    时间: 2009-8-20 11:19
顶下
作者: aq217    时间: 2009-8-25 16:18
学习学习啊 !!!!!!!!!!!!!
作者: nj_sm    时间: 2009-8-27 00:49
我要学学!
作者: ciyun20    时间: 2009-9-11 13:05
学习
作者: LeeTien    时间: 2010-5-12 18:04
看看
作者: hgxly    时间: 2010-7-1 11:56
看看
作者: 离开机会个    时间: 2010-7-13 06:49
看看
作者: caep4083    时间: 2011-2-12 00:09
学习
作者: stzd    时间: 2011-9-30 22:37
感谢
作者: chenyingfengsx    时间: 2011-9-30 22:42
收藏一下了!
作者: 关向齐    时间: 2012-6-29 00:32
看看
作者: 关向齐    时间: 2012-6-29 00:32
看看




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3