至今为止好象都是打开一个现存的excel文件。谁能告诉我怎么新建一个呢?

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/2972/2972741.xml?temp=.5063135
    Private Sub Form_Load()
    Dim excelapp
    Set excelapp = CreateObject("Excel.Application")
    excelapp.Visible = True
    excelapp.Workbooks.Add
    End Sub
      

  2.   

    '****************************Create the excle file******************************
    Public Function cFunXlCreateExcle(ByVal cFunTheSheetName As String) As BooleanOn Error Resume Next
        Set cXlapp = GetObject(cOutFilePathString, _
            "Excel.Application")                         'Get excel application object
        If Err.Number <> 0 Then
          Set cXlapp = CreateObject("Excel.Application") 'Create excel application object
          Err.Clear
        End If
    On Error GoTo Err                                    'Check whether Happen error
            Set cXlWorkBook = cXlapp.Workbooks().Add     'Create excel file
            Set cXlSheets = cXlapp.Worksheets            'Set cXlSheets with excel workbooks
            Set cXlSheet = cXlWorkBook.ActiveSheet       'Set cXlSheet with excel workbook ActiveSheet
            cXlSheet.Name = cFunTheSheetName
            cXlapp.DisplayAlerts = False                 'Not clue on user,
            cXlSheets("Sheet2").Delete                   'delete worksheet
            cXlSheets("Sheet3").Delete
            cXlapp.DisplayAlerts = True
            cXlSheet.Activate                            'Activate cXlSheet
            cXlapp.Visible = True                        'cXlSheet whether visible
            mCreateExcle = True                          'Ok return true
            Exit Function
    Err:
            MsgBox Err.Number & " " & Err.Description    'Print error bescription
            mCreateExcle = False                         'Fail return false
            
    End Function
      

  3.   

    Private cXlapp As Excel.Application                  'Declare excel application
    Private cXlWorkBook As Excel.Workbook                'Declare excel workbook
    Private cXlSheets As Object                          'Declare excel worksheets
    Private cXlSheet As Excel.Worksheet                  'Declare excel worksheet
    Private cOutFilePathString As String                 'Declare Path string
      

  4.   

    谢谢。
    To  andfor710061(清风笑) 
    我觉得在
    On Errer Goto ...
    在错误处理方法的后面最好加一行:
    On Error Goto 0
    来取消错误陷阱
    我以前有个vb的多线程程序。没有在处理错误以后加一行,最后造成了别的一段代码错误然后转到那个错误处理方法了。
    希望大家可以就这个主题讨论。