28

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?

Community
  • 1
  • 1
nobillygreen
  • 1,548
  • 5
  • 19
  • 27
  • 2
    Have you considered to make a custom message box form? – Joe DF Aug 30 '12 at 02:28
  • If I was more experienced in VBA and knew how to, that sounds like the path I'd go down. However, my experience is small and, as it is, my time is quite limited. – nobillygreen Aug 30 '12 at 03:06

5 Answers5

47

For this code:

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

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

---------------------------
Microsoft Excel
---------------------------
Does Control+C work on a lowly, old message box?
---------------------------
OK   
---------------------------
ray
  • 8,521
  • 7
  • 44
  • 58
13

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

Change these properties of the textbox in design mode.

  1. SpecialEffect - fmiSpecialEffectFlat
  2. BackStyle - Transparent
  3. Locked - True

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

kmote
  • 16,095
  • 11
  • 68
  • 91
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
13

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

Moumit
  • 8,314
  • 9
  • 55
  • 59
najeem
  • 1,841
  • 13
  • 29
4

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

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

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
4

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."
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188