簡體   English   中英

VBA-將工作表中的單元格復制到Outlook電子郵件中

[英]VBA - Copy Cell from Sheet into Outlook email

我是VBA的新手,並創建了以下發送電子郵件的代碼。 我的問題是如何從我當前使用的Excel工作表中復制一些要粘貼到Excel文件中的單元格?

謝謝,

 Sub CIR_Save_Email()

 Dim objoutlook As Object
 Set objoutlook = CreateObject("outlook.application")

 Dim objemail As Object
 Set objemail = objoutlook.createitem(olmailitem)
 Const olFormatHTML As Long = 2

 emailbodymessage = "<HTML><BODY>Hi Team," & _
 "<br><br>Attached is the Display's CIR for today<br><br>" & _
 "<b>Brief overview of CIR</b><br><br>" & _
 "<b>Purpose:</b> To get a snapshot of what your current inventory levels by SKU are every day." & _
 "<ul style=""list-style-type:circle"">" & _
   "<li><b>Unrestricted QTY</b> The total inventory at that DC (i.e.Deliveries Created + Available Qty)</li>" & _
  "<li><b>Deliveries Created:</b> Orders that are being processing at that DC (i.e. they will NOT be included in Available Inventory)</li>" & _
   "<li><b>Available:</b> How many cases are available to use at that DC </li>" & _
   "<li><b>Avail DOS:</b> How many DOS the available cases equate to</li>" & _
 "<li><b>IT QTY:</b> How man cases are in transit</li>" & _
 "<li><b>Avail +IT DOS:</b> How many DOS the available cases equate to</li>" & _
 "</ul> </body> </html>"

 emailbodymessage2 = "<html><body><ul style=""list-style-type:circle"">" & _
 "<li><b>Future Available:</b> The total DOS of cases Avail + IT</li>" & _
 "<li><b>QI QTY:</b> How many cases are on Qualitiy (ie Non-Conformance)</li>" & _
 "<li><b>Blocked QTY:</b> How many cases are blocked from ordering due to damages, short dating, expired, etc." & _
"<li><b>CM- months:</b> The forecasts of the months past (CM-1=July)</li>" & _
 "<li><b>% to Fcst:</b> How much of your projected forecast has shipped this month</li>" & _
 "<li><b>Current SNAP Fcst:</b> This month's projected forecast</li>" & _
"<li><b>CM+ months:</b> The forecasts of the months moving forward (CM+1= September)</li>" & _
 "</ul> </body></html>"

 With objemail
     .To = emaillist
       .cc = ""
     .Subject = "Display's CIR " & Date
     .BodyFormat = olFormatHTML '// 2
 .HTMLBody = emailbodymessage & emailbodymessage2 

 .display

 End With

 End Sub

您可以使用以下功能(內部使用將范圍導出到HTML)將excel范圍轉換為html。 然后,應將結果HTML包含在生成的HTML正文中。

該函數將Range導出到HTML臨時創建的文件中,然后將內容僅剝離div(不包含周圍的HTML標簽)。

但是,我不確定格式和其他詳細信息是否符合您的要求。 另一種解決方案是從單元格手動構造HTML,但這是很多工作。

用法: str = GetHtml("Sheet1","D4:E6")

Public Function GetHtml(ByVal sheetName As String, ByVal rangeName As String) As String
Dim fso As FileSystemObject
Dim fileName As String
Dim txtStream As TextStream
Dim html As String
Dim line As String
Dim readLines As Boolean

Set fso = New FileSystemObject

 Dim rng As range
 fileName = fso.GetSpecialFolder(2) & "\" & Replace(fso.GetTempName, ".tmp", ".html")
 If fso.FileExists(fileName) Then
    fso.DeleteFile fileName
 End If

 Set rng = Sheets(sheetName).range(rangeName)

 ActiveWorkbook.PublishObjects.Add(SourceType:=xlSourceRange, fileName:=fileName, Sheet:=rng.Worksheet.Name, Source:=rng.Address, HtmlType:=xlHtmlStatic).Publish
 Set txtStream = fso.OpenTextFile(fileName, ForReading, False)

 readLines = False
 html = ""
 Do While Not txtStream.AtEndOfStream
    line = txtStream.ReadLine
    If InStr(line, "<!--START OF OUTPUT FROM EXCEL PUBLISH AS WEB PAGE WIZARD") > 0 Then
        readLines = True
    End If
    If readLines Then
        html = html & vbCrLf & line
    End If
    If readLines And InStr(line, "<!--END OF OUTPUT FROM EXCEL PUBLISH AS WEB PAGE WIZARD") > 0 Then
        readLines = False
    End If
 Loop
 txtStream.Close
 Set txtStream = Nothing

 If fso.FileExists(fileName) Then
    fso.DeleteFile fileName
 End If
 Set fso = Nothing

 GetHtml = html

End Function

您說過要“從我當前使用的excel工作表中復制一些要粘貼到excel文件中的單元格”。 我認為您是從Excel復制並粘貼到電子郵件正文中的,對。

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
    'Only the visible cells in the selection
    Set rng = Selection.SpecialCells(xlCellTypeVisible)
    'You can also use a fixed range if you want
    'Set rng = Sheets("YourSheet").Range("D4:D12").SpecialCells(xlCellTypeVisible)
    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 = "ron@debruin.nl"
        .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 = Nothing
End Function

https://www.rondebruin.nl/win/s1/outlook/bmail2.htm

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM