操作excel,第一次运行可以,第二次运行为什么报错?
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(txtFile.Text)
Set xlSheet = xlBook.Sheets(1)
xlSheet.Range("A1:J1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
xlApp.Visible = True
Set xlApp = Nothing
Set xlBook = Nothing
Set xlSheet = Nothing
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(txtFile.Text)
Set xlSheet = xlBook.Sheets(1)
xlSheet.Range("A1:J1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
运行到第二个 With Selection
.HorizontalAlignment = xlCenter
时报错:实时错误“91”,对象变量或with块变量未设置
其中 第二个Selection=nothing