在Excel表中阅读完整的字符串
我们在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
是否有解决方法?
这是一个邮件,因此在这种情况下的数据源是一个Excel文件。行MasterDoc.mailmerge.datasource.datafields(“ emailbody”)。值
从列“ emailbody”中检索excel文件中的值,但它仅检索前255个字符。
We have a macro written in VBA within Word that reads data from an Excel file. The HTML body of an email is stored in a field within Excel.
The field is not completely read and the text is shrunk.
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
In the example above the text field in Excel is read until "Thank you" the rest of the text is not available in the variable 'HTMLBody'. What can be done to get the full length of the string here?
It seems this line is not retrieving the full string:
masterDoc.MailMerge.DataSource.DataFields("EmailBody").Value
Is there a workaround?
It's a mailmerge so the DataSource in this case is an Excel file. The line masterDoc.MailMerge.DataSource.DataFields("EmailBody").Value
retrieves the value in the Excel file from column "EmailBody" but it only retrieves the first 255 characters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是您单词dataSourc“ emailbody”中的价值,我认为您的代码中没有对Excel的引用。您的数据源来自哪里,您是否首先从Excel导入日期?
I think this is the value in your Word Datasourc "EmailBody" i see no reference to Excel in your code. Where does your Datasource come from, did you first import the date from Excel?