是否可以在MSForms.dataObject剪贴板中列出或枚举格式标识符?
目前正在使用Excel 2007 VBA ......
我设置了正确的引用,并且能够在剪贴板中设置文本,并且如果格式标识符已知且明确命名,则可以再次检索它,但是是否可以列出或循环遍历所有标识符?以下是我的非工作代码。任何帮助或建议将不胜感激。
Sub GetTextFromClipboard()
Dim DataObj As MSForms.dataObject
Dim lngCounter As Long
If DataObj Is Nothing Then Set DataObj = New MSForms.dataObject
With DataObj
.SetText "Test", "Custom_1"
.PutInClipboard
.SetText "Another Test", "Custom_2"
.PutInClipboard
.SetText "Last Test", "Custom_3"
.PutInClipboard
End With
DataObj.GetFromClipboard
'NON-WORKING PORTION BELOW
For lngCounter = LBound(Application.ClipboardFormats) To UBound(Application.ClipboardFormats)
On Error Resume Next
Debug.Print Application.ClipboardFormats(lngCounter)
Debug.Print DataObj.GetText(lngCounter)
Debug.Print DataObj.GetText(Application.ClipboardFormats(lngCounter))
On Error GoTo 0
Next
'THIS PORTION WORKS
Debug.Print DataObj.GetFormat("Custom")
Debug.Print DataObj.GetText("Custom")
DataObj.Clear
Set DataObj = Nothing
End Sub
我遇到的问题是检索剪贴板中保存的不同格式标识符的值(剪贴板数据)和名称。
所需的最终结果示例:
1) "Test", "Custom_1"
2) "Another Test", "Custom_2"
3) "Last Test", "Custom_3"
答案 0 :(得分:1)
这不是一个真正的答案,但在评论中格式化这将是可怕的。我删除了On error resume next
以查看代码失败的位置。我在Excel 2013上测试您的代码,因为我没有2007,但其中一些应该可以帮助您。我不这样做,你可以使用"custom"
作为参数,因为它只按MSDN接受以下内容:
CommaSeparatedValue
Html
Rtf
Text
UnicodeText
我需要注释掉你的一些代码,因为它在几行上给我造成了错误。
Invalid FORMATETC structure
我会回到为什么我认为这种情况很快就会发生。我想知道xClipBoardFormat类型的友好名称而不是enum号。所以我做了以下。
Public Function ClipboardFormatToString(value As XlClipboardFormat) As String
' This will take the enum value and convert it to a string equivelent.
Select Case value
Case XlClipboardFormat.xlClipboardFormatBIFF
ClipboardFormatToString = "Binary Interchange file format for Excel version"
Case XlClipboardFormat.xlClipboardFormatBIFF12
ClipboardFormatToString = "Binary Interchange file format 12"
Case XlClipboardFormat.xlClipboardFormatBIFF2
ClipboardFormatToString = "Binary Interchange file format 2"
Case XlClipboardFormat.xlClipboardFormatBIFF3
ClipboardFormatToString = "Binary Interchange file format 3"
Case XlClipboardFormat.xlClipboardFormatBIFF4
ClipboardFormatToString = "Binary Interchange file format 4"
Case XlClipboardFormat.xlClipboardFormatBinary
ClipboardFormatToString = "Binary format"
Case XlClipboardFormat.xlClipboardFormatBitmap
ClipboardFormatToString = "Bitmap format"
Case XlClipboardFormat.xlClipboardFormatCGM
ClipboardFormatToString = "CGM format"
Case XlClipboardFormat.xlClipboardFormatCSV
ClipboardFormatToString = "CSV format"
Case XlClipboardFormat.xlClipboardFormatDIF
ClipboardFormatToString = "DIF format"
Case XlClipboardFormat.xlClipboardFormatDspText
ClipboardFormatToString = "Dsp Text format"
Case XlClipboardFormat.xlClipboardFormatEmbeddedObject
ClipboardFormatToString = "Embedded Object"
Case XlClipboardFormat.xlClipboardFormatEmbedSource
ClipboardFormatToString = "Embedded Source"
Case XlClipboardFormat.xlClipboardFormatLink
ClipboardFormatToString = "Link"
Case XlClipboardFormat.xlClipboardFormatLinkSource
ClipboardFormatToString = "Link to the source file"
Case XlClipboardFormat.xlClipboardFormatLinkSourceDesc
ClipboardFormatToString = "Link to the source description"
Case XlClipboardFormat.xlClipboardFormatMovie
ClipboardFormatToString = "Movie"
Case XlClipboardFormat.xlClipboardFormatNative
ClipboardFormatToString = "Native"
Case XlClipboardFormat.xlClipboardFormatObjectDesc
ClipboardFormatToString = "Object description"
Case XlClipboardFormat.xlClipboardFormatObjectLink
ClipboardFormatToString = "Object link"
Case XlClipboardFormat.xlClipboardFormatOwnerLink
ClipboardFormatToString = "Link to the owner"
Case XlClipboardFormat.xlClipboardFormatPICT
ClipboardFormatToString = "Picture"
Case XlClipboardFormat.xlClipboardFormatPrintPICT
ClipboardFormatToString = "Print picture"
Case XlClipboardFormat.xlClipboardFormatRTF
ClipboardFormatToString = "RTF format"
Case XlClipboardFormat.xlClipboardFormatScreenPICT
ClipboardFormatToString = "Screen Picture"
Case XlClipboardFormat.xlClipboardFormatStandardFont
ClipboardFormatToString = "Standard Font"
Case XlClipboardFormat.xlClipboardFormatStandardScale
ClipboardFormatToString = "Standard Scale"
Case XlClipboardFormat.xlClipboardFormatSYLK
ClipboardFormatToString = "SYLK"
Case XlClipboardFormat.xlClipboardFormatTable
ClipboardFormatToString = "Table"
Case XlClipboardFormat.xlClipboardFormatText
ClipboardFormatToString = "Text"
Case XlClipboardFormat.xlClipboardFormatToolFace
ClipboardFormatToString = "Tool Face"
Case XlClipboardFormat.xlClipboardFormatToolFacePICT
ClipboardFormatToString = "Tool Face Picture"
Case XlClipboardFormat.xlClipboardFormatVALU
ClipboardFormatToString = "Value"
Case XlClipboardFormat.xlClipboardFormatWK1
ClipboardFormatToString = "Workbook"
Case Else
ClipboardFormatToString = "Unknown"
End Select
End Function
记下Case Else ... "Unknown"
部分。现在,我知道格式是我在循环中运行的。请注意,我在文件Dim strFormat As String
的开头也有这个,因为我是Option Explicit
的粉丝。
DataObj.SetText "On a scale of 1 to 10 i am awesome"
DataObj.PutInClipboard
DataObj.GetFromClipboard
For lngCounter = LBound(Application.ClipboardFormats) To UBound(Application.ClipboardFormats)
strFormat = ClipboardFormatToString(Application.ClipboardFormats(lngCounter))
Debug.Print "Format is: " & ClipboardFormatToString(Application.ClipboardFormats(lngCounter))
If strFormat <> "Unknown" Then
Debug.Print DataObj.GetText(lngCounter)
End If
在我的直接窗口中将以下内容告知我
Format is: Text
On a scale of 1 to 10 i am awesome
Format is: Unknown
当我考虑“未知”剪贴板格式时,我能够毫无问题地返回文本。可以返回的应该是格式的数字是44,而不是here的注册格式。我唯一能提到的44值是来自一个没有真正解释的帖子。我添加link来表明我试图理解它。
知道您应该能够遍历并使用您想要的标识符。