title: Text To Clipboard in VBA Windows 10 Issue
body: <p>I have a function that I use to send a string to the windows clipboard:</p>
  
  <pre><code>Sub TextToClipboard(ByVal Text As String)
  
    With CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}") 'FM20.dll (Microsoft Forms 2.0 Object Library)
      .SetText Text
      .PutInClipboard
    End With
  
  End Sub
  </code></pre>
  
  <p>I recently upgraded my machine to Windows 10 and now when I run this function it eats everything in my clipboard and replaces it with a few garbage characters. I get different results on what these characters are depending on the application I paste them into:</p>
  
  <ol>
  <li>VBA Editor: ??</li>
  <li>Microsoft Word: ?? (surrounded by boxes)</li>
  <li>Notepad++: xEF xBF xBF xEF xBF xBF (white text surrounded by black boxes)</li>
  </ol>
  
  <p>I took <a href="https://msdn.microsoft.com/en-us/library/office/ff192913.aspx?f=255&amp;MSPPError=-2147217396" rel="noreferrer">code from MSDN</a> to use the Windows API (I made my functions PtrSafe as you'll see below) and the "GlobalUnlock" function returns '1' so I guess it can't allocate the memory correctly.</p>
  
  <pre><code>Option Explicit
  
  #If VBA7 Then
  
  Declare PtrSafe Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) _
     As Long
  Declare PtrSafe Function GlobalLock Lib "kernel32" (ByVal hMem As Long) _
     As Long
  Declare PtrSafe Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, _
     ByVal dwBytes As Long) As Long
  Declare PtrSafe Function CloseClipboard Lib "User32" () As Long
  Declare PtrSafe Function OpenClipboard Lib "User32" (ByVal hwnd As Long) _
     As Long
  Declare PtrSafe Function EmptyClipboard Lib "User32" () As Long
  Declare PtrSafe Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, _
     ByVal lpString2 As Any) As Long
  Declare PtrSafe Function SetClipboardData Lib "User32" (ByVal wFormat _
     As Long, ByVal hMem As Long) As Long
  
  #Else
  
  Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) _
     As Long
  Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) _
     As Long
  Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, _
     ByVal dwBytes As Long) As Long
  Declare Function CloseClipboard Lib "User32" () As Long
  Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) _
     As Long
  Declare Function EmptyClipboard Lib "User32" () As Long
  Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, _
     ByVal lpString2 As Any) As Long
  Declare Function SetClipboardData Lib "User32" (ByVal wFormat _
     As Long, ByVal hMem As Long) As Long
  
  #End If
  
  Public Const GHND = &amp;H42
  Public Const CF_TEXT = 1
  Public Const MAXSIZE = 4096
  
  Sub ClipBoard_SetData(MyString As String)
     Dim hGlobalMemory As Long, lpGlobalMemory As Long
     Dim hClipMemory As Long, X As Long
  
     ' Allocate moveable global memory.
     '-------------------------------------------
     hGlobalMemory = GlobalAlloc(GHND, Len(MyString) + 1)
  
     ' Lock the block to get a far pointer
     ' to this memory.
     lpGlobalMemory = GlobalLock(hGlobalMemory)
  
     ' Copy the string to this global memory.
     lpGlobalMemory = lstrcpy(lpGlobalMemory, MyString)
  
     ' Unlock the memory.
     If GlobalUnlock(hGlobalMemory) &lt;&gt; 0 Then
        MsgBox "Could not unlock memory location. Copy aborted."
        GoTo OutOfHere2
     End If
  
     ' Open the Clipboard to copy data to.
     If OpenClipboard(0&amp;) = 0 Then
        MsgBox "Could not open the Clipboard. Copy aborted."
        Exit Sub
     End If
  
     ' Clear the Clipboard.
     X = EmptyClipboard()
  
     ' Copy the data to the Clipboard.
     hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)
  
  OutOfHere2:
  
     If CloseClipboard() = 0 Then
        MsgBox "Could not close Clipboard."
     End If
  
  End Sub
  </code></pre>
  
  <p>I did get this method to work, but the window pops up for a second and it puts a new line character at the end which isn't exactly ideal, plus it would require having a connection with Excel for the wait function. Not terrible either I guess. </p>
  
  <pre><code>Sub SetClipboard(Text As String)
  
    With CreateObject("WScript.Shell").Exec("clip")
      With .stdIn
        .WriteLine Text
        .Close
      End With
  
      Do While .Status = 0
          Application.Wait 1
      Loop
  
    End With
  
  End Sub
  </code></pre>
  
  <p>Finally, I ran first two functions on another Windows 7 machine via <a href="https://www.microsoft.com/en-us/download/details.aspx?id=44989" rel="noreferrer">Remote Desktop Connection Manager</a> and it successfully ran and changed the clipboard on my Windows 10 machine successfully.</p>
  
  <p>So I'm not sure if doing the upgrade to Windows 10 messed with these libraries or the clipboard is different somehow. Is there any way for me to get these working again? Maybe someone else with Windows 10 and Office won't have the issue at all and it's just my machine?</p>