2.
Private Sub Command1_Click()
On Error Resume Next
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim i, j As Integer
Dim iXlRow As Integer
Set xlApp = GetObject(, "Excel.Application")
If Err.Number = 429 Then
'***************************************************************
' Excel is NOT running, so create a new instance
'***************************************************************
On Error Resume Next
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
End If
If Err.Number <> 429 And Err.Number <> 0 Then
MsgBox "打开 Excel 时发生错误!请检查是否正确安装了 Excel 2000 或更高版本!", vbExclamation, App.Title
End If
Err.Clear
Set xlSheet = xlApp.Worksheets.Add
If Err.Number Then
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlApp.Worksheets.Add
End If
On Error GoTo ErrH
'*******************************************************************
' Make sure Excel is visible
'*******************************************************************
With xlSheet.PageSetup
.TopMargin = 20
.LeftMargin = 20
.BottomMargin = 75
.RightMargin = 20
.Orientation = xlLandscape
.CenterHorizontally = True
.RightFooter = "第&P页,共&N页"
End With
xlSheet.PrintPreview
Set xlSheet = Nothing
Set xlApp = Nothing
Exit Sub
ErrH:
MsgBox "打开 Excel 时发生错误!请检查是否正确安装了 Excel 2000 或更高版本!如Excel 正在运行,请先关闭!" & Err.Description, vbExclamation, App.Title
End Sub
Set xlapp = Nothing
Set x_Cls = New ClsPrint
x_Cls.OpenFile = App.Path + "\report\print.xls"
x_Cls.OpenSheet = "account"
If x_Cls.OpenXlsObj Then
'Set xlsheet = New Excel.Worksheet
Set xlsheet = x_Cls.GetSheet
xlsheet.Columns("a:h").AutoFit
x_Cls.SetVisible = True
xlsheet.PrintPreview
Set xlsheet = Nothing
Set x_Cls = Nothing