无法运行我的代码在其他机器上发送电子邮件

时间:2017-11-23 01:42:25

标签: vba excel-vba outlook excel

我正在帮助我的朋友使用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 个答案:

答案 0 :(得分:1)

而不是

wdDoc.Range(1, 1).Paste

尝试

wdDoc.Range.Paste

答案 1 :(得分:0)

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

wdDoc.Application.Selection.PasteAndFormat wdFormatOriginalFormatting

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