none
VBA Clipboard, Copy RRS feed

  • Question

  • I've looked at the code by Leigh Weber: http://social.msdn.microsoft.com/Forums/office/en-US/ee9e0d28-0f1e-467f-8d1d-1a86b2db2878/a-clipboard-object-for-vba-including-microsoft-word?forum=worddev

    It seems it may be part of a solution to a issue I'm having.  I am trying to create a function to determine if the selection in a Word document is discontinuous.  The process I have works, but it involves overwriting what the user may already have in the clipboard.

    If I could use Leigh's class somehow to replace my:

    Selection.Copy

    with something copies the selection to this VBAClipboard then maybe the user's clipboard would remain unaffected.  I can't figure it how.  Thanks.

    Sub test()
      MsgBox IsDiscontiguous
    End Sub
    Function IsDiscontiguous() As Boolean
    Dim lngRefCount As Long
    Dim strRefText As String
    Dim lngCharacterCount As Long
    Dim oCB As MSForms.DataObject
    Dim strCBText As String
    Dim i As Long
      IsDiscontiguous = False
      If Selection.Type = wdSelectionIP Then Exit Function
      strRefText = Selection.Text
      strRefText = Replace(strRefText, Chr(10), "")
      strRefText = Replace(strRefText, Chr(11), "")
      strRefText = Replace(strRefText, Chr(13), "")
      lngRefCount = Len(strRefText)
      Selection.Copy
      Set oCB = New MSForms.DataObject
      oCB.GetFromClipboard
      strCBText = oCB.GetText
      strCBText = Replace(strCBText, Chr(10), "")
      strCBText = Replace(strCBText, Chr(11), "")
      strCBText = Replace(strCBText, Chr(13), "")
      lngCharacterCount = Len(strCBText)
      If lngRefCount <> lngCharacterCount Then
        IsDiscontiguous = True
      End If
    End Function
    


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Monday, July 14, 2014 12:04 PM

All replies

  • Hi,

    Yes, the macro will overwrite the data that the user may already have in the clipboard.

    I would suggest you storing data from clipboard as String and setting data of clipboard in the end.

    Please try:

    Function IsDiscontiguous() As Boolean
        Dim lngRefCount As Long
        Dim strRefText As String
        Dim lngCharacterCount As Long
        Dim oCB As MSForms.DataObject
        Dim strCBText As String
        Dim temp As String
        Dim i As Long
        IsDiscontiguous = False
        If Selection.Type = wdSelectionIP Then Exit Function
        strRefText = Selection.Text
        strRefText = Replace(strRefText, Chr(10), "")
        strRefText = Replace(strRefText, Chr(11), "")
        strRefText = Replace(strRefText, Chr(13), "")
        lngRefCount = Len(strRefText)
        Set oCB = New MSForms.DataObject
        oCB.GetFromClipboard
        temp = oCB.GetText
        Selection.Copy
        Set oCB = New MSForms.DataObject
        oCB.GetFromClipboard
        strCBText = oCB.GetText
        strCBText = Replace(strCBText, Chr(10), "")
        strCBText = Replace(strCBText, Chr(11), "")
        strCBText = Replace(strCBText, Chr(13), "")
        lngCharacterCount = Len(strCBText)
        If lngRefCount <> lngCharacterCount Then
          IsDiscontiguous = True
        End If
        Set oCB = New MSForms.DataObject
        oCB.SetText (temp)
        oCB.PutInClipboard
    End Function
    Hope this helps.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, July 16, 2014 2:49 AM
    Moderator
  • George,

    That seems to be an improvement. However if the user's Windows clipboard content is something other than just text (e.g., formatted text, or a graphic, or a mix, etc.) then the user's content is still altered. 

    Leigh's class seems to be able to accommodate whatever format the Windows format can handle, but I don't see a way to put the selection in the VBA clipboard class (i.e., VBAClipboard.copy)


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Wednesday, July 16, 2014 3:25 AM
  • Hi,

    Have a look at:

    How to: Retrieve Information from the Clipboard

    How to: Send Information to the Clipboard


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, July 16, 2014 6:04 AM
    Moderator
  • George,

    Did you read the title of this post?  I have seen these links before.  I am trying to send (or rather copy the selection) to an instance of Leigh Weber's "VBAClipboard" class not the windows clipboard.  I could be wrong, but don't see how these links help in this situation.


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Wednesday, July 16, 2014 12:30 PM
  • Hi,

    Thank you for the response.

    I'm trying to involve some senior engineers into this issue and it will take some time. Your patience will be greatly appreciated.

    Sorry for any inconvenience and have a nice day!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, July 17, 2014 3:11 AM
    Moderator
  • Hi Greg,

    We are aware that there are limitations with discontiguous selections from Object Model level and are captured in the following article.

    http://support.microsoft.com/kb/288424

    For your current use case where you would like to check if the selection is contiguous or not. You may use the following workaround.

    Sub NonContigTest()
        On Error Resume Next
        Selection.ConvertToTable
        If Err.Number = 4605 Then
            MsgBox "non contig"
        Else
            ActiveDocument.Undo
            MsgBox "contig"
        End If
    End Sub


    Sangeeth,MSFT

    Friday, July 25, 2014 2:35 PM