【问题标题】:Cannot run my codes for sending email in other machine无法运行我的代码以在其他机器上发送电子邮件
【发布时间】:2017-11-23 01:42:25
【问题描述】:

我正在帮助我的朋友使用 VBA 开发她的代码。我已经在我的笔记本电脑上成功运行了这些代码,但是当我们将代码复制到她的机器时,她遇到了错误。

这是我的代码:

Sub Test()

    Call sendingEmailWithChecklist("Book1.xlsm")

End Sub

Sub sendingEmailWithChecklist(workbookName As String)

    Dim recipient As String
    Dim cc As String
    Dim subject As String
    Dim body As Range
    Dim greetings As String
    Dim message As String
    Dim signature As String
    Dim ebody As String

    Dim olApp As Outlook.Application
    Dim olInsp As Outlook.Inspector
    Dim wdDoc As Word.Document
    Dim olEmail As Outlook.MailItem
    Dim worksheetName As String
    Dim content As Range

    Set olApp = New Outlook.Application
    Set olEmail = olApp.CreateItem(olMailItem)

    Sheet2.Activate

    recipient = Range("B3").Value
    cc = Range("B4").Value
    subject = Range("B5").Value

    greetings = Range("B6").Value
    message = Range("B7").Value

    ebody = greetings & vbNewLine & vbNewLine & message & vbNewLine

    signature = Range("B8").Value

    'Workbooks(workbookName).Activate
    worksheetName = "Sheet1"


    With olEmail
        .Display

        .To = recipient
        .cc = cc
        .subject = subject

        Set olInsp = .GetInspector
        Set wdDoc = olInsp.WordEditor

        Workbooks(workbookName).Worksheets(worksheetName).Activate

        Workbooks(workbookName).Worksheets(worksheetName).Cells.Copy

        'Range("A1:F17").Select
        'Selection.Copy

    End With

    With olEmail

        .Display
        wdDoc.Range(1, 1).Paste
        wdDoc.Range.InsertBefore ebody

        '.Send


    End With

End Sub

wdDoc.Range(1,1).Paste 是她的错误。我们都从工具中声明了相同的引用,但错误仍然在这一行。为什么它不能在她的机器上运行可能是什么错误?

PS。她不想使用HTMLbody

【问题讨论】:

  • 你为什么有2个With olEmail
  • 我已经用 olEmail 删除了 1 个,但仍然没有解决我们的错误。
  • 作为@tlemaster 的stated,将其更改为wdDoc.Range.Paste 会使您的代码在我的最后工作。它还会在复制的 Range 之前粘贴 ebodyIt didn't give us the outcome that we want 。请具体说明您遇到的问题和错误,以便我们为您提供帮助。

标签: vba excel outlook


【解决方案1】:

代替

wdDoc.Range(1, 1).Paste

试试

wdDoc.Range.Paste

【讨论】:

  • 我的 ebody 中的文本已粘贴到我范围的第一个单元格中。它没有给我们想要的结果
【解决方案2】:

如果您想进一步控制在邮件正文中粘贴数据的方式,您可能需要使用 Word Selection 对象(表达式)而不是 Range。比如:

wdDoc.Application.Selection.PasteAndFormat wdFormatOriginalFormatting

以上粘贴复制项目的原始格式。
您可以根据预期结果选择其他PasteAndFormat选项。

【讨论】:

  • @RouellaMayGabineteAmponin 什么错误和什么行?
  • 在那条线上。 wdDoc.Application.Selection.PasteAndFormat wdFormatOriginalFormatting
  • @RouellaMayGabineteAmponin 错误信息是什么?你确定你有正确的参考集吗?
猜你喜欢
  • 2020-06-23
  • 2014-08-02
  • 1970-01-01
  • 1970-01-01
  • 2021-07-22
  • 2020-03-09
  • 1970-01-01
  • 2014-05-13
  • 1970-01-01
相关资源
最近更新 更多