【发布时间】:2022-10-02 09:06:40
【问题描述】:
我们在 Word 中有一个用 VBA 编写的宏,它从 Excel 文件中读取数据。电子邮件的 HTML 正文存储在 Excel 中的一个字段中。
该字段未完全读取,文本已缩小。
Set masterDoc = ActiveDocument \' Identify the ActiveDocument (foremost doc when Macro run) as \"masterDoc\"
masterDoc.MailMerge.DataSource.ActiveRecord = wdLastRecord \' jump to the last active record (active = ticked in edit recipients)
lastRecordNum = masterDoc.MailMerge.DataSource.ActiveRecord \' retrieve the record number of the last active record so we know when to stop
masterDoc.MailMerge.DataSource.ActiveRecord = wdFirstRecord \' jump to the first active record (active = ticked in edit recipients)
Do While lastRecordNum > 0 \' create a loop, lastRecordNum is used to end the loop by setting to zero (see below)
masterDoc.MailMerge.Destination = wdSendToNewDocument \' Identify that we are creating a word docx (and no e.g. an email)
masterDoc.MailMerge.DataSource.FirstRecord = masterDoc.MailMerge.DataSource.ActiveRecord \' Limit the selection to just one document by setting the start ...
masterDoc.MailMerge.DataSource.LastRecord = masterDoc.MailMerge.DataSource.ActiveRecord \' ... and end points to the active record
masterDoc.MailMerge.Execute False \' run the MailMerge based on the above settings (i.e. for one record)
Set singleDoc = ActiveDocument \' Identify the ActiveDocument (foremost doc after running the MailMerge) as \"singleDoc\"
\'region Send email
Set oItem = oOutlookApp.CreateItem(olMailItem)
Dim pdfFilePath As String
pdfFilePath = masterDoc.MailMerge.DataSource.DataFields(\"PdfFolderPath\").Value & Application.PathSeparator & _
masterDoc.MailMerge.DataSource.DataFields(\"PdfFileName\").Value & \".pdf\"
Dim HTMLBody As String
HTMLBody = masterDoc.MailMerge.DataSource.DataFields(\"EmailBody\").Value
With oItem
.To = masterDoc.MailMerge.DataSource.DataFields(\"EmailAddress\").Value
.Subject = \"Test\"
.HTMLBody = HTMLBody
\'.HTMLBody = \"Hello, this is me sending an email to you. We need to know some things. The things we need to know are if we\'re able to send out emails to anyone anyplace at anytime. Also, why are strings cut of at a specific length? Can you help me with this? Thank you, the wolf of Wall Street\"
.Attachments.Add Source:=pdfFilePath, Type:=olByValue
.Send
End With
\'endregion
singleDoc.Close False \' Close \"singleDoc\", the variable \"singleDoc\" can now be used for the next record when created
If masterDoc.MailMerge.DataSource.ActiveRecord >= lastRecordNum Then \' test if we have just created a document for the last record
lastRecordNum = 0 \' if so we set lastRecordNum to zero to indicate that the loop should end
Else
masterDoc.MailMerge.DataSource.ActiveRecord = wdNextRecord \' otherwise go to the next active record
End If
Loop \' loop back to the Do start
End Sub \' Mark the end of the Subroutine
在上面的示例中,Excel 中的文本字段被读取,直到“谢谢”,其余文本在变量“HTMLBody”中不可用。在这里可以做些什么来获得字符串的全长?
这条线似乎没有检索完整的字符串:
masterDoc.MailMerge.DataSource.DataFields(\"EmailBody\").Value
有解决方法吗?
它是一个邮件合并,所以在这种情况下 DataSource 是一个 Excel 文件。 masterDoc.MailMerge.DataSource.DataFields(\"EmailBody\").Value 行从列 \"EmailBody\" 检索 Excel 文件中的值,但它只检索前 255 个字符。