简体   繁体   English

如何将数据从Excel复制/粘贴到网页(VBA)?

[英]How to copy/paste data from excel to a webpage (VBA)?

So I'm trying to copy/paste data from excel into a webpage text box using VBA. 因此,我正在尝试使用VBA将数据从excel复制/粘贴到网页文本框中。 However, my problem is that if I have, for example 3 or 4 rows of data copied, when pasting the values into the webpage using vba, only 1 row will be copied rather than all the rows. 但是,我的问题是,例如,如果我复制了3或4行数据,则在使用vba将值粘贴到网页中时,将仅复制1行,而不是所有行。

Here is my code: 这是我的代码:

.Document.getElementsByTagName("textarea")(0).Value = ActiveCell.Value

Any ideas? 有任何想法吗? If I take out the (0) I get an error: 如果我取出(0),则会出现错误:

object doesn't support this property or method. 对象不支持此属性或方法。

This is some code of mine that works: 这是我的一些有效的代码:

Sub FillOutInvoice(Account As Account)

    Dim ie As InternetExplorer
    Dim elem As HTMLLinkElement

    Set ie = New InternetExplorer
    ie.Visible = True
    ie.navigate Settings.ErpAddress

    Do While ie.Busy Or ie.readyState <> 4
        DoEvents
    Loop

    With ie.document
        .getElementsByClassName(Settings.InputClass)(0).innerText = Account.InvoiceNumber
        .getElementsByClassName(Settings.InputClass)(1).innerText = Day(Account.InvoiceDate)
    End With

    Do While ie.Busy Or ie.readyState <> 4
        DoEvents
    Loop

    Application.Wait Now + #12:00:02 AM#
    ie.Quit

End Sub

As you see, the needed property is .InnerText and not .Value . 如您所见,所需的属性是.InnerText而不是.Value

Here is an example of sending a range of rows text to a textarea element using the clipboard 这是使用剪贴板将一定范围的行文本发送到textarea元素的示例

Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls

Public Sub InsertData()
    Dim ie As New InternetExplorer
    With ie
        .Visible = True
        .Navigate2 "https://www.google.com/search?q=google+translate&rlz=1C1GCEB_enGB815GB815&oq=google+tran&aqs=chrome.0.0j69i57j0l4.2057j0j7&sourceid=chrome&ie=UTF-8"

        While .Busy Or .readyState < 4: DoEvents: Wend

        Dim clipboard As Object
        Set clipboard = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")

        ActiveSheet.Range("A1:A3").Copy

        With clipboard
            .GetFromClipboard
            ie.document.getElementsByTagName("textarea")(1).innerText = .GetText
        End With
        Application.CutCopyMode = False
        Stop
        .Quit
    End With
End Sub

Contents of Range("A1:A3") 范围的内容(“ A1:A3”)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM