首页 > 解决方案 > Select Signature before sending email / Outlook VBA

问题描述

I need a help with code in vba. I need a promptbox where can i select one of three signatures before sending e-mail. Of course i know i can choose it manually while writing e-mail, but our team works with custormers personal datas and it has to be kind of reminder to choose a good one. I created a simple script which display prompt with message "Did u pick a correct signature", but its not enough. Never used VBA in outlook before so it's even harder.

标签: vbaoutlook

解决方案


我决定使用用户表单。我的代码工作了一段时间,但我改变了一些东西,现在它不起作用,我被卡住了。

此 Outlook 会话:

Public WithEvents myOlInspectors As Outlook.Inspectors

Sub myOlInspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
    
    Dim stopka As String
    Dim msg As Outlook.MailItem
    If Inspector.CurrentItem.Class = olMail Then
        Set msg = Inspector.CurrentItem
        If msg.Size = 0 Then
           UserForm2.Show
                 
    Select Case lstNum
    Case -1
         stopka = ""
    Case 0
         stopka = "Subject1"
    Case 1
         stopka = "Subject2"
    Case 2
         stopka = "Subject3"
    End Select
          
End If
End If

Subject1 = "<b><font face=""Artifakt Element"" size=""3"" color=""#003366"">blah blah</font></b><br>" _
    & " <b> <font size=""2"" color=""black""> Blah </b> </font> <br>" _
    & " <font size=""2""> Phone: +99 999 999 999 / Mobile: +99 999 999 999 / Mail : <a href=""mailto:blahblah@blah.com""> blah blah </a> </font> <br>" _
    & " <font size=""2""> <b> BLAH-Blah blah blah </b> Warsaw, xxxxxx 32 </font> <br>" _
    & " <br>" _
    & " <font size=""2""> <a href=""www.google.com""> www.google.com </a> / <a href=""www.google.com""> BLAH</a> </font> <br>" _
    & " <font size=""1""> blah blah blah blah blah</font>"
    
With OMail
    .HTMLBody = Subject1 & .HTMLBody
    .Display
Set OMail = Nothing
End With

End Sub

用户窗体2

Private Sub UserForm_Initialize()
  
  With ComboBox1
    .AddItem "Subject1"
    .AddItem "Subject2"
    .AddItem "Subject3"
  End With
End Sub


Private Sub btnOK_Click()
    lstNum = ComboBox1.ListIndex
    Unload Me
End Sub

模块2

Public Sub ChooseTemplate()

Subject1 = "<b><font face=""Artifakt Element"" size=""3"" color=""#003366"">blah blah</font></b><br>" _
        & " <b> <font size=""2"" color=""black""> Blah </b> </font> <br>" _
        & " <font size=""2""> Phone: +99 999 999 999 / Mobile: +99 999 999 999 / Mail : <a href=""mailto:blahblah@blah.com""> blah blah </a> </font> <br>" _
        & " <font size=""2""> <b> BLAH-Blah blah blah </b> Warsaw, xxxxxx 32 </font> <br>" _
        & " <br>" _
        & " <font size=""2""> <a href=""www.google.com""> www.google.com </a> / <a href=""www.google.com""> BLAH</a> </font> <br>" _
        & " <font size=""1""> blah blah blah blah blah</font>"

Dim OMail As Outlook.MailItem
Dim oContact As Outlook.ContactItem

'If TypeName(ActiveExplorer.Selection.Item(1)) = "ContactItem" Then
' Set oContact = ActiveExplorer.Selection.Item(1)

Dim msg As Outlook.MailItem
    If Inspector.CurrentItem.Class = olMail Then
        Set msg = Inspector.CurrentItem
        If msg.Size = 0 Then

Dim strTemplate As String
 Set OMail = Application.CreateItem(olMailItem)
  UserForm1.Show
  
  

    Select Case lstNum
    Case -1
         OMail.Subject = ""
    Case 0
         OMail.Subject = "Subject_1"
    Case 1
         OMail.Subject = "Subject_2"
    Case 2
         OMail.Subject = "Subject_3"
    End Select
End If
With OMail
  .To = oContact.Email1Address
  .ReadReceiptRequested = True
  .Subject = "Test Makro"
  .Body = "Hi " & oContact.FirstName & "," & vbCrLf & vbCrLf & OMail.Body
  .Display
End With
  End If
Set OMail = Nothing


End Sub

推荐阅读