Results 1 to 8 of 8

Thread: [2005] Paste from clipboard.

  1. #1

    Thread Starter
    Hyperactive Member NPassero's Avatar
    Join Date
    May 2007
    Location
    NJ
    Posts
    272

    [2005] Paste from clipboard.

    I am trying to paste negative values copied from Excel formatted with the (123) into a DataGrid. When I copy it with the column formatted to a decimal it rejects the value because it reads it as a string "(123)" instead of "-123". Is it possible to catch the text from the clipboard and replace the ( with a - and then get rid of the second ) ..
    Last edited by NPassero; Jul 9th, 2007 at 10:36 AM.

  2. #2

    Thread Starter
    Hyperactive Member NPassero's Avatar
    Join Date
    May 2007
    Location
    NJ
    Posts
    272

    Re: [2005] Paste from clipboard.

    I can do it using a button just calling this sub routine. But i need to capture the paste event, anyone know how to pull that off.
    vb.net Code:
    1. Private Sub PasteRoutine()
    2.  
    3.       Dim objClipboard As IDataObject = Clipboard.GetDataObject()
    4.       Dim myString As String = objClipboard.GetData(DataFormats.Text).ToString
    5.  
    6.       Dim first As Integer = myString.IndexOf("(")
    7.       Dim second As Integer = myString.IndexOf(")")
    8.  
    9.       If first > -1 And second > -1 Then
    10.          If IsNumeric(myString.Substring(first, (myString.Length - second) + second)) Then
    11.             myString = myString.Replace("(", "-")
    12.             myString = myString.Replace(")", "")
    13.             formulaTB.Text = myString
    14.          End If
    15.       End If
    16.  
    17.    End Sub

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Paste from clipboard.

    There is no event specifically associated with data being pasted. I'm guessing that there's a Windows message associated with a paste action so you could inherit the control, override the WndProc method and test to find what message(s) that might be. Once you know that you can trap that message and take the appropriate action.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] Paste from clipboard.

    The message is WM_PASTE.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Paste from clipboard.

    I just did a quick test with a TextBox and the TextChanged event was raised before the WM_PASTE message was received, so that would mean that the value would already have been rejected before you became aware that a paste operation was taking place.

    What you could do is instead of using Ctrl+V, select a different key combination (e.g. Ctrl+Alt+V) and trap it in the KeyDown event of the target control. You can then call your PasteRoutine method to process the value that is currently on the clipboard.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member NPassero's Avatar
    Join Date
    May 2007
    Location
    NJ
    Posts
    272

    Re: [2005] Paste from clipboard.

    Alright, I changed the code a little but, this could work perfectly if there was a way to check if the clipboard data has changed. Any event handler to do that ?

  7. #7
    Member
    Join Date
    Jul 2004
    Posts
    40

    Re: [2005] Paste from clipboard.

    Hello Guys... (In the same vein)
    I am doing a Clipboard.SetDataObject(WorkSheetObj.Range("A6:A9").Copy().ToString)

    and am going to OUTLOOK
    and do a control V

    What's being pasted is "TRUE"
    not the contents of those cells in Excel..
    Any ideas how to do this right?

    Thanks in advance

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] Paste from clipboard.

    Looks like you are not using the proper type of the expected data.

    Clipboard.SetText(WorkSheetObj.Range("A6:A9"))

    Or such
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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