在Excel VBA中“粘贴”stringvariables而不是剪贴板的内容?

我有一个stringvariables,在Excel VBA中包含一个HTML表格。 我知道,当这个表存储在剪贴板中,我调用.PasteSpecial时,Excel会做一些漂亮的预处理,并以当前表格中的方式填充当前表格中的单元格。

但是,如果我简单地将单元格/范围的.Value设置为stringvariables,则不会执行这样的预处理,并且将整个string,HTML标记和全部string都转储到单元格中。 我想要以前的结果,但是我不能使用剪贴板,因为它被其他地方的应用程序所使用,并且不能保证我不会覆盖关键数据。 它也是asynchronous使用,所以我不能简单地保存剪贴板的当前内容,使用剪贴板,然后恢复剪贴板的以前的内容。

那么,是否有任何方法来获得“粘贴预处理”发生时设置的格式化string的范围值?

如果有人知道答案,我仍然会好奇,但是我决定继续前进,放弃将表存储在工作表中的想法。 相反,我自己parsing表格,并使用InStr函数find我需要的值(因为它们大部分是相邻的键=值对),这对我的应用程序来说不是非常慢。

我不能想到无需调用剪贴板的Excel预处理器。 对于parsing,你可能想看看分裂function。 这是一个例子。

Sub ParseTable() Dim sHtmlTable As String Dim vaTable As Variant Dim i As Long Const STDSTART = "<td" Const STDEND = "</td" sHtmlTable = "<table border=""1""><tr><td>row 1, cell 1</td><td>row 1, cell 2</td></tr><tr><td>row 2, cell 1</td><td>row 2, cell 2</td></tr></table>" vaTable = Split(sHtmlTable, ">") For i = LBound(vaTable) To UBound(vaTable) If vaTable(i) = STDSTART Then Debug.Print Replace(vaTable(i + 1), STDEND, "") End If Next i End Sub 

这只是一个评论(stackeoverflow不让我评论propper的方式呢)。

你可能可以用你想要的方式使用一些API。

很久以前,我玩弄了它(寻找某种方式来欺骗MS Word),我记得只要input内容types的正确标识,就可以将任何内容存储到剪贴板中(如纯文本,格式化文本, html等)。 存储内容后,您必须使用相应的API函数再次粘贴正确types的内容。

我没有像我期望的那样快速取得进展,而且时间不够,所以我放弃了这个想法。 如果你想给它一个机会,查找MSDN的API调用(我现在没有在这里,否则我会马上给你)。

编辑:我find了代码。 下面的所有代码应该保存在一个模块中:

 ' 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 lFormatID As Long) As String '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(0&)) Then ' 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