【问题标题】:Prompt for Subject Line When Creating a New Email创建新电子邮件时提示主题行
【发布时间】:2017-04-02 20:53:14
【问题描述】:

我使用的是 2007 Outlook。

我正在尝试获取一个代码,在创建新电子邮件时提示用户选择一个固定的单选按钮选项,如下所示 [A]: , [R]:, [F:] , [!] : , 空白(使主题行空白的选项)。

我希望该选择自动插入到主题行中。

我在网上找到了代码,但在代码末尾出现错误。

Private Sub m_colInspectors_NewInspector(ByVal Inspector As Outlook.Inspector)

我将此代码粘贴到 ThisOutlookSession 模块中。

Option Explicit
Private WithEvents m_colInspectors As Outlook.Inspectors
Private WithEvents CurrentInspector As Outlook.Inspector

Private Sub Application_Startup()
    Set m_colInspectors = Application.Inspectors
End Sub

Private Sub CurrentInspector_Activate()
    Dim oMail As Outlook.MailItem
    If Len(UserForm1.SelectedSubject) Then
        Set oMail = CurrentInspector.CurrentItem
        oMail.Subject = UserForm1.SelectedSubject
    End If
    Set CurrentInspector = Nothing
End Sub

Private Sub m_colInspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
If TypeOf Inspector.CurrentItem Is Outlook.MailItem Then
    If Inspector.CurrentItem.EntryID = vbNullString Then
        UserForm1.SelectedSubject = vbNullString
        UserForm1.Show
        Set CurrentInspector = Inspector
    End If
End If
End Sub

我创建了一个带有单选按钮和一个命令按钮的表单,我在其中插入了以下代码。

Option Explicit
Public SelectedSubject As String

Private Sub CommandButton1_Click()
    If OptionButton1.Value = True Then
        SelectedSubject = "Test"
    End If
    Hide
End Sub

【问题讨论】:

  • 我已经为你添加了 Outlook-VBA 作为标签。您的问题中没有提到 Excel,所以我认为应该删除它。

标签: vba email outlook userform


【解决方案1】:

这可能会让你想要你想要的。把它放在ThisOutlookSession 下。当用户点击Sends 时,这会触发,这意味着他们在发送之前无法更改主题行。我正在使用UserForm1 和您为此使用的代码。添加任意数量的单选按钮,只需将OptionButton1 修改为2 和值即可。

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim strSubject As String
Dim Prompt$

    strSubject = Item.Subject
    ' Show RadioButtons
    UserForm1.Show
    ' Set Subject Line as the value from the selected RadioButton
    strSubject = UserForm1.SelectedSubject

    ' Set the message subject
    Item.Subject = strSubject
    strSubject = Item.Subject

    ' Test if Subject Line is empty
    If Len(Trim(strSubject)) = 0 Then
        Prompt$ = "Subject is Empty. Are you sure you want to send the Mail?"
        If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Subject") = vbNo Then
            Cancel = True
        End If
    End If
End Sub

【讨论】:

  • 感谢您的回复 Niclas。我按照指示删除了代码并提示我的表单,但在发送之前它没有将所选选项粘贴到主题中。主题行保持不变。我确定问题出在我的表单上,我仍在学习 VBA,也许我没有附加分配给选项按钮的值。你能帮我分析一下我的表格,看看我做错了什么吗?
  • 它对我有用,但我认为你是对的,与上面的代码相比,你的用户表单的命名问题是正确的。所以它显示了你的表格,你可以做选择吗?而你还在使用SelectedSubject
猜你喜欢
  • 1970-01-01
  • 2021-10-14
  • 1970-01-01
  • 1970-01-01
  • 2015-10-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-20
  • 1970-01-01
相关资源
最近更新 更多