2017-08-23 38 views
4

我在名爲Select_Email_Template的Outlook用戶窗體後面使用了以下代碼。取消按鈕進行選擇而不是取消

Private Sub UserForm_Initialize() 
    With ComboBox1 
    .AddItem "Account Amendment Non SC" 
    .AddItem "Account Amendment SC Application Received" 
    .AddItem "Account Amendment SC" 
    .AddItem "Account Creation Non SC" 
    .AddItem "Account Creation SC Application Received" 
    .AddItem "Account Creation SC" 
    .AddItem "Export Function" 
    .AddItem "Password Reset" 
    End With 
End Sub 

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

Private Sub btnCancel_Click() 
    Unload Select_Email_Template 
End Sub 

ComboBox允許用戶選擇電子郵件模板。當選擇一個,並且確定單擊時,模板在Outlook中打開。

這是這將打開模板代碼:

Public lstNum As Long 

Public Sub Email_Templates() 

    Dim outMail As Outlook.MailItem 

    Select_Email_Template.Show 

    Select Case lstNum 

    ' Following the listbox entries 


    Case 0 
     Set outMail = CreateItemFromTemplate("TemplatePath\Account Amendment Non SC.oft") 

    Case 1 
     Set outMail = CreateItemFromTemplate("TemplatePath\Account Amendment SC Application Received.oft") 

    Case 2 
     Set outMail = CreateItemFromTemplate("TemplatePath\Account Amendment SC.oft") 

    Case 3 
     Set outMail = CreateItemFromTemplate("TemplatePath\Account Creation Non SC.oft") 

    Case 4 
     Set outMail = CreateItemFromTemplate("TemplatePath\Account Creation SC Application Received.oft") 

    Case 5 
     Set outMail = CreateItemFromTemplate("TemplatePath\Account Creation SC.oft") 

    Case 6 
     Set outMail = CreateItemFromTemplate("TemplatePath\Export Function.oft") 

    Case 7 
     Set outMail = CreateItemFromTemplate("TemplatePath\Export Function.oft") 

    End Select 

    ' Use for a specific purpose not randomly 
    ' On Error Resume Next 

    With outMail 
     .Display 
    End With 

    ' On Error GoTo 0 

cleanup: 
     Set outMail = Nothing 

    End Sub 

當用戶點擊取消,窗體關閉,但第一個模板從列表中打開Outlook。

如何在沒有此第一個模板的同時打開的情況下關閉表單?

回答

2

雖然它可以通過使用全局變量來解決這個問題,一個非常巧妙的解決辦法是使用UserForm.Tag屬性將結果傳遞迴主過程。

注意,卸載窗體還刪除標記值,所以你需要隱藏在單擊處理用戶窗體,在主過程中使用的標記值,然後卸載用戶窗體。

Select_Email_Template用戶窗體代碼:

Private Sub UserForm_Initialize() 

    Dim varTemplateName As Variant 

    With ComboBox1 
    For Each varTemplateName In Templates(NameOnly:=True) ' Templates() also works 
     .AddItem varTemplateName 
    Next 
    End With 

End Sub 

Private Sub btnOK_Click() 
    Me.Tag = Me.ComboBox1.ListIndex 
    Me.Hide 
End Sub 

Private Sub btnCancel_Click() 
    Me.Tag = -1 
    Me.Hide 
End Sub 

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) 
    If CloseMode = VBA.VbQueryClose.vbFormControlMenu Then 
    Cancel = True 
    btnCancel_Click 
    End If 
End Sub 

非類模塊代碼:

Public Sub Email_Templates() 
    With Select_Email_Template 
    .Show 
    If .Tag <> -1 Then 
     CreateItemFromTemplate(Templates(.Tag, FullPath:=True)).Display ' Templates(.Tag) also works 
    End If 
    End With 
    Unload Select_Email_Template 
End Sub 

