I'm having troubles passing an Array from user-defined Type to a Function:
Defined Type (in a standard Module):
Public Type JPrinters
Available() As String
Default As String
End Type
Function to make a list of all available printers (std Module):
Function ListPrinters() As JPrinters
Dim GetPrinters As Object: Set GetPrinters = CreateObject("WScript.Network").EnumPrinterConnections
Dim i As Integer
Dim j() As String 'Array of Printer Names
ReDim j(0 To GetPrinters.Count \ 2 - 1)
For i = 0 To UBound(j)
'Load this array element with the Name from the list
j(i) = GetPrinters.Item(i * 2 + 1)
Next i
Dim k As JPrinters
With k
.Available = j
.Default = Application.ActivePrinter
End With
ListPrinters = k
End Function
Sub to populate a ComboBox by an Array (which works fine, normally – std Module):
Sub PopulateComboBoxBy2DArray(Ctrl As Control, ByVal Source_1D_2D_Array As String, Optional DefaultValue As String)
Ctrl.List = Source_1D_2D_Array
If DefaultValue Then
Ctrl.Value = DefaultValue
End If
End Sub
Then I try to call everything on Workbook_Open:
Private Sub Workbook_Open()
Dim Prt As JPrinters
Prt = ListPrinters
PopulateComboBoxBy2DArray List1.PrinterList, Prt.Available, Prt.Default
End Sub
Option Explicit
defined. And it just gives my Compile error: Type mismatch . I tried a lot but cannot figure out why it won't accept the Prt.Available
Array. Any ideas?
(I will also be glad for any other suggestions regarding the code.)
Thank you,
J
Resolved (thanks to Dave from Mr. Excel forums). It goes like this:
Type:
Type J_Type_Printers
Available() As Variant
Default As Variant
End Type
Function:
Function J_Fx_ListPrinters() As J_Type_Printers
Dim GetPrinters As Object
Dim i As Integer
Dim j() As Variant 'Array of Printer Names
Dim k As J_Type_Printers
Set GetPrinters = CreateObject("WScript.Network").EnumPrinterConnections
ReDim j(0 To GetPrinters.Count \ 2 - 1)
For i = 0 To UBound(j)
'Load this array element with the Name from the list
j(i) = GetPrinters.Item(i * 2 + 1)
Next i
With k
.Available = j
.Default = Application.ActivePrinter
End With
J_Fx_ListPrinters = k
End Function
Sub to populate combobox by array contents:
Sub J_Sub_Controls_ComboBox_ListArray(ByVal Ctrl As ComboBox, ByVal SrcArray As Variant, Optional DefaultValue As Variant)
Ctrl.List = SrcArray
If Not IsMissing(DefaultValue) Then Ctrl.Value = DefaultValue
End Sub
Call on Workbook_Open:
Private Sub Workbook_Open()
Dim Prt As J_Type_Printers: Prt = J_Fx_ListPrinters
J_Sub_Controls_ComboBox_ListArray List1.PrinterList, Prt.Available, Prt.Default
End Sub
I believe the problem is the declaration of the Source_1D_2D_Array parameter. It is declared as string and it needs to be a string array. The type mismatch is because you are passing an string array to a string parameter.
Also the check for the DefaultValue needs to be a comparison. I think you want:
Sub PopulateComboBoxBy2DArray(Ctrl As Control, ByRef Source_1D_2D_Array() As String, Optional DefaultValue As String = "")
Ctrl.List = Source_1D_2D_Array
If DefaultValue<>"" Then
Ctrl.Value = DefaultValue
End If
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.