[英]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.