概要
CDO (Microsoft Collaboration Data Objects) を使用して添付ファイル付きメールを送信しているが,添付ファイルの名称だけ変更したい
方法
1.目的のファイル名をエンコードするための関数
UTF-8 , Base64 エンコードするコードは以下の通りです
Public Function EncodeBase64(ByRef text As String) As String
Dim node As Object, obj As Object
Set node = CreateObject("Msxml2.DOMDocument.6.0").createElement("base64")
Set obj = CreateObject("ADODB.Stream")
node.DataType = "bin.base64"
With obj
.Type = 2
.Charset = "utf-8"
.Open
.WriteText text
.Position = 0
.Type = 1
.Position = 0
End With
node.nodeTypedValue = obj.Read
EncodeBase64 = Replace(node.text, vbLf, "")
If Left(EncodeBase64, 4) = "77u/" Then
EncodeBase64 = Mid(EncodeBase64, 5)
End If
End Function
2.メール添付部分のソースを調整します
.AddAttachment sendFiles(i)
.Attachments(.Attachments.COUNT).Charset = "utf-8" 'iso-2022-jp(標準)だとThunderbirdでファイル名の一部が文字化けする
'---------------
ここからが添付ファイル名を変更する部分
Dim sendFilevirtualName As String
sendFilevirtualName = EncodeBase64(sendFilesName(i))
.Attachments(.Attachments.COUNT).Fields("urn:schemas:mailheader:content-disposition").Value = "attachment; filename=""=?UTF-8?B?" & sendFilevirtualName & "?="""
.Attachments(.Attachments.COUNT).Fields.Updateプロパティ
Access 2010
Access 2019
Windows 10

コメント