excel - Excel VBA:用作电子邮件地址时缺少前导零
问题描述
我正在尝试使用 Excel VBA 起草一封新电子邮件,并在抄送列表中添加电子邮件地址“00000@mycompany.com”,而“00000”是 5 位数的员工编号。用户应在表格范围“T5”、“V5”或“X5”中填写员工编号。
但是,如果他们的员工编号以零开头,则电子邮件地址中将缺少前导零。
例如,他们填写的号码是“01234”,但草稿电子邮件中的电子邮件地址将是“1234@mycompany.com”
我努力了
.text
.numberformat="@" and
.numberformat="00000"
但他们不工作。
谁能帮我解决这个问题?多谢!
Private Sub sendmail_Click()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Form")
Dim OA As Object
Dim msg As Object
Dim filepath As String
Set OA = CreateObject("Outlook.Application")
Dim folder
Set folder = CreateObject("scripting.filesystemobject")
Set msg = OA.CreateItem(0)
With msg
.display
End With
signature = msg.Body
Dim var_cc As String
var_cc = ""
With msg
.To = "abc@mycompany.com"
If Application.WorksheetFunction.IsNumber(sh.Range("T5")) = True Then
var_cc = var_cc & sh.Range("T5").NumberFormat = "@" & "@mycompany.com;"
Else
End If
If Application.WorksheetFunction.IsNumber(sh.Range("V5")) = True Then
var_cc = var_cc & sh.Range("V5").Value & "@mycompany.com;;"
Else
End If
If Application.WorksheetFunction.IsNumber(sh.Range("X5")) = True Then
var_cc = var_cc & sh.Range("X5").Value & "@mycompany.com;;"
Else
End If
.cc = var_cc
.Subject = sh.Range("G5").Value & " " & sh.Range("D5").Value & " " & sh.Range("A5").Value & " " & sh.Range("E3").Value & " / " &
sh.Range("E2").Value
.Body = "Dear All," & vbNewLine & _
"Please find attached missing info. Thank you." & _
vbNewLine & signature
.importance = 2
.Attachments.Add (Application.ActiveWorkbook.FullName)
End With
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
解决方案
您可以使用
var_cc = var_cc & Format(sh.Range("T5").Value, "00000") & "@mycompany.com;"
代替
var_cc = var_cc & sh.Range("T5").NumberFormat = "@" & "@mycompany.com;"
推荐阅读
- python - python中的类变量使用Pyglet
- azure-cognitive-search - 翻译技能不将字段写入输出
- python-3.x - pip3 在我的路径中,但是当我尝试运行它时它失败了
- c - 如何在代码中打印退出状态?
- python - 如何更新此 Bokeh ColumnDataSource?
- three.js - 如何让 Dashed THREE.MeshLine 工作?
- xml - 从大型 xml 文件中删除节点的平台无关的 powershell 方法是什么?
- macos - macOS 如何通过捆绑标识符定位应用程序?
- javascript - 使 D3 图上的轴线消失
- objective-c - 是否可以将 RealityKit 与 Objective-C 一起使用