Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从excel中获取单元格并将其插入outlook电子邮件?_Excel_Vba - Fatal编程技术网

如何从excel中获取单元格并将其插入outlook电子邮件?

如何从excel中获取单元格并将其插入outlook电子邮件?,excel,vba,Excel,Vba,我使用VBA 7和Excel 2010 我正在尝试创建一个按钮,该按钮将自动从我的电子表格中获取单元格C4:J15,并将其插入电子邮件中。但无论我为xmailbody列出了什么组合(参见代码),电子邮件都是空白的。如何获取值?格式也可以复制,但这不是绝对必要的 Private Sub CommandButton1_Click() Dim xOutApp As Object Dim xOutMail As Object Dim xMailBody As String On Error Resume

我使用VBA 7和Excel 2010

我正在尝试创建一个按钮,该按钮将自动从我的电子表格中获取单元格C4:J15,并将其插入电子邮件中。但无论我为xmailbody列出了什么组合(参见代码),电子邮件都是空白的。如何获取值?格式也可以复制,但这不是绝对必要的

Private Sub CommandButton1_Click()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
On Error Resume Next
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = ActiveSheet.Range(F7).Value
          On Error Resume Next
With xOutMail
    .To = "test@test.com"
    .CC = ""
    .BCC = ""
    .Subject = "Schedule Request"
    .Body = xMailBody
    .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
MsgBox ("Your request has been submitted to your supervisor.  Please contact your supervisor if you do not receive a reply in the same business day")
End Sub

我希望在excel电子表格中有一个按钮,可以将值插入outlook电子邮件并自动发送。这将用于员工每周通过电子邮件发送预定时间。

Ron de Bruin在其网站上用VBA编写了非常有用的代码,并帮助我更好地理解与Outlook的交互。例如,这一个(只是一个复制粘贴,为获取单元格F7而修改)可以完成这项工作。不要忘记将字段更改为、抄送、主题等:

Sub Mail_Selection_Range_Outlook_Body()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2016
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object

    Set rng = Nothing
    On Error Resume Next
    Set rng = Range("F7")
    On Error GoTo 0

    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .HTMLBody = RangetoHTML(rng)
        .Send   'or use .Display
    End With
    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub


Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With

    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

    'Close TempWB
    TempWB.Close savechanges:=False

    'Delete the htm file we used in this function
    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = No
End function

xBody
是一个二维单元格值数组-您需要循环该数组,并将其格式化为可分配给
Body
属性的字符串。您不能直接传递数组。感谢您在本周一早上这么早发表评论。你能帮我更多吗?这是我第一次使用VB,所以我有点笨。你能给我一个可能的代码示例,或者把我转到一个关于如何生成循环的页面吗?别忘了结尾的“End Function”,我刚刚在一分钟前编辑了它,谢谢你的帮助