取消按钮执行了选择操作而不是取消操作

4

我正在使用以下代码,用于 Outlook UserForm Select_Email_Template 的后台。

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允许用户选择电子邮件模板。当用户选择了一个模板并点击OK时,该模板将在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中将打开列表中的第一个模板。

如何在不同时打开第一个模板的情况下关闭表单?

3个回答

3

虽然使用全局变量可以解决这个问题,但更加优雅的解决方法是使用UserForm.Tag属性将结果传回主过程。

请注意,在卸载UserForm时也会删除标签值,因此需要在单击处理程序中隐藏UserForm,在主过程中使用标签值,然后再卸载UserForm。

Select_Email_Template UserForm 代码:

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,当用户点击取消时,返回有效索引(在你的示例中为0到7),当用户点击确定时。

代码还将ALT+F4、单击关闭框(及其键盘快捷键等效项ALT+SPCC)和任何其他关闭UserForm的方法重定向到取消按钮的单击处理程序。

我还有权利重构您的代码,使所有模板数据仅在一个地方声明一次,即在Templates()函数中。

我在此函数中使用了静态变量,以便数组只初始化一次。您可以只用Dim声明它,跳过空检查,它仍然可以正常工作。


注意:如果您对我的变量命名约定感到好奇,它基于RVBA


嗨@robinCTS,非常感谢您的付出和解释。它完美地运行。非常感谢和问候 - IRHM

2

全局变量lstnum最初为0。由于您将lstnum用作选择触发器,请将其更改为-1,以符合规范。

选择电子邮件模板的Outlook表单

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

2

@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    

@IRHM 我能想到的另一件事是,在用户窗体按钮(取消按钮)的属性中,将“Cancel”字段设置为“True”。 - Mitch
嗨 @Mitch,感谢您回复我。不幸的是,当我这样做时,OK按钮不再起作用。敬礼 - IRHM
@IRHM 我已经发布了一个可工作的示例,请测试。我想你会很高兴。当我要发布这个时,没有其他人回复,然后我看到还有两个非常好的答案。 - Mitch
虽然这个代码可以运行,但是 This should exit 字符串有点丑陋。我建议使用 None 作为更好的替代方案。甚至可以使用 请选择一个模板 - robinCTS
@robinCTS 没有争议。这只是我在测试时使用的一些东西,以确保它做我想要的事情。实际上整个东西都相当丑陋,但是能用。我的大部分东西基本上都是这样。我不会说我是一个程序员,当然也不是黑客,但更多的是一个骗子。在62岁时,功能比美学更重要。(微笑)。我实际上投票支持你的解决方案。问候。-mm - Mitch
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接