Public Function Templates _ 
       (_ 
        Optional ByVal plngIndex As Long = -1 _ 
       , Optional ByVal NameOnly As Boolean = False _ 
       , Optional ByVal FullPath As Boolean = False _ 
       ) _ 
     As Variant 

    Const strcTemplatesDir As String = "<TemplatesPath>\" 
    Const strcTemplateExtension As String = ".oft" 

    Static avarTemplateNames As Variant 

    If IsEmpty(avarTemplateNames) Then 
    avarTemplateNames = Array _ 
    (_ 
     "Account Amendment Non SC" _ 
    , "Account Amendment SC Application Received" _ 
    , "Account Amendment SC" _ 
    , "Account Creation Non SC" _ 
    , "Account Creation SC Application Received" _ 
    , "Account Creation SC" _ 
    , "Export Function" _ 
    , "Export Function" _ 
    ) 
    End If 
    If plngIndex <> -1 Then 
    If NameOnly = True And FullPath = False Then 
     Templates = avarTemplateNames(plngIndex) 
    Else 
     Templates = strcTemplatesDir & avarTemplateNames(plngIndex) & strcTemplateExtension 
    End If 
    Else 
    Templates = avarTemplateNames 
    End If 

End Function 

說明:

這裏的主要思想是返回,通過Select_Email_Template.Tag性質, -1當用戶點擊取消和一個當用戶點擊OK時,有效索引(在您的示例中爲0到7)。

該代碼還重定向ALT + F4,點擊關閉框(和它的鍵盤快捷鍵等效ALT + SPC; Ç),並關閉窗體到的任何其它方法取消按鈕的點擊處理程序。

我也冒昧地重構你的代碼,所以所有的模板數據只聲明一次,只在一個地方,即在Templates()函數中。

我在這個函數中使用了一個靜態變量,這樣該數組只能初始化一次。你可以用Dim來聲明它,並跳過空的檢查,它仍然可以正常工作。


注意:如果您想了解我的變量命名約定,它是基於RVBA

+0

嗨@robinCTS,非常感謝你把它們放在一起並進行解釋。它完美的作品。 非常感謝和親切的問候 – IRHM

1

@IRHM我有以下工作正常。我已經使用MsgBoxes進行測試,並測試了每個選項。嘗試一下。一旦它看起來像是爲你工作,註釋掉或刪除不必要的東西,改變變量名稱,你應該很好去。

Sub Email_Templates() 
Dim ComboBox1 
Dim intCount As Integer 
Dim intSelectedIndex As Integer 
Dim myNum As Integer 

'Dim outMail As Outlook.MailItem 

Userform1.Show 

myNum = Userform1.ComboBox1.ListIndex 

If myNum = 0 Then 
    Goto Abort 
Else 

MsgBox ("Back to the main module") 

Select Case myNum 

Case 1 
    'Using MsgBox to test and make sure it's working 
    MsgBox "You selected Account Amendment Non SC - Option 1" 
    'Set outMail = CreateItemFromTemplate("TemplatePath\Account Amendment Non SC.oft") 

Case 2 
    MsgBox "You selected Account Amendment SC Application Received - Option 2" 
    'Set outMail = CreateItemFromTemplate("TemplatePath\Account Amendment SC Application Received.oft") 

Case 3 
    MsgBox "You Selected Account Amendment SC - Option 3" 
    'Set outMail = CreateItemFromTemplate("TemplatePath\Account Amendment SC.oft") 
End Select 
Abort: 
End If 
Unload UserForm1 
End Sub 

把下面的行用戶窗體代碼模塊中:

Sub btnCancel_Click() 
    MsgBox ("Cancel button selected") 
    Unload Me 
End Sub 

Sub btnOK_Click() 
    MsgBox ("OK Selected") 
    Me.Hide 
End Sub 

Sub UserForm_Initialize() 
'Put all your AddItems here 
    With ComboBox1 
    .Clear 
    .AddItem "This should exit", 0 
    .AddItem "Selection 1", 1 
    .AddItem "Selection 2", 2 
    .AddItem "Selection 3", 3 
    End With 
End Sub  
+0

嗨@Mitch謝謝你把它放在一起。 我已經把所有的代碼放在Userform模塊中,但不幸的是問題仍然存在。 非常感謝和問候 – IRHM

+0

@IRHM我可以想到的唯一的其他事情是,在用戶窗體按鈕(取消按鈕)的屬性中,將「取消」字段設置爲「真」 – Mitch

+0

Hi @Mitch,謝謝你的光臨回到我身邊。不幸的是,當我這樣做時,確定按鈕不再起作用。 親切的問候 – IRHM

2

全局變量lstnum最初是0。由於您使用lstnum作爲選擇觸發使其-1,以符合規範。

Outlook Form To Select Email Template

Case -1 
' -1 is what you want to use if nothing is selected 

根據您正在使用從窗體返回的選擇方法。

Private Sub btnCancel_Click() 
    lstNum = -1 
    Unload Select_Email_Template 
End Sub