vb6 连续创建excel并操作,但只能作用在第一个book,求解
起因:
使用vb6 创建excel并操作导入图片,为方便用户操作,这个EXCEL是不保存不退出的,由用户自行选择需要的操作。
问题:
1:首次创建的excel如果不关闭,
Dim xlapp As Object 'Excel.Application
Set xlapp = CreateObject("Excel.Application")
Dim XlBook As Object 'Excel.Workbook
Set XlBook = xlapp.Workbooks.Add
再次由VB6创建的EXCEL能生成,但对文件的操作只体现在第一个生成的book1那里,我需要建那个就体现在那个,如图,BOOK2应该有图片而不能写到BOOK1里!
2:如果关闭第一个EXCEL则报下列错误
具体位置如下:
要求:
求解!谢谢!
[解决办法]
Private Sub Command1_Click()
' Start Excel
Dim xlapp As Object 'Excel.Application
Set xlapp = CreateObject("Excel.Application")
Dim XlBook As Object 'Excel.Workbook
Set XlBook = xlapp.Workbooks.Add
' EXCEL是否可见
xlapp.Visible = True
'xlapp.Visible = False
With xlapp
.Cells.Select
.Selection.RowHeight = 112
.Selection.ColumnWidth = 16
.Rows("1:1").Select
' With Selection '你原来的,这表示当前工作簿的选区,和新建的工作簿没关系,而这时可能没有工作簿被激活,引用要出错
With .Selection '加了个点在前面,表示xlapp的选区
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
End With
.Selection.RowHeight = 45
' With ActiveWindow 同前面
With .ActiveWindow
.SplitColumn = 9
.SplitRow = 1
End With
.ActiveWindow.SplitColumn = 2
.ActiveWindow.FreezePanes = True
.Range("A1") = "位号"
.Range("B1") = "图片"
.Range("C1") = "正常"
End With
End Sub