Vba “取消”按钮进行选择,而不是取消

Vba “取消”按钮进行选择,而不是取消,vba,button,outlook,userform,Vba,Button,Outlook,Userform,我在一个名为Select\u Email\u Template的Outlook用户表单后面使用以下代码 Private Sub UserForm_Initialize() With ComboBox1 .AddItem "Account Amendment Non SC" .AddItem "Account Amendment SC Application Received" .AddItem "Account Amendment SC" .AddItem "

我在一个名为Select\u Email\u 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
组合框允许用户选择电子邮件模板。选择一个模板并单击“确定”后,将在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中打开


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

@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
在UserForm代码模块中放置以下行:

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    

全局变量lstnum最初为0。因为使用lstnum作为选择触发器,所以将其设置为-1,以符合规范

按照您正在使用的方法从userform返回选择

Private Sub btnCancel_Click()
    lstNum = -1
    Unload Select_Email_Template
End Sub
虽然可以通过使用全局变量来解决问题,但更简洁的解决方案是使用
UserForm.Tag
属性将结果传递回主过程

请注意,卸载UserForm也会删除标记值,因此需要在单击处理程序中隐藏UserForm,在主过程中使用标记值,然后卸载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
公共子电子邮件_模板()
使用“选择电子邮件”模板
显示
如果.Tag-1,那么
CreateItemFromTemplate(模板(.Tag,FullPath:=True))。显示“模板(.Tag)也可以工作
如果结束
以
卸载选择电子邮件模板
端接头
公共函数模板_
( _
可选ByVal plngIndex,长度=-1_
,可选的ByVal名称仅为布尔值=False_
,可选的ByVal FullPath为Boolean=False_
) _
作为变体
Const strcTemplatesDir As String=“\”
Const strcTemplateExtension As String=“.oft”
静态avarTemplateNames作为变量
如果是空的(avarTemplateNames),则
avarTemplateNames=数组_
( _
“账户修订非SC”_
,“收到账户修改SC申请”_
,“账户修订SC”_
,“帐户创建非SC”_
,“收到帐户创建SC应用程序”_
,“帐户创建SC”_
,“导出功能”_
,“导出功能”_
)
如果结束
如果plngIndex为-1,则
如果NameOnly=True且FullPath=False,则
模板=avarTemplateNames(plngIndex)
其他的
Templates=strcTemplatesDir&AvartTemplateNames(plngIndex)&strcTemplateExtension
如果结束
其他的
模板=avarTemplateNames
如果结束
端函数
说明:

这里的主要思想是,当用户单击“取消”时,通过
Select\u Email\u Template.Tag
属性返回-1,当用户单击“确定”时返回有效索引(在您的示例中为0到7)

该代码还将ALT+F4、单击关闭框(及其键盘快捷键等价物ALT+SPC;C)以及关闭用户表单的任何其他方法重定向到cancel按钮的单击处理程序

我还对代码进行了重构,这样所有模板数据只声明一次,并且只在一个地方声明,即在
Templates()
函数中

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


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


嗨,米奇,谢谢你把这些放在一起。我已经将所有代码放在Userform模块中,但不幸的是,问题仍然存在。多谢各位regards@IRHM我唯一能想到的另一件事是,在userform按钮(Cancel按钮)的属性中,将“Cancel”字段设置为“True”Hi@Mitch,谢谢你带着这个返回给我。不幸的是,当我这样做时,OK按钮不再起作用。友善的Regards@IRHM我整天都在做这个。我试过你的代码,修改过,仍然得到同样的结果。它与调用userform然后返回脚本有关。取消用户表单只会关闭表单并返回脚本,选择Case lstNum在该点以0的值拾取,这就是为什么它一直选择第一个选项。@IRHM我已经发布了一个工作示例。请测试一下。我想你会高兴的。嗯,当我发布这篇文章时,没有其他人回应,然后我看到了另外两个非常好的答案。嗨@robinCTS,非常感谢你把这篇文章放在一起并给出解释。它工作得很好。非常感谢和亲切的问候
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