[英]User-defined Type & passing Array to Function
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):列出所有可用打印机的功能(std 模块):
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 用一个数组来填充一个 ComboBox(它工作正常,通常 - std 模块):
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:然后我尝试调用 Workbook_Open 上的所有内容:
Private Sub Workbook_Open()
Dim Prt As JPrinters
Prt = ListPrinters
PopulateComboBoxBy2DArray List1.PrinterList, Prt.Available, Prt.Default
End Sub
Option Explicit
defined.Option Explicit
。 And it just gives my Compile error: Type mismatch .它只是给出了我的Compile 错误: Type mismatch 。 I tried a lot but cannot figure out why it won't accept the
Prt.Available
Array.我尝试了很多,但无法弄清楚为什么它不接受
Prt.Available
数组。 Any ideas?有任何想法吗?
(I will also be glad for any other suggestions regarding the code.) (对于有关代码的任何其他建议,我也很高兴。)
Thank you,谢谢,
J J
Resolved (thanks to Dave from Mr. Excel forums).已解决(感谢 Excel 先生论坛的 Dave)。 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 按数组内容填充组合框:
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:调用 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.我相信问题在于 Source_1D_2D_Array 参数的声明。 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.此外,对 DefaultValue 的检查也需要进行比较。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.