【问题标题】:Read the full string in an Excel sheet读取 Excel 工作表中的完整字符串
【发布时间】: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 个字符。

    标签: excel vba ms-word


    【解决方案1】:

    我认为这是您的 Word 数据源“电子邮件正文”中的值,我在您的代码中没有看到对 Excel 的引用。您的数据源来自哪里,您是否首先从 Excel 导入日期?

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    • 这是一个邮件合并,所以在这种情况下,DataSource 是一个 Excel 文件。代码masterDoc.MailMerge.DataSource.DataFields("EmailBody").Value 从“EmailBody”列中检索 Excel 文件中的值,但它只检索前 255 个字符。
    猜你喜欢
    • 2015-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-16
    相关资源
    最近更新 更多