如何将多个Outlook项目的数组合并成一个大数组

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

How to merge several arrays of Outlook items into one big array

问题

' 有没有办法将两个对象数组合并成一个?
' 我想将所有 objArray2 中的项目添加到 objArray1 的末尾,并创建一个新数组,其中包含来自两个数组的项目?
' 我尝试通过基本的数组连接来合并,就像合并字符串数组一样。

英文:

I do collections of Outlook items with VBA taking items from particular Outlook folders.

In the Excel VBA code below I collect items from two folders into two arrays.

Set olGetArchMeetings = olNS.Folders(2).Folders(4).Items
olGetArchMeetings.IncludeRecurrences = True
olGetArchMeetings.Sort "[Start]"
strRestrictionArch = "[Start] >= '" & mStart & "' AND [End] <= '" & mEnd & "'"
Set objArray1 = olGetArchMeetings.restrict(strRestrictionArch)
    
Set olGetMeetings = olNS.GetDefaultFolder(9).Items 
olGetMeetings.IncludeRecurrences = True
olGetMeetings.Sort "[Start]"
strRestriction = "[Start] >= '" & mStart & "' AND [End] <= '" & mEnd & "'"
Set objArray2 = olGetMeetings.restrict(strRestriction)

Is there any way to merge two arrays of objects into one?

I want to add all items from objArray2 to the end of objArray1 and make a new array that will contain items from both arrays?

I tried to merge via basic array joining like merging string arrays.

答案1

得分: 1

首先,Items 类的 Restrict 方法会对 Items 集合应用过滤器,返回一个包含与过滤器匹配项的原始项目的新集合,但不是一个数组。

答案是:没有简单的方法可以从不同的 Restrict 调用中获取单个 Items 集合。你可以考虑构建从找到的项目中提取数据的数组。但更好的方法是在 Outlook 中使用可以在后台运行的单个搜索。

Application.AdvancedSearch 方法允许根据指定的 DAV Searching and Locating (DASL) 搜索字符串在多个文件夹中执行搜索。要指定多个文件夹路径,请用单引号括起每个文件夹路径,并用逗号分隔单引号括起的文件夹路径。

在 Outlook 中使用 AdvancedSearch 方法的关键好处包括:

  • 搜索在另一个线程中执行。您无需手动运行另一个线程,因为 AdvancedSearch 方法会自动在后台运行。
  • 可以搜索任何类型的项目:邮件、约会、日历、备忘录等,位置也可以是任何地方,即超出特定文件夹的范围。RestrictFind/FindNext 方法可应用于特定的 Items 集合(请参阅 Outlook 中 Folder 类的 Items 属性)。
  • 对 DASL 查询的全面支持(还可以用于搜索自定义属性)。为了提高搜索性能,如果为存储启用了即时搜索(请参阅 Store 类的 IsInstantSearchEnabled 属性),可以使用即时搜索关键字。
  • 您可以使用 Search 类的 Stop 方法随时停止搜索过程。

在我为技术博客撰写的文章中了解更多:Outlook 中的程序化高级搜索:C#、VB.NET

英文:

First of all, the Restrict method of the Items class applies a filter to the Items collection, returning a new collection containing all of the items from the original that match the filter, but not an array.

> The questions is: Is there any way to merge two arrays of objects into one? Like add all items from objArray2 to the end of objArray1 and therefore make a new Array that will contain itmes from both arrays?

No, there is no trivial way of getting a single Items collection from different Restrict calls. You may consider building an array of data extracted from items found. But a better yeat approach is to use a single search which can be run in the background in Outlook.

The Application.AdvancedSearch method allows performing a search based on a specified DAV Searching and Locating (DASL) search string in multiple folders. To specify multiple folder paths, enclose each folder path in single quotes and separate the single quoted folder paths with a comma.

The key benefits of using the AdvancedSearch method in Outlook are:

  • The search is performed in another thread. You don’t need to run another thread manually since the AdvancedSearch method runs it automatically in the background.
  • Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The Restrict and Find/FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook).
  • Full support for DASL queries (custom properties can be used for searching too). To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).
  • You can stop the search process at any moment using the Stop method of the Search class.

Read more about that in the article that I wrote for the technical blog: Advanced search in Outlook programmatically: C#, VB.NET.

答案2

得分: 0

我不知道如何为Outlook编写代码,但基本的数组合并会像这样。这是针对一维数组的代码:

子过程 合并数组()

    Dim obA As Object, obB As Object, obC As Object, obD As Object
    Dim arrA As Variant, arrB As Variant, arrAll As Variant
    Dim m As Integer, n As Integer, first As Integer, last As Integer

    '设置对象
    Set obA = Cells(1)
    Set obB = Cells(2)
    Set obC = Cells(3)
    Set obD = Cells(4)

    '声明数组维度
    ReDim arrA(1 To 2)
    ReDim arrB(1 To 2)

    '填充两个数组
    Set arrA(1) = obA
    Set arrA(2) = obB

    Set arrB(1) = obC
    Set arrB(2) = obD

    first = UBound(arrA) + 1             ' = 3 
    last = UBound(arrA) + UBound(arrB)   ' = 4

    '扩展第一个数组以合并第二个数组
    ReDim Preserve arrA(1 To last)
     
    For m = first To last
       n = n + 1
       Set arrA(m) = arrB(n)
    Next m
    
End Sub
英文:

I have no clue on how to code for outlook, but basic array merging would be like this. That is code for one-dimensional arrays only:

Sub ArrayMerge()

Dim obA As Object, obB As Object, obC As Object, obD As Object
Dim arrA As Variant, arrB As Variant, arrAll As Variant
Dim m As Integer, n As Integer, first As Integer, last As Integer

'setting objects
Set obA = Cells(1)
Set obB = Cells(2)
Set obC = Cells(3)
Set obD = Cells(4)

'dimensioning arrays
ReDim arrA(1 To 2)
ReDim arrB(1 To 2)

'filling both arrays
Set arrA(1) = obA
Set arrA(2) = obB

Set arrB(1) = obC
Set arrB(2) = obD

first = UBound(arrA) + 1             ' = 3 
last = UBound(arrA) + UBound(arrB)   ' = 4

'Enlarge the first array to join the second one
ReDim Preserve arrA(1 To last)
 
   For m = first To last
       n = n + 1
       Set arrA(m) = arrB(n)
   Next m

End Sub

huangapple
  • 本文由 发表于 2023年2月16日 02:17:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75463923.html
匿名

发表评论

匿名网友

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

确定