简体   繁体   English

在没有单元格的情况下从Excel发送Outlook邮件

[英]Sending outlook mails from excel without cells

I the below VBA code which generates a mail from different cells in an excel spreadsheet. 下面的VBA代码从excel电子表格中的不同单元格生成邮件。 The problem is that when I use it to generate an email, the mails will not fit to automatically to the screen like a normal outlook mail does. 问题是,当我使用它生成电子邮件时,这些邮件将无法像普通Outlook邮件那样自动适应屏幕。 So if I read it on a smartphone screen the text won't fit to the screen. 因此,如果我在智能手机屏幕上阅读该文本,则该文本将无法显示在屏幕上。

There's a macro that sends the mail and a function that selects the range. 有一个用于发送邮件的宏和一个用于选择范围的函数。

Sub Mail_Sheet_Outlook_Body()

Dim rng As Range
Dim StrBody As String
Dim OutApp As Object
Dim OutMail As Object

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With
'add this for the text string
'StrBody = Sheets("Sheet2").Range("A1").Value & "<br>" & _
 '         Sheets("Sheet2").Range("A2").Value & "<br>" & _
  '        Sheets("Sheet2").Range("A3").Value & "<br><br><br>"

Set rng = Nothing
Set rng = ActiveSheet.UsedRange
'You can also use a sheet name
'Set rng = Sheets("YourSheet").UsedRange

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

On Error Resume Next
With OutMail
    .to = "...." ' email adress here


' Worksheets("Sheet2").Range("A1:A18")
    .cc = ""
    .BCC = ""
    .Subject = "The short update"
    '.HTMLBody = StrBody & RangetoHTML(rng)
    .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)
 ' Working in Office 2000-2013
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
For Each Row In rng
For Each Column In Row
StrBody = StrBody & " " & Column
Next
StrBody = StrBody & "<br>"
Next

You probably want something along the lines of the above code. 您可能需要上述代码中的某些内容。
Even manually entered tables don't autofit. 即使是手动输入的表格也无法自动调整。
If you're really trying to keep the format you might consider exporting the range to a shape and converting it to a picture. 如果您确实想保留格式,则可以考虑将范围导出为形状并将其转换为图片。 Good luck with that. 祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM