You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过VBA添加InputBox获取收件人邮箱并填入Outlook收件人栏

实现Outlook模板邮件自动填充收件人邮箱

当然可以实现这个需求啦!咱们只需要在现有代码基础上添几行,就能轻松让InputBox收集收件人邮箱,然后自动填入邮件的「收件人」栏。我帮你修改好了完整代码,还加了注释说明关键部分:

Sub TerminationsEmail()
    Dim txtUser As String
    Dim txtRecipientEmail As String
    Dim myOlApp As Object
    Dim itm As Object
    
    ' 获取离职员工姓名(保留你原来的逻辑)
    txtUser = InputBox("Terminated Employee's Name", "Name of Recipient")
    ' 新增:获取收件人邮箱地址的InputBox
    txtRecipientEmail = InputBox("Recipient's Email Address", "Email of Recipient")
    
    ' 初始化Outlook应用并加载模板(记得补全模板的完整路径哦)
    Set myOlApp = CreateObject("Outlook.Application")
    Set itm = myOlApp.CreateItemFromTemplate("C:\Users\fm81996\AppData\Roaming\Microsoft\Templates\YourTemplate.oft")
    
    ' 替换模板里的%_%为员工姓名(同时处理纯文本和HTML格式的模板)
    itm.Body = Replace(itm.Body, "%_%", txtUser)
    itm.HTMLBody = Replace(itm.HTMLBody, "%_%", txtUser)
    
    ' 核心操作:把收集到的邮箱赋值给邮件的收件人栏
    itm.To = txtRecipientEmail
    
    ' 弹出邮件窗口供你检查或修改
    itm.Display
    
    ' 释放对象,避免内存占用
    Set itm = Nothing
    Set myOlApp = Nothing
End Sub

几个小提醒:

  • 一定要把模板路径补全,确保后缀是.oft(Outlook模板的标准格式)
  • 如果你的模板是纯文本格式,HTMLBody那行可以删掉;如果是HTML格式,这行必须保留,不然模板里的占位符可能替换不完整
  • 可以给InputBox加个默认提示,比如InputBox("Recipient's Email Address", "Email of Recipient", "john.doe@company.com"),用户体验会更好

内容的提问来源于stack exchange,提问作者Fabricio Martinez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:32:15