【问题标题】:Excel Userform - get HTML from clipboardExcel Userform - 从剪贴板获取 HTML
【发布时间】:2013-06-03 22:12:07
【问题描述】:

Excel 2010、2013

我在剪贴板上有一些 HTML,我想通过 Excel 用户窗体对其进行解析。

我可以使用 VB.Net 检索剪贴板上的格式,并且“HTML 格式”列在返回的数组中。但是 EXCEL VBA 中的 s = MyDataobj.GetText("HTML Format") 失败。事实上,我无法将任何参数传递给GetText() 以返回任何内容。我可以将剪贴板粘贴到电子表格中,Excel 可以很好地粘贴 HTMl 表。

将数据放在剪贴板上的程序是 Lotus Notes,所以谁知道可能存在哪些特殊格式。

有没有办法在 VBA 中发现 DataObject 中可用的可用格式(以及用于检索数据的幻数/字符串)?

这是我用于提取文本的代码。我应该能够通过 GetText 检索其他格式,但我不知道要传递的参数值。

        Public Function GetText() As String
        On Error GoTo Local_err
            Dim MyData   As DataObject
            Dim strClip   As String

            Set MyData = New DataObject
            MyData.GetFromClipboard
            GetText = MyData.GetText
        local_exit:
            Exit Function
        Local_err:
            MsgBox Err & " " & Err.Description & vbCrLf & vbCrLf & "GetText from Clipboard: text not found"
            Resume local_exit
            Resume
        End Function

【问题讨论】:

    标签: excel userform vba


    【解决方案1】:

    使用 VBA,您只能从数据对象中获取文本。我认为您需要为此进行 api 调用-Chip Pearson 有示例代码:http://www.cpearson.com/excel/Clipboard.aspx 这可能会对您有所帮助。

    【讨论】:

    • 谢谢 - 我快速查看了代码,但没有找到获得替代格式的方法。我可能会使用一个临时工作表并将 HTML 粘贴到那里,然后从单元格中提取数据。
    【解决方案2】:

    可以从 VBA 的剪贴板访问替代格式。

    这个“食谱”(在vbaccelerator 找到)可以解决问题:

    1) 创建一个新的代码模块并将以下代码放入其中:

    ' Clipboard functions:
    Private Declare Function OpenClipboard Lib "USER32" (ByVal hWnd As Long) As Long
    Private Declare Function CloseClipboard Lib "USER32" () As Long
    Private Declare Function GetClipboardData Lib "USER32" (ByVal wFormat As Long) As Long
    Private Declare Function IsClipboardFormatAvailable Lib "USER32" (ByVal wFormat As Long) As Long
    Private Declare Function RegisterClipboardFormat Lib "USER32" Alias "RegisterClipboardFormatA" (ByVal lpString As String) As Long
    ' Memory functions:
    Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
    
    Public Function GetClipboardIDForCustomFormat(ByVal sName As String) As Long
        Dim wFormat As Long
        wFormat = RegisterClipboardFormat(sName & Chr$(0))
        If (wFormat > &HC000&) Then
            GetClipboardIDForCustomFormat = wFormat
        End If
    End Function
    
    Public Function GetClipboardDataAsString(ByVal hWndOwner As Long, ByVal lFormatID As Long) As String
        Dim bData() As Byte
        Dim hMem   As Long
        Dim lSize  As Long
        Dim lPtr   As Long
    
        ' Open the clipboard for access:
        If (OpenClipboard(hWndOwner)) Then
            ' Check if this data format is available:
            If (IsClipboardFormatAvailable(lFormatID) <> 0) Then
                ' Get the memory handle to the data:
                hMem = GetClipboardData(lFormatID)
                If (hMem <> 0) Then
                    ' Get the size of this memory block:
                    lSize = GlobalSize(hMem)
                    If (lSize > 0) Then
                        ' Get a pointer to the memory:
                        lPtr = GlobalLock(hMem)
                        If (lPtr <> 0) Then
                            ' Resize the byte array to hold the data:
                            ReDim bData(0 To lSize - 1) As Byte
                            ' Copy from the pointer into the array:
                            CopyMemory bData(0), ByVal lPtr, lSize
                            ' Unlock the memory block:
                            GlobalUnlock hMem
    
                            ' Now return the data as a string:
                            GetClipboardDataAsString = StrConv(bData, vbUnicode)
    
                        End If
                    End If
                End If
            End If
            CloseClipboard
        End If
    
    End Function
    

    2) 在您的代码中插入类似

    Dim myContent As String 
    myContent = GetClipboardDataAsString(0, 49382)
    

    其中 49382 是 HTML 的格式 ID。

    您可以使用 NirSofts Freeware InsideClipboard 来显示您当前剪贴板中可能存在的多个内容以及相关的格式 ID。

    【讨论】:

      猜你喜欢
      • 2011-02-16
      • 2015-07-29
      • 1970-01-01
      • 2023-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      相关资源
      最近更新 更多