Results 1 to 18 of 18

Thread: selecting text from msgbox

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2002
    Location
    LONDON
    Posts
    48

    selecting text from msgbox

    Strange request, and I'm pretty sure it's not supported, but is there any any way to allow the text from a message box to be copied and then pasted elsewhere......

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: selecting text from msgbox

    Originally posted by chris22moore
    Strange request, and I'm pretty sure it's not supported, but is there any any way to allow the text from a message box to be copied and then pasted elsewhere......
    you mean a message box from another program?? nope...

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    If it's from a VB MsgBox then do this:

    VB Code:
    1. Option Explicit
    2.  
    3. Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    4. Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    5. Public Const GW_CHILD = 5
    6. Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    7. Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    8. Public Const GW_HWNDNEXT = 2
    9.  
    10. Public Sub GetMsgBoxText(strCaption As String)
    11.  
    12.     Dim strBuffer As String, hOK
    13.     Dim hChild As Long
    14.     Dim strClassName As String
    15.     Dim hMsg As Long
    16.     Dim sName As String
    17.     Dim sGoodText As String
    18.    
    19.     hMsg = FindWindow("#32770", strCaption)
    20.  
    21.     hChild = GetWindow(hMsg, GW_CHILD)
    22.     ' Start looping through all child windows
    23.     Do
    24.     'Get the window class name
    25.     strClassName = Space$(128)
    26.     strBuffer = GetClassName(hChild, ByVal strClassName, 128)
    27.     strClassName = Left$(strClassName, strBuffer)
    28.     If strClassName = "Button" Then
    29.         ' If you want to look at the text of a button
    30.         sName = Space$(128)
    31.         strBuffer = GetWindowText(hChild, sName, 128)
    32.         If strBuffer > 0 Then
    33.             sName = Left$(sName, strBuffer)
    34.         End If
    35.     End If
    36.     If strClassName = "Static" Then
    37.         ' It's a label or msgbox
    38.         sName = Space$(128)
    39.         strBuffer = GetWindowText(hChild, sName, 128)
    40.         If strBuffer > 0 Then
    41.             sName = Left$(sName, strBuffer)
    42.             MsgBox sName
    43.             Exit Do
    44.         End If
    45.     End If
    46.     ' Get the next child window handle.
    47.     hChild = GetWindow(hChild, GW_HWNDNEXT)
    48.     Loop While hChild <> 0
    49.  
    50. End Sub
    51.  
    52. Public Sub Main()
    53.  
    54.     ' Enter the text in the MsgBox caption
    55.     GetMsgBoxText "Project1"
    56.  
    57. End Sub

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2002
    Location
    LONDON
    Posts
    48
    cheers - i'll give it a go

  5. #5

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2002
    Location
    LONDON
    Posts
    48
    Unfortunately after writing the original request I was pulled onto something else so have only now been abke to give it a go.
    This may seem a bit naive, but how do you implement the code example given. And how is this going to allow me to display a msgbox, from which I can then copy the text of the prompt ?

    cheers

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Create a project named Project1 that displays any message box. Create Project2, add a code module, and copy my code to the code module. Run Project1 and display its msgbox. While it is displayed, run Project2 and it will display the text of the message box from Project1.

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2002
    Location
    LONDON
    Posts
    48
    Cool
    Got that working and it's possible something I could use. The thing is it's not quite what I was after. Basically the system I'm looking at uses the VB message box to display system information to users. Some of this information consists of reference numbers which it would be useful to copy and paste elsewhere.
    I therefore need the message box itself to allow a user to select and copy the text from the prompt portion of the message box. Is this possible or do you have any other ideas ?
    One other thing - you pass "#32770" to FindWindow - what does this value represent ?

    Cheers

  9. #9
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611
    May'be it is helpful to know that [ctrl]-[shift]-C copy the text of a msgbox. Not all msgbox's but most of them

  10. #10
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611
    Well you could write you're own msgbox function, with a form.

  11. #11

    Thread Starter
    Member
    Join Date
    Oct 2002
    Location
    LONDON
    Posts
    48
    ctrl shift c copies the text for title, prompt and button text which is noit what I'm after really.
    I could write my own msgbox, but I fancy playing especially if it involves some windows api stuff as I could do with a bit of practice at this........

  12. #12

    Thread Starter
    Member
    Join Date
    Oct 2002
    Location
    LONDON
    Posts
    48
    any more on this one or can it be closed ?!!!

  13. #13
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    A really whacky way is to use an INPUTBOX instead of a MSGBOX with the referred numbers being set as the default text. You just have to ignore any returned values of the INPUTBOX and have to make do with "OK" and "CANCEL" buttons only.

    Regards

    KayJay

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  14. #14
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Originally posted by chris22moore
    Cool
    Got that working and it's possible something I could use. The thing is it's not quite what I was after. Basically the system I'm looking at uses the VB message box to display system information to users. Some of this information consists of reference numbers which it would be useful to copy and paste elsewhere.
    I therefore need the message box itself to allow a user to select and copy the text from the prompt portion of the message box. Is this possible or do you have any other ideas ?
    One other thing - you pass "#32770" to FindWindow - what does this value represent ?

    Cheers
    I picked up this code from someone else a while back. #32770 is apparently the VB class name of a MsgBox.

    As was suggested by Lightning, the best solution for you is probably to use your own form.

  15. #15
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Originally posted by chris22moore
    Cool
    Got that working and it's possible something I could use. The thing is it's not quite what I was after. Basically the system I'm looking at uses the VB message box to display system information to users. Some of this information consists of reference numbers which it would be useful to copy and paste elsewhere.
    I therefore need the message box itself to allow a user to select and copy the text from the prompt portion of the message box. Is this possible or do you have any other ideas ?
    One other thing - you pass "#32770" to FindWindow - what does this value represent ?

    Cheers
    Do you have the code for this application, or is it one you installed on ya machine that was written by someone else?
    If you have the code for it, then change the code so instead of displaying the info in a MsgBox, create a custom form that displays the infor in text boxs...

    Woka

  16. #16

    Thread Starter
    Member
    Join Date
    Oct 2002
    Location
    LONDON
    Posts
    48
    in the end i created my own msgbox using form + textbox.
    I guess I just wanted to play with the win api.
    cheers for all the advice

  17. #17
    Hyperactive Member Dmitri K's Avatar
    Join Date
    Sep 2002
    Location
    West Palm Beach, FL
    Posts
    444
    It's funny, if you pull the text from the message box that YOUR program created, and even add the code to press the button on the message box to self close it, now the question arises? Why creating such a message box? Instead of Msgbox "This text here needs to be copied", use str = "This text here needs to be copied", and than msgbox istr (if you still need to show the message box). If it's an error from a dll or what not, i know there's an API to get the last error, but I don't remember it.

  18. #18

    Thread Starter
    Member
    Join Date
    Oct 2002
    Location
    LONDON
    Posts
    48
    not quite sure i understand what you mean.......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width