mphill
09-09-2008, 03:55 PM
I added the Option Explicit and I am now unable to get the names from an Excel sheet (column A, row 2-82) to my Word combobox. It throws an Error 13 type mismatch. I have tried changing the ListItems but no success.
Thanks in advance for any way to correct the situation.
Private Sub UserForm_Initialize()
Dim objExlApp As Excel.Application
Dim objExlWB As Excel.Workbook
Dim exlSheetAddresses As Worksheet
Dim exlRange As Range
Dim exlWasNotRunning As Boolean
Dim WbToWorkOn As String
Dim ListItems As Variant
Dim i As Integer
Dim projNumber As String
WbToWorkOn = "C:\Temp\GeologyNames.xlsx"
' WbToWorkOn = "g:\Geology\Shared\Word 2007\geologynames.xlsx"
On Error Resume Next
Set objExlApp = GetObject(, "Excel.Application")
If Err Then
exlWasNotRunning = True
Set objExlApp = New Excel.Application
End If
On Error GoTo Err_Handler
'objExlApp.Visible = False 'Not visible by default
Set objExlWB = objExlApp.Workbooks.Open(FileName:=WbToWorkOn)
Set exlSheetAddresses = objExlApp.ActiveWorkbook.Worksheets("Addresses")
'Populate the TO combobox, as more names are added to the
'spreadsheet the range number will need to be increased.
'Example from A2:A82 to A2:A90. Do this for the cbTo, cbFrom
'and the lbCC area.
With Me.cbTO
.Clear
Application.ScreenUpdating = False
cbTO.List = exlSheetAddresses.Range("A2:A82").Value
Application.ScreenUpdating = True
ListItems = objExlApp.WorksheetFunction.Transpose(ListItems)
'**'Throws error here
For i = 1 To UBound(ListItems)
.AddItem ListItems(i)
Next i
.ListIndex = -1
End With
If exlWasNotRunning Then
objExlApp.Quit
End If
Set objExlApp = Nothing
Set objExlWB = Nothing
Set exlSheetAddresses = Nothing
Set exlRange = Nothing
Exit Sub
Err_Handler:
MsgBox WbToWorkOn & " caused a problem." & Err.Description, vbCritical, _
"Error:" & Err.Number
If exlWasNotRunning Then
objExlApp.Quit
End If
End Sub
Thanks in advance for any way to correct the situation.
Private Sub UserForm_Initialize()
Dim objExlApp As Excel.Application
Dim objExlWB As Excel.Workbook
Dim exlSheetAddresses As Worksheet
Dim exlRange As Range
Dim exlWasNotRunning As Boolean
Dim WbToWorkOn As String
Dim ListItems As Variant
Dim i As Integer
Dim projNumber As String
WbToWorkOn = "C:\Temp\GeologyNames.xlsx"
' WbToWorkOn = "g:\Geology\Shared\Word 2007\geologynames.xlsx"
On Error Resume Next
Set objExlApp = GetObject(, "Excel.Application")
If Err Then
exlWasNotRunning = True
Set objExlApp = New Excel.Application
End If
On Error GoTo Err_Handler
'objExlApp.Visible = False 'Not visible by default
Set objExlWB = objExlApp.Workbooks.Open(FileName:=WbToWorkOn)
Set exlSheetAddresses = objExlApp.ActiveWorkbook.Worksheets("Addresses")
'Populate the TO combobox, as more names are added to the
'spreadsheet the range number will need to be increased.
'Example from A2:A82 to A2:A90. Do this for the cbTo, cbFrom
'and the lbCC area.
With Me.cbTO
.Clear
Application.ScreenUpdating = False
cbTO.List = exlSheetAddresses.Range("A2:A82").Value
Application.ScreenUpdating = True
ListItems = objExlApp.WorksheetFunction.Transpose(ListItems)
'**'Throws error here
For i = 1 To UBound(ListItems)
.AddItem ListItems(i)
Next i
.ListIndex = -1
End With
If exlWasNotRunning Then
objExlApp.Quit
End If
Set objExlApp = Nothing
Set objExlWB = Nothing
Set exlSheetAddresses = Nothing
Set exlRange = Nothing
Exit Sub
Err_Handler:
MsgBox WbToWorkOn & " caused a problem." & Err.Description, vbCritical, _
"Error:" & Err.Number
If exlWasNotRunning Then
objExlApp.Quit
End If
End Sub