我通过使用多维数组找到了一个完美的解决方案。
代码为:
- Dim excelApp As Excel.Application
- Dim wbk As Workbook
- Dim sht As Worksheet
- Private Sub cmdListBlocks_Click()
-
- Dim Block As AcadBlock
- Dim i As Integer
- Dim MyBlockArray() As Variant
-
- i = 0
-
- For Each Block In ThisDrawing.Blocks
- i = i + 1
-
- ReDim Preserve MyBlockArray(2, i)
- MyBlockArray(0, i) = Block.Name
- MyBlockArray(1, i) = Block.Count
- Next Block
- Me.ListBoxBlocks.ColumnCount = 2
- Me.ListBoxBlocks.ColumnWidths = "36;36"
- Me.ListBoxBlocks.Column() = MyBlockArray
- End Sub
- Private Sub CommandButton1_Click()
- Dim i As Integer
- ListCount = Me.ListBoxBlocks.ListCount
- On Error Resume Next
- Set excelApp = GetObject(, "Excel.Application")
- If Err <> 0 Then
- Err.Clear
- Set excelApp = CreateObject("Excel.Application")
- If Err <> 0 Then
- MsgBox "Εrror Opening Excel!", vbExclamation
- End
- End If
- End If
- excelApp.Visible = True
- Set wkbkObj = excelApp.Workbooks.Open(FileName:="c:\dipola.xls") [b]' here i just open a certain excel Workbook[/b]
- Set sheetObj = wkbkObj.Worksheet(1) [b]' I put the values in the 2nd sheet[/b]
- For i = 0 To ListCount - 1
- If ListBoxBlocks.List(i, 0) = "C1" Then Range("B1").Cells.Value = ListBoxBlocks.List(i, 1)
- If ListBoxBlocks.List(i, 0) = "C2" Then Range("B2").Cells.Value = ListBoxBlocks.List(i, 1) [b]' Here I make a lot of checks so i just wrote two to show the way I send values to certain cells in excel[/b]
- Next
- wkbkObj.Sheets("DIPOLA").Select [b] ' I focus on the 1st sheet that is the actual report page for the user to print[/b]
- End Sub
- Private Sub CommandButton2_Click()
- End
- End Sub
我刚刚创建了一个带有3个按钮和一个列表框的用户表单(只是为了从中读取值并将其发送到excel)。
我不确定它是否会在2011年起作用,但在2009年和2007年起作用。 |