取消按钮进行选择而不是取消
Cancel Button makes a selection rather than cancelling
我在名为 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 允许用户select 电子邮件模板。 select编辑并单击 确定 后,模板将在 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
将以下行放入用户窗体代码模块中:
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 作为 Select 触发器,因此将其设置为 -1,以符合规范。
Case -1
' -1 is what you want to use if nothing is selected
按照您使用的方法return从用户表单中选择。
Private Sub btnCancel_Click()
lstNum = -1
Unload Select_Email_Template
End Sub
虽然 可以通过使用全局变量来解决问题,但更简洁的解决方案是使用 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
非Class模块代码:
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
解释:
这里的主要思想是 return,通过 Select_Email_Template.Tag
属性,当用户点击 取消 和一个 -1当用户单击 OK.
时的有效索引(在您的示例中为 0 到 7)
该代码还重定向 ALT+F4,单击关闭框(及其等效的键盘快捷键 ALT+SPC;C),以及任何其他关闭用户窗体的方法,到取消按钮的点击处理程序。
我还冒昧地重构了您的代码,因此所有模板数据仅在一个地方声明一次,即在 Templates()
函数中。
我在这个函数中使用了一个静态变量,这样数组只会被初始化一次。您可以只用 Dim
声明它并跳过空检查,它仍然可以正常工作。
注意:如果你对我的变量命名规则感到好奇,它是基于 RVBA.
我在名为 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 允许用户select 电子邮件模板。 select编辑并单击 确定 后,模板将在 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
将以下行放入用户窗体代码模块中:
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 作为 Select 触发器,因此将其设置为 -1,以符合规范。
Case -1
' -1 is what you want to use if nothing is selected
按照您使用的方法return从用户表单中选择。
Private Sub btnCancel_Click()
lstNum = -1
Unload Select_Email_Template
End Sub
虽然 可以通过使用全局变量来解决问题,但更简洁的解决方案是使用 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
非Class模块代码:
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
解释:
这里的主要思想是 return,通过 Select_Email_Template.Tag
属性,当用户点击 取消 和一个 -1当用户单击 OK.
该代码还重定向 ALT+F4,单击关闭框(及其等效的键盘快捷键 ALT+SPC;C),以及任何其他关闭用户窗体的方法,到取消按钮的点击处理程序。
我还冒昧地重构了您的代码,因此所有模板数据仅在一个地方声明一次,即在 Templates()
函数中。
我在这个函数中使用了一个静态变量,这样数组只会被初始化一次。您可以只用 Dim
声明它并跳过空检查,它仍然可以正常工作。
注意:如果你对我的变量命名规则感到好奇,它是基于 RVBA.