ForEach循环与在CLI中打印的循环不同吗?

时间:2019-05-04 22:38:00

标签: powershell outlook scripting

当我输入'

$Listboxcal= New-Object System.Windows.Forms.ListBox 

add-type -assembly “Microsoft.Office.Interop.Outlook” | out-null
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace(“MAPI”)
# https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook.oldefaultfolders?view=outlook-pia
# Calendar = olFolderCalendar = 9
$calendar = $namespace.GetDefaultFolder(9)

# get today's calendar items!
 $cItems = $calendar.Items | Where-Object {$_.Start -ge $(Get-Date) -and $_.Start -le $(Get-Date).AddDays(1)} | select Start, Subject. 

'CLI可以完美打印日期/时间/主题标题

但是当我添加'

$Listboxcal= New-Object System.Windows.Forms.ListBox 
$Listboxcal.Location = New-Object System.Drawing.Size(10,10) 
$Listboxcal.Size = New-Object System.Drawing.Size(230,270) 
$Listboxcal.Height = 150
[…]
ForEach ($calendars in $cItems) {[void]$Listboxcal.Items.Add($calendars)} '

我只得到“ @(Start = 05/05/2019 11:30:00; Subject =}”打印。理想情况下,我希望将时间开始并将Subject打印到列表框中。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

永远不要显式过滤代码中的项目-这是非常低效的。始终使用Items.Find / FindNext或Items.Restrict,尤其是如果您想扩展重复项。

请参见https://docs.microsoft.com/en-us/office/vba/api/outlook.items.includerecurrences

上的示例