4

VBA 调用 Lotus 发送邮件的收件人长度问题

 4 years ago
source link: https://zhiqiang.org/coding/lotus-vba-recepient-no-longer-than-256.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

VBA 调用 Lotus 发送邮件的收件人长度问题

作者: 张志强

, 发表于 2014-06-12

, 共 1741 字 , 共阅读 91 次

公司使用 Lotus ,我每天都通过 Excel 编写 VBA 代码自动调用 Lotus 发送邮件,发现一个奇怪的现象。一般我们通过下面的 VBA 代码指定邮件收件人:

但需要上面这个收件人列表不能超过 256 个字节,否则会被截断,从而丢失一部分收件人。

这个问题可以被解决。Lotus 的收件人列表不光可以是字符串,还可以是字符串数组。所以下面这么写也是可以的:

字符串数组里每一个字符串可以是多个接收人,但每个单独的字符串都不能超过 256 个字节,否则一样会被截断:

.SendTo = Array("[email protected]; [email protected]",  "[email protected]")  ' it's OK too

通常我们会把收件人保存在 Excel 的某个单元格中,不同收件人之间用分号「;」隔开。这时候我们可以这样调用:

.SendTo = Array(Split(Sheet1.range("A1").value(), ";"))

可一劳永逸解决字符串阶段问题。

VBA 中字符串被截断的问题不光发生在 Lotus 的接收人列表中,我很早就发现通过 VBA 修改 Excel 的数据连接字符串和 SQL 查询语句的时,字符串长度也不能超过 256 字节,一旦超过,需要用上面一样的办法解决,将字符串转成字符串数组:

' 更改数据表的来源
' wb:工作表对象
' connectionName:数据来源连接名称
' strSQL:新查询语句(修改SQL的查询代码)
' strSQLConnection:新连接语句(修改来源数据库,在对DBF数据库操作时非常有用)
' author: [email protected], 2010
Public Sub ChangeODBCConnection(wb As Excel.Workbook, connectionName As String, _
                                Optional strSQL As String = "", Optional strSQLConnection As String = "")

    With wb.Connections(connectionName).OLEDBConnection
        If Len(strSQLConnection) Then .Connection = SplitString(strSQLConnection) ' here
        If Len(strSQL) Then .CommandText = SplitString(strSQL) ' here
    End With
    wb.Connections(connectionName).Refresh
End Sub

' 将字符串分割成短字符串的数组
Function SplitString(ByVal s As String) As Variant
    Dim ss() As Variant
    Dim i As Long

    ReDim ss(0 To Len(s) \ 200)        ' note: it is not 256
    For i = 0 To UBound(ss)
        ss(i) = Array(mid(s, i * 200 + 1, 200))
    Next i

    SplitString = ss
End Function

Q. E. D.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK