简体   繁体   English

如何使用Excel VBA在InnerText中复制或插入多个范围数据

[英]How to copy or insert multiple range data in InnerText using Excel VBA

I am trying to send email using VBA through GMAIL by using internet explorer. 我正在尝试使用Internet Explorer通过VMA通过VMA发送电子邮件。 I have data in Range("A1:AL4") and i want to put this data in gmail's compose body. 我在Range(“ A1:AL4”)中有数据,我想将此数据放入gmail的撰写正文中。 if I try with single range like on A1 my code work but it does not work with multiple cells. 如果我尝试在A1上使用单个范围,则我的代码可以工作,但不适用于多个单元格。

'Insert Email content
IE.document.querySelector("div[class*= LW-avf]").innerText = Sheets("Sheet1").Range("A1:AL4").Value

I want the whole data from A1 to AL4 in the email body. 我想要电子邮件正文中从A1到AL4的全部数据。

You could try something like this: 您可以尝试这样的事情:

Dim cell As Range
Dim sht As Worksheet
Dim emailBody As String
emailBody = ""
Set sht = ThisWorkbook.Worksheets("Sheet1")
For Each cell In sht.Range("A1:AL4").cells
    emailBody = emailBody & cell.Value
Next cell

So basically you concatenate all strings in your range and form a single big string which you can then assign to your email's body. 因此,基本上,您可以连接范围内的所有字符串,并形成一个大字符串,然后可以将其分配给电子邮件的正文。

Of course, if you need spaces or linebreaks between the contents of each cell you can do that: 当然,如果在每个单元格的内容之间需要空格或换行符,则可以执行以下操作:

emailBody = emailBody & " " & cell.Value

or 要么

emailBody = emailBody & vbNewLine & cell.Value

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

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