这概述了我想要做的事情。
这对我不起作用,目前还不清楚为什么。
提前感谢您提供任何帮助。
Sub mySub()
dim myDict as Dictionary
myDict=new Dictionary
myDict=myFunc()
End Sub
Function myFunc()
dim myDict2
set myDict2 = new Dictionary
'some code that does things and adds to myDict2'
myFunc=myDict2
End Function
答案 0 :(得分:28)
您需要在分配对象而不是值的任何时候使用SET关键字:
Sub mySub()
dim myDict as Dictionary
set myDict = myFunc()
End Sub
Function myFunc() as Dictionary
dim myDict2 as Dictionary
set myDict2 = new Dictionary
'some code that does things and adds to myDict2'
set myFunc=myDict2
End Function
您的原始代码也将myDict创建为新的Dictionary对象,然后立即将其替换为另一个。你可以跳过这一步。
答案 1 :(得分:0)
我看到这是一个老问题,但是AND帖子解决方案帮助我弄清楚了这一点,我将其带入了一个新的高度。这是一个小飞跃。谢谢!
如何转换用进程名称和ID填充字典的函数,以便它返回字典对象?然后,这是一个简单的任务,即在工作表中填充字典内容,这是我从博客中学到的。我希望我有作者的名字,但包含链接。
当然是假设Sheet1。根据需要定制。同样,这与你们两个人发布的内容相比都是很小的飞跃。绝对出色的工作人员,谢谢!
Sub Test_AllRunningApps()
Dim apps As Dictionary
Set apps = AllRunningApps()
'Populate a sheet with a dictionary - http://exceldevelopmentplatform.blogspot.com/2018/05/vba-writing-dictionaries-to-worksheet.html
Sheet1.Cells(1, 1).Resize(apps.Count, 1).Value2 = Application.Transpose(apps.Keys)
Sheet1.Cells(1, 2).Resize(apps.Count, 1).Value2 = Application.Transpose(apps.Items)
Set apps = Nothing
End Sub
'Similar to: http://msdn.microsoft.com/en-us/library/aa393618%28VS.85%29.aspx
Public Function AllRunningApps() As Dictionary
Dim strComputer As String
Dim objServices As Object, objProcessSet As Object, Process As Object
Dim oDic As Object, oDic2 As Object, a() As Variant
Set oDic = CreateObject("Scripting.Dictionary")
strComputer = "."
Set objServices = GetObject("winmgmts:\\" _
& strComputer & "\root\CIMV2")
Set objProcessSet = objServices.ExecQuery _
("Select Name, ProcessID FROM Win32_Process", , 48)
For Each Process In objProcessSet
If Not oDic.exists(Process.Name) Then
oDic.Add Key:=Process.Properties_("Name").Value, Item:=Process.Properties_("ProcessID").Value
End If
Next
Set AllRunningApps = oDic
Set objProcessSet = Nothing
Set oDic = Nothing
End Function