簡體   English   中英

如何將 Excel 范圍內的字符串列表分配給 Outlook VBA 變量?

[英]How to assign a list of strings from an Excel range to an Outlook VBA variable?

如何將 excel 范圍內的字符串列表分配給 outlook vba 變量?

我在 Outlook 中運行以下內容。 我打算從 Excel 工作表中提取電子郵件地址(字符串)列表,以進行發件人比較。

Sub OutlookExcel()
    
    'declare variables
    Dim xExcelFile As String
    Dim xExcelApp As Excel.Application
    Dim xWb As Excel.Workbook
    Dim xWs As Excel.Worksheet
        
    'declare xls file
    xExcelFile = "[path]\test1.xlsx"
    'open the xls file
    Set xExcelApp = CreateObject("Excel.Application")
    Set xWb = xExcelApp.Workbooks.Open(xExcelFile)
    Set xWs = xWb.Sheets(1)
       
    ' get the email adresses. here i get into trouble
    Dim rng As Variant
    Set rng = xWs.Range("A2:A3")
        
    Debug.Print rng(1)
    Debug.Print rng(2)
    Debug.Print rng(3)
    Debug.Print rng(9)
    Debug.Print rng(10)
    Debug.Print rng(0)            
End Sub

返回

A2
A3
A4
A10

A1

在 test1.xlsx 中使用我的“電子郵件地址”,如下所示:
在此處輸入圖像描述

請注意,我將 rng 指定為兩個單元格“A2:A3”,但包含單元格 A1 到 A10。 rng(100) 返回空字符串。

我想要一個索引為 1 和 2 的列表對象,包含“A2”和“A3”。 在第二步中,我需要我的列表包含單元格 A2,直到列 A 的最后一個非空單元格。

編輯
以上是最小的例子。 上下文是我想檢查傳入的電子郵件項目的發件人地址並將它們移動到相應的文件夾。

關鍵問題是我獲得的電子郵件地址列表對於 for each 循環來說似乎是無限長的:

Function Findstring(email As String, rng2 As Variant) As Integer
    'returns -1 if email is not found in rng2, otherwise its index

    Findstring = -1
    Dim i As Integer
    i = 1
    For Each Item In rng2  'never stops, i ever increases => function doesnt return
        If email = Item Then
            Findstring = i
            Exit For
        End If
        i = i + 1
    Next
End Function

其中數組 rng2 是包含電子郵件的列表,如下所示:

Sub GetEMailsFolders(ByRef rng1 As Variant, ByRef rng2 As Variant, ByRef n As Variant)
    
    'declare variables
    Dim xExcelFile As String
    Dim xExcelApp As Excel.Application
    Dim xWb As Excel.Workbook
    Dim xWs As Excel.Worksheet
        
    'declare email & folder xls file
    xExcelFile = "C:\temp\temp.xlsx"
    'open the file
    Set xExcelApp = CreateObject("Excel.Application")
    Set xWb = xExcelApp.Workbooks.Open(xExcelFile)
    Set xWs = xWb.Sheets(1)
        
    'extract folders (column A), emails (column O) and thir number
    n = xWs.Range(xWs.Range("A2"), xWs.Range("A2").End(xlDown)).Count  'works
    Set rng1 = xWs.Range(xWs.Range("A2"), xWs.Range("A2").Offset(n - 1, 0)) 'folder names
    Set rng2 = xWs.Range(xWs.Range("O2"), xWs.Range("O2").Offset(n - 1, 0)) 'emails
End Sub

最后,我需要一個工作區變量,其中包含一個(有限)列表,我可以 a) 遍歷每個列表和 b) 像list(1)list(N)這樣的索引

[解決方案]

我的“技巧”是將變體 ReDim 到已知長度,並分配每個元素:

n = xWs.Range(xWs.Range("A2"), xWs.Range("A2").End(xlDown)).Count ' works
ReDim rng1(1 To n) As Variant
For i = 1 To n
    rng1(i) = xWs.Cells(i + 1, 1)
    Debug.Print rng1(i), rng2(i)
Next

現在一切順利。
我仍然對這種變異行為感到震驚,或者我還沒有理解它。

這是你想要做的嗎?

Option Explicit
Sub OutlookExcel()
    'declare variables
    Dim xExcelFile As String
    Dim xExcelApp As Excel.Application
    Dim xWb As Excel.Workbook
    Dim xWs As Excel.Worksheet

    'declare xls file
    xExcelFile = "C:\Temp\Temp.xlsm"
    'open the xls file
    Set xExcelApp = CreateObject("Excel.Application")
    Set xWb = xExcelApp.Workbooks.Open(xExcelFile)
    Set xWs = xWb.Sheets(1)

    ' get the email adresses. here i get into trouble
    Dim rng As Variant
    Set rng = xWs.Range("A2:A3")

    Dim Cell As Range
    For Each Cell In rng
        Debug.Print Cell.Value
    Next

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM