Array VBA未被填充为分配的第一个值

huangapple go评论79阅读模式
英文:

Array VBA not getting populated with the first value assigned

问题

尝试创建一个宏以将我的待办事项列表复制到Outlook的任务列表文件夹中。我希望在运行宏时对任务列表进行某种验证,以防止重复复制任务。所以我的计划是比较待办事项和任务的主题数组。

Set TaskFolder = Session.GetDefaultFolder(olFolderTasks)
i = 0 
For Each CurrentItem In TaskFolder.Items
   If TypeOf CurrentItem Is Outlook.TaskItem Then
        Dim otMail As Outlook.TaskItem: Set otMail = CurrentItem
        arrTaskSubject(i) = otMail.Subject
   End If
   i = i + 1
   ReDim Preserve arrTaskSubject(i)
Next CurrentItem

我在我的任务Outlook文件夹中只有2个任务,主题分别是TASK1和TASK2。然而,当我尝试将元素保存到任务列表中的数组时,只保存了数组中的最后一个元素。

在断点下一次执行循环时,我获得了这些值:

Array VBA未被填充为分配的第一个值

在下一次执行周期中,我有这些值:
Array VBA未被填充为分配的第一个值

为什么不将TASK1和TASK2都保存到arrTaskSubject数组中?

英文:

Trying to build a macro to copy my To-Do List to Task list folder in Outlook. I want to have some sort of validation against the Task list, to not duplicate the Tasks when I run the macro. So my
plan is to compare the subject arrays of To-Do and Tasks.

Set TaskFolder = Session.GetDefaultFolder(olFolderTasks)
i = 0 
For Each CurrentItem In TaskFolder.Items
   If TypeOf CurrentItem Is Outlook.TaskItem Then
        Dim otMail As Outlook.TaskItem: Set otMail = CurrentItem
        arrTaskSubject(i) = otMail.Subject
   End If
   i = i + 1
   ReDim Preserve arrTaskSubject(i)
Next CurrentItem

I have only 2 task in my Tasks outlook foolder with the subject TASK1 and TASK2. However when I try to save the elements to an array from task list, it gets saved only the last element in the array.

Breakpointing the Next CurrentItem I get these values:

Array VBA未被填充为分配的第一个值

At the next excution cycle I have these values:
Array VBA未被填充为分配的第一个值

Why is not saving both TASK1 and TASK2 to arrTaskSubject array?

答案1

得分: 2

在开始时,数组没有大小,这就是为什么 Task1 没有分配给它的原因。

当它移动到 Task2 时,将其分配给 i=1,然后重新调整大小为 2。

您可以按以下方式更改代码:

Set TaskFolder = Session.GetDefaultFolder(olFolderTasks)
i = 0 
For Each CurrentItem In TaskFolder.Items
   If TypeOf CurrentItem Is Outlook.TaskItem Then
        ReDim Preserve arrTaskSubject(i)
        Dim otMail As Outlook.TaskItem: Set otMail = CurrentItem
        arrTaskSubject(i) = otMail.Subject
        i = i + 1
   End If
Next CurrentItem

这将在数组中填充 Task1(索引 0)和 Task2(索引 1)。

如果您希望在数组中保留空项(如果有的话),则应将 i 的增量移出 if 语句。

英文:

in the beginning the array doesn't have a size, so that's why the Task1 is not assigned to it

when it moves to Task2 it assigns it to i=1, and then redims to size 2

you can change the code in the following way:

Set TaskFolder = Session.GetDefaultFolder(olFolderTasks)
i = 0 
For Each CurrentItem In TaskFolder.Items
   If TypeOf CurrentItem Is Outlook.TaskItem Then
        ReDim Preserve arrTaskSubject(i)
        Dim otMail As Outlook.TaskItem: Set otMail = CurrentItem
        arrTaskSubject(i) = otMail.Subject
        i = i + 1
   End If
Next CurrentItem

this will populate the array with Task1 at index 0 and Task 2 at index 1

if you want to keep empty items in the array if there are any then you should move the i increment out of the if

huangapple
  • 本文由 发表于 2020年1月3日 19:13:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定