VBA 消息框中的可选文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12188769/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:54:39  来源:igfitidea点击:

Selectable Text in VBA Message Box

vbauser-interfaceexcel-vbaexcel

提问by nobillygreen

When I create a msgbox in VBA, the user cannot interact with the text at all. In my case, I want the user to be able to highlight and copy the text. I'm thinking the better way to do this may be to add a button that copies the text to the clipboard. Any suggestions?

当我在 VBA 中创建一个 msgbox 时,用户根本无法与文本交互。就我而言,我希望用户能够突出显示和复制文本。我认为更好的方法可能是添加一个将文本复制到剪贴板的按钮。有什么建议?

回答by ray

For this code:

对于此代码:

msgbox "Does Control+C work on a lowly, old message box?"

you can press ctrl+ c, open notepad, ctrl+ vand you will get this:

你可以按ctrl+ c,打开记事本,ctrl+v你会得到这个:

---------------------------
Microsoft Excel
---------------------------
Does Control+C work on a lowly, old message box?
---------------------------
OK   
---------------------------

回答by Siddharth Rout

If you want text that is "selectable" then don't use MsgBox. Use a Custom formand instead of a label use a textbox. However...

如果您想要“可选择”的文本,则不要使用 MsgBox。使用自定义表单而不是标签使用文本框。然而...

Change these properties of the textbox in design mode.

在设计模式下更改文本框的这些属性。

  1. SpecialEffect - fmiSpecialEffectFlat
  2. BackStyle - Transparent
  3. Locked - True
  1. 特殊效果 - fmiSpecialEffectFlat
  2. BackStyle - 透明
  3. 锁定 - 真

And then use this code

然后使用此代码

Option Explicit

Private Sub UserForm_Initialize()
    Me.Caption = "Message Box"
    TextBox1.Text = "Hello World!"
End Sub

enter image description here

在此处输入图片说明

回答by najeem

You can use an inputbox instead of a message box

您可以使用输入框代替消息框

inputbox "Copy the below text", "Copy Text", "Text value"

inputbox copy text

输入框复制文本

enter image description here

在此处输入图片说明

回答by Tim Williams

You could use VBA to set the clipboard text if the user typically would copy the entire message.

如果用户通常会复制整个消息,您可以使用 VBA 来设置剪贴板文本。

Or use input box instead of msgbox, since the user can copy from the populated value.

或者使用输入框而不是 msgbox,因为用户可以从填充的值中复制。

回答by Jean-Fran?ois Corbett

You could make your own custom message box using a UserForm. Here's a rudimentary proof-of-concept: a userform with one text box (from which you can select and copy text).

您可以使用用户窗体制作自己的自定义消息框。这是一个基本的概念验证:一个带有一个文本框的用户表单(您可以从中选择和复制文本)。

enter image description here

在此处输入图片说明

Sub MyMsg(msg As String)
    With MsgUserForm ' this is the name I gave to the userform
        .TextBox1.Text = msg
        .Show
    End With
End Sub

Usage:

用法:

MyMsg "General failure reading hard drive."