Consulting

Results 1 to 3 of 3

Thread: Clipboard clearing and loading with VBA: a mystery

  1. #1
    VBAX Regular
    Joined
    Feb 2020
    Location
    Auckland, NZ
    Posts
    13
    Location

    Clipboard clearing and loading with VBA: a mystery

    The following code is meant to load the current active document path name into the clipboard so that I can paste it using Ctrl + V into a dialog of a SaveAsDaisy addin (which creates an xml translation). This addin is located on a ribbon tab called "Accessibility".

    Sometimes it works as intended, but sometimes it fails and Ctrl + V pastes nothing into the dialog, even though the path is now visible in the Clipboard panel.

    Sub ClipboardLoadPathName()
    
            Dim docCurrent          As Document
            Dim strPath             As String
            Dim doPath              As DataObject
            Dim oData               As New DataObject 'object to use the clipboard
            
            Set docCurrent = ActiveDocument
            
            On Error GoTo ErrorHandling
            
            ' --------------------------------------------------------------
            ' Clear the clipboard first to make sure it's empty
            ' --------------------------------------------------------------
            
            oData.SetText Text:=Empty       'Clear
            oData.PutInClipboard            'take in the clipboard to empty it
            
            ' --------------------------------------------------------------
            'Return the path of an open document in the documents collection
            ' --------------------------------------------------------------
            
            strPath = Documents(docCurrent).Path
            Debug.Print strPath
            
            ' ----------------------------------------------------------------
            ' Swap out the SaveAsDaisy Output folder for the XML folder
            ' ----------------------------------------------------------------
            
            strPath = Replace(strPath, "SaveAsDaisy Output", "XML")
            
            Set doPath = New DataObject
            doPath.SetText strPath
            doPath.PutInClipboard
                    
            ' ----------------------------------------------------------------
            ' After copying path to clipboard, switch to the Accessibility tab
            ' ----------------------------------------------------------------
            
             docCurrent.Save
                    
            SwitchTab "Accessibility" ' calls another function to switch ribbon tab
    
            Exit Sub
            
    ErrorHandling:
        MsgBox "Check that the Accessibility tab is loaded, or save the file somewhere first, and try again."
    
    End Sub
    The path name always does appear in the Clipboard panel, but when Ctrl + V fails, it also pastes two rectangles into the document which are hex 3F symbols (or question marks if pasted into the VB window). To proceed, I have to click on the item in the Clipboard panel, and manually copy it again from the document, which defeats the whole purpose of the macro.

    Question: is there any obvious reason why this sometimes works and sometimes does not? Is there something weird about Ctrl + V accessing the clipboard contents?

    I have noticed different results from Ctrl + V from different methods of clearing the clipboard:

    1. The method used by this code will result in Ctrl + V entering those two hex 3F symbols into a document. So the clipboard is not really cleared.

    oData.Clear
            oData.SetText Text:=""
            oData.PutInClipboard
    2. The method used by this code results in Ctrl + V pasting nothing at all:

    Sub ClipboardClear()
    
        ' using the relevant declarations 
    
        OpenClipboard (0&)
        EmptyClipboard
        CloseClipboard
        
    End Sub

  2. #2
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    104
    Location
    Does it only paste the rectangles or where exactly are they pasted in relation to the path?

  3. #3
    VBAX Regular
    Joined
    Feb 2020
    Location
    Auckland, NZ
    Posts
    13
    Location
    Rectangles are only pasted wherever the cursor happens to be in a document when the user pastes with Ctrl + V.

    Even if the path is visible in the Clipboard panel, put there by the macro, Ctrl + V can fail to paste it into the document, pasting the symbols instead. But clicking on the item in the Clipboard panel will of course paste the path into the document.

    There seems to be an occasional disconnect between clipboard content and whatever Ctrl + V has access to.

Posting Permissions

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