如何将剪贴板内容粘贴到字表

时间:2015-12-02 16:54:55

标签: vba ms-word word-vba

作为从网络中提取数据的一部分,我使用以下(有问题的)代码。

Sub USPTOAbstHTML1()
Application.ScreenUpdating = False
Dim Rng As Range, Tbl As table, StrTxt As String, HttpReq As Object, i As Long, oHtml As MSHTML.HTMLDocument, IE As SHDocVw.InternetExplorer
Set HttpReq = CreateObject("Microsoft.XMLHTTP")
Set oHtml = New HTMLDocument
Set IE = CreateObject("InternetExplorer.Application")
With ActiveDocument.Range
    For Each Tbl In .Tables
        With Tbl
            For i = 1 To .Rows.Count
                With .Cell(i, 2).Range
                            If .Hyperlinks.Count > 0 Then
                            MsgBox .Hyperlinks(1).Address
                                HttpReq.Open "GET", .Hyperlinks(1).Address, False
                                HttpReq.send
                                oHtml.body.innerHTML = HttpReq.responseText
                                MsgBox HttpReq.responseText
                                StrTxt = oHtml.getElementsByClassName("claim").Item.innerHTML
                                With IE
                                    .Visible = False
                                    .navigate "about:blank"
                                    .Document.body.innerHTML = StrTxt
                                    .Document.execCommand "SelectAll"
                                    .Document.execCommand "Copy"
                                End With
                                With Tbl.Cell(i, 5).Range
                                   Selection.PasteAndFormat (wdPasteDefault)
                                End With
                            End If
                            .Collapse wdCollapseEnd
                            .Find.Execute
                End With
            Next
        End With
    Next
End With
Set HttpReq = Nothing
Application.ScreenUpdating = True
End Sub

上面代码中的问题是,我找不到任何方法可以将剪贴板内容添加到特定位置,即Tbl.Cell(i,5).Range 代码在选择的任何地方插入数据。

我尝试了MSForms.DataObject,但我只能找到只包含文本的示例,而我的剪贴板内容不仅仅是文本。 (带图像的格式化文本)

还有其他方法可以完成工作吗?

1 个答案:

答案 0 :(得分:0)

Range对象还有Paste,PasteFormat和类似的方法。您可能首先需要折叠范围,使其位于单元格内(而不是包含单元格)。例如:

Set rng = Tbl.Cell(i, 5).Range
rng.Collapse wdCollapseStart
rng.PasteAndFormat wdPasteDefault