英文:
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。然而,当我尝试将元素保存到任务列表中的数组时,只保存了数组中的最后一个元素。
在断点下一次执行循环时,我获得了这些值:
为什么不将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:
At the next excution cycle I have these values:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论