vba 用于列出宏的组合框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11491539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Combo box to list Macros
提问by Rsmithy
I want when you hit 'send' you are presented with a form. This I am developing in Outlook 2010.
我希望当您点击“发送”时,您会看到一个表格。这是我在 Outlook 2010 中开发的。
Is there a way to populate a combo-box with a list of Macros?
有没有办法用宏列表填充组合框?
Public Sub Confidential()
Application.ActiveInspector.CurrentItem.Sensitivity = olConfidential
Application.ActiveInspector.CurrentItem.Save
Set MsgSub = Outlook.Application.ActiveInspector.CurrentItem
Set objMail = Outlook.Application.ActiveInspector.CurrentItem
Subject = MsgSub.Subject
MsgSub.Subject = Subject + " - [CONFIDENTIAL]"
Email = objMail.HTMLBody
info = " <html> <body> <FONT color=#666666> <font-size: 11px> <p></p> AUTO TEXT: This message has been marked as 'CONFIDENTIAL' please treat it as such </body> </font> </html>"
objMail.HTMLBody = Email + info
End Sub
Private Sub Sens_DropButtonClick()
Sens.AddItem "Confidential()"
Sens.AddItem "Normal()"
End Sub
Public Sub Send_Click()
Set objMail = Outlook.Application.ActiveInspector.CurrentItem
objMail.Send
End Sub
Would I be right in thinking that this is a public sub?
我认为这是一个公共潜艇是对的吗?
My goal is when you hit the 'send' button a form will appear with a dropdown box, this has 4 options which are the sensitivity options you can use with the emails, except I have created them as macros and added code on them (to add to subject and footer of message) but I wont it so a user is forced to make a selection, hence why I am creating this form instead of having the 4 buttons.
我的目标是当您点击“发送”按钮时,将出现一个带有下拉框的表单,其中有 4 个选项,这些选项是您可以与电子邮件一起使用的敏感度选项,除了我将它们创建为宏并在其上添加了代码(以添加到消息的主题和页脚)但我不会这样做,因此用户被迫进行选择,因此为什么我要创建此表单而不是具有 4 个按钮。
回答by Siddharth Rout
My goal is when you hit the 'send' button a form will appear with a dropdown box, this has 4 options which are the sensitivity options you can use with the emails, except I have created them as macros and added code on them (to add to subject and footer of message) but I wont it so a user is forced to make a selection, hence why I am creating this form instead of having the 4 buttons. – Rsmithy 36 mins ago
我的目标是当您点击“发送”按钮时,将出现一个带有下拉框的表单,其中有 4 个选项,这些选项是您可以与电子邮件一起使用的敏感度选项,除了我将它们创建为宏并在其上添加了代码(以添加到消息的主题和页脚)但我不会这样做,因此用户被迫进行选择,因此为什么我要创建此表单而不是具有 4 个按钮。– Rsmithy 36 分钟前
If I understand you correctly, Yes it is possible to do what you want. See this Example
如果我理解正确,是的,可以做你想做的事。看这个例子
Let's say you have a userform with 4 options A,B,C and D and the userform code is
假设您有一个包含 4 个选项 A、B、C 和 D 的用户表单,并且用户表单代码是
Private Sub UserForm_Initialize()
ComboBox1.AddItem "A"
ComboBox1.AddItem "B"
ComboBox1.AddItem "C"
ComboBox1.AddItem "D"
End Sub
Private Sub CommandButton1_Click()
lstNo = ComboBox1.ListIndex
Unload Me
End Sub
Next Paste this in a module
Next 将此粘贴到模块中
Public lstNo As Long
and this in the ThisOutlookSession
而这在 ThisOutlookSession
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
UserForm1.Show
MsgBox "user chose " & lstNo & "from combo"
Select Case lstNo
Case -1
'User didn't select anything in the combo
Case 0
'User selected option 1 in the combo
Case 1
'User selected option 2 in the combo
Case 2
'User selected option 3 in the combo
Case 3
'User selected option 4 in the combo
End Select
End Sub
Replace the above comments in the Select
Statement` with the Macro Names that you want executed depending on the user choice.
Select
根据用户的选择,用您要执行的宏名称替换Statement` 中的上述注释。
SNAPSHOTS IN ACTION
快照在行动
And this is what you get when you select the Option D (ListIndex 3)
这就是您选择 Option D (ListIndex 3)
FOLLOWUP
跟进
Dim email As String, info As String
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
UserForm1.Show
Select Case lstNo
Case -1
'User didn't select anything, default will be used
Case 0
With Item
.Sensitivity = olNormal
.Save
End With
Case 1
With Item
.Sensitivity = olPersonal
.Save
End With
Case 2
With Item
.Sensitivity = olPrivate
.Save
End With
Case 3
With Item
.Sensitivity = olConfidential
.Subject = .Subject & " - [CONFIDENTIAL]"
Email = .HTMLBody
info = " <html> <body> <FONT color=#666666> <font-size: 11px> <p></p> AUTO TEXT: " & _
"This message has been marked as 'CONFIDENTIAL' please treat it as such </body> </font> </html>"
.HTMLBody = Email & info
.Save
End With
End Select
End Sub
回答by JimmyPena
Is there a way to populate a combo-box with a list of Macros?
有没有办法用宏列表填充组合框?
Not in Outlook, I'm afraid. Programmatic access to the VBA IDE isn't supported in Outlook due to the possibility of spreading viruses or conducting malicious activity via email.
恐怕不在 Outlook 中。由于可能通过电子邮件传播病毒或进行恶意活动,因此 Outlook 不支持对 VBA IDE 的编程访问。
See VBA Extensibility in Outlookfor reference.
请参阅Outlook 中的 VBA 扩展性以供参考。
The closest you can come is by programmatically displaying the "Run Macro" dialog, like this:
最接近的方法是以编程方式显示“运行宏”对话框,如下所示:
ActiveExplorer.CommandBars.FindControl(,186).Execute
ActiveExplorer.CommandBars.FindControl(,186).Execute
However I'm not sure if this is available in Outlook 2010.
但是我不确定这在 Outlook 2010 中是否可用。
But showing a dialog box can't possibly be your goal. Maybe if you explain what your goal is, someone can suggest a better way of reaching it that doesn't require populating a combo box with a list of macro names.
但是显示对话框不可能是您的目标。也许如果你解释你的目标是什么,有人可以建议一种更好的方法来实现它,不需要用宏名称列表填充组合框。