[英]Filling the Clipboard with content covering multiple cells which itself are containing line breaks (Alt-Enter) in VBA Excel?
I'm working on a VBA Excel Add-In to provide users with clipboard text containing values from multiple cells, but also having multiple lines within a cell.我正在开发一个 VBA Excel 加载项,为用户提供包含来自多个单元格的值的剪贴板文本,但在一个单元格中也有多行。 Unfortunately I couldn't find a solution which keeps cell seperations as well as line breaks within a cell, correctly.不幸的是,我找不到一个解决方案来正确地保持单元格分离以及单元格内的换行符。 Is there any way of modifying my exemplary Sub "ClipboardDoesntWork" in order to deliver the same clipboard as Sub "CopyPasteWorks" but without using the workaround of filling cells and copying them?有什么方法可以修改我的示例性 Sub“ClipboardDoesntWork”,以便提供与 Sub“CopyPasteWorks”相同的剪贴板,但不使用填充单元格并复制它们的解决方法?
Public Sub CopyPasteWorks()
Dim text1a As String
Dim text1b As String
Dim text2a As String
Dim text2b As String
Dim firstcell As String
Dim secondcell As String
Columns("A:C").Clear
Columns("A:C").ColumnWidth = 20
text1a = "Line 1 in Cell 1"
text1b = "Line 2 in Cell 1"
text2a = "Line 1 in Cell 2"
text2b = "Line 2 in Cell 2"
firstcell = text1a & vbLf & text1b
secondcell = text2a & vbLf & text2b
Range("A1").Value = firstcell
Range("A2").Value = secondcell
Range("A1:A2").Copy
MsgBox "Clipboard filled with content of two cells!" & vbLf & "(covering two lines, each)"
End Sub
Public Sub ClipboardDoesntWork()
'NOTE! Code requires library reference: Microsoft Forms 2.0 Object Library
Dim text1a As String
Dim text1b As String
Dim text2a As String
Dim text2b As String
Dim firstcell As String
Dim secondcell As String
Dim objData As New DataObject
text1a = "Line 1 in Cell 1"
text1b = "Line 2 in Cell 1"
text2a = "Line 1 in Cell 2"
text2b = "Line 2 in Cell 2"
firstcell = text1a & vbLf & text1b
secondcell = text2a & vbLf & text2b
objData.SetText firstcell & vbCrLf & secondcell
objData.PutInClipboard
MsgBox "Clipboard filled with content of four cells!"
End Sub
I tried various versions of line feeds and/or carrier returns (vbLf, vbCr, vbCrLf, vbNewLine, chr(10)) and different techniques of filling the clipboard (MSForms.DataObject, API method), but nothing works so far.我尝试了各种版本的换行和/或载体返回(vbLf、vbCr、vbCrLf、vbNewLine、chr(10))和填充剪贴板的不同技术(MSForms.DataObject、API 方法),但到目前为止没有任何效果。 (My final Code is generating the clipboard string by joing array fields (coming from a database query), but the hurdle I couldn't take can also be demonstrated well with the simplified codes shown above.) (我的最终代码是通过连接数组字段(来自数据库查询)生成剪贴板字符串,但我无法克服的障碍也可以用上面显示的简化代码很好地证明。)
Okay, I have the answer.好的,我有答案了。
Close your File Browser.关闭文件浏览器。
Apparently .PutInClipboard
doesn't work in Windows 10 while browser is open.显然,当浏览器打开时,.PutInClipboard 在.PutInClipboard
10 中不起作用。
Option Explicit
Public Sub ClipboardDoesntWork()
'NOTE! Code requires library reference: Microsoft Forms 2.0 Object Library
Dim text1a As String
Dim text1b As String
Dim text2a As String
Dim text2b As String
Dim firstcell As String
Dim secondcell As String
Dim objData As DataObject
Set objData = New DataObject
text1a = "Line 1 in Cell 1"
text1b = "Line 2 in Cell 1"
text2a = "Line 1 in Cell 2"
text2b = "Line 2 in Cell 2"
firstcell = text1a & Chr(10) & text1b
secondcell = text2a & Chr(10) & text2b
objData.SetText firstcell & Chr(10) & secondcell
objData.PutInClipboard
MsgBox "Clipboard filled with content of four cells!"
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.