API-Copy text that is displayed in a message box

Discussion in 'SolidWorks' started by Wayne Tiffany, Jul 26, 2004.

  1. Is there a way to call a message box such that the user can highlight the

    contents and copy to the clipboard? Or, do I have to pop up a user form and

    use that instead?


    WT
     
    Wayne Tiffany, Jul 26, 2004
    #1
  2. You will have to include the Microsoft Forms Object Library in your project.
    If you have some other form in your library the editor probably already
    added it.

    Dim swApp As Object
    Sub main()
    Dim MsgboxText as String
    Dim c As DataObject
    Set swApp = Application.SldWorks
    MsgboxText = "Copy this text."
    answer = MsgBox(MsgboxText, vbYesNoCancel, "Copy Message?")

    If answer = vbYes Then
    Set c = New DataObject
    c.SetText MsgboxText
    c.PutInClipboard
    End If

    End Sub

    Regards,

    Corey Scheich
     
    Corey Scheich, Jul 26, 2004
    #2
  3. If I read this correctly, I will have to put this into VB, rather than a
    macro as the macro can't do the forms?? SW says it doesn't like the
    DataObject type.

    WT
     
    Wayne Tiffany, Jul 26, 2004
    #3
  4. I know it doesn't like that data type that is why you have to add a
    reference in you project to the Microsoft Forms Object Library. You do this
    by Tools>References in the dialogue put a checkmark next to Microsoft Forms
    X.X Oject Library. On my computer it is Microsoft Forms 2.0 Oject Library.
    Then if you run the code SW will like that object type, because it is
    defined in that library. Hope that helps.

    Corey
     
    Corey Scheich, Jul 26, 2004
    #4
  5. I don't find Microsoft Forms... in my list. Did you catch my reference to
    doing this in a macro inside SW, rather than in a project in VB? I'm
    thinking that this forces me to do it in VB - correct?

    WT
     
    Wayne Tiffany, Jul 26, 2004
    #5
  6. It may be at the top of the list because it was included in another project
    recently. I did catch that and that is not the case because I tested it in
    a SW macro. If you Insert>Userform it will automatically add it to your
    references. Then you can delete the userform, but it has to be in the list.

    Corey
     
    Corey Scheich, Jul 26, 2004
    #6
  7. I am sending you the example

     
    Corey Scheich, Jul 26, 2004
    #7
  8. Wayne Tiffany

    rocheey Guest

    2 other ways come yo mind:

    1) Easy way:
    Use an INPUTBOX, and seed the initial text

    2) Hard way:
    API code follows:




    Declare Function TSB_API_GlobalAlloc Lib "kernel32" Alias "GlobalAlloc" _
    (ByVal wFlags As Long, ByVal dwBytes As Long) _
    As Long

    Declare Function TSB_API_GlobalLock Lib "kernel32" Alias "GlobalLock" _
    (ByVal hMem As Long) _
    As Long

    Declare Function TSB_API_lstrCopy Lib "kernel32" Alias "lstrcpyA" _
    (ByVal lpString1 As Any, ByVal lpString2 As Any) _
    As Long

    Declare Function TSB_API_GlobalUnlock Lib "kernel32" Alias "GlobalUnlock" _
    (ByVal hMem As Long) _
    As Long

    Private Const GHND = &H42
    Private Const CF_TEXT = 1

    Private Function SetClipboardData(ByVal sTextToWrite As String) As Boolean

    Dim lHoldMem As Long
    Dim lGlobalMem As Long
    Dim lClipMem As Long
    Dim lTmp As Long

    On Error GoTo SetClipboardData_Error

    ' Allocate moveable global memory.
    lHoldMem = TSB_API_GlobalAlloc(GHND, Len(sTextToWrite) + 1)

    ' Lock the block to get a far pointer to this memory.
    lGlobalMem = TSB_API_GlobalLock(lHoldMem)

    ' Copy the string to this global memory.
    lGlobalMem = TSB_API_lstrCopy(lGlobalMem, sTextToWrite)

    ' Unlock the memory.
    If TSB_API_GlobalUnlock(lHoldMem) = 0 Then

    ' Open the Clipboard to copy data to.
    If TSB_API_OpenClipboard(0&) <> 0 Then

    ' Clear the Clipboard.
    lTmp = TSB_API_EmptyClipBoard()

    ' Copy the data to the Clipboard.
    lClipMem = TSB_API_SetClipboardData(CF_TEXT, lHoldMem)

    lTmp = TSB_API_CloseClipboard()
    End If
    End If

    SetClipboardData = True

    SetClipboardData_Exit:
    Exit Function

    SetClipboardData_Error:
    SetClipboardData = False
    Resume SetClipboardData_Exit
    End Function
     
    rocheey, Jul 27, 2004
    #8
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.