Board logo

標題: [發問] outlook方法改為系統預設的郵件程式 [打印本頁]

作者: takeshilin88    時間: 2016-3-17 11:52     標題: outlook方法改為系統預設的郵件程式

請教各位大大:
原本使用outlook方法寄信,
因老闆要求要改成系統預設的郵件程式寄信(windows live mail),
請問要如何修改?
  1. Sub Send_Range_Body()

  2.     Dim rng As Range
  3.     Dim OutApp As Object
  4.     Dim OutMail As Object
  5.     Application.ReferenceStyle = xlA1

  6.     'Set rng = Nothing
  7.     On Error Resume Next
  8.     Set rng = Sheets("sheet1").Range("A4:F20").SpecialCells(xlCellTypeVisible)
  9.     On Error GoTo 0

  10.     If rng Is Nothing Then
  11.         MsgBox "請選擇一個選區並且工作表不能是保護狀態。" & _
  12.                vbNewLine & "請再次嘗試。", vbOKOnly
  13.         Exit Sub
  14.     End If

  15.     With Application
  16.         .EnableEvents = False
  17.         .ScreenUpdating = False
  18.     End With

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

  21.     On Error Resume Next
  22.     With OutMail
  23.         .To = "[email protected]"
  24.         '.CC = "副本給誰"  
  25.         '.BCC = "密件副本給誰"   
  26.         .Subject = "hello"  '主旨
  27.         .HTMLBody = RangetoHTML(rng)  
  28.         .Display
  29.         
  30.     End With
  31.     On Error GoTo 0

  32.     With Application
  33.         .EnableEvents = True
  34.         .ScreenUpdating = True
  35.     End With

  36.     Set OutMail = Nothing
  37.     Set OutApp = Nothing
  38. End Sub

  39. 'FUNCTION RangetoHTML
  40. Function RangetoHTML(rng As Range)
  41. ' Changed by Ron de Bruin 28-Oct-2006
  42. ' Working in Office 2000-2016
  43.     Dim fso As Object
  44.     Dim ts As Object
  45.     Dim TempFile As String
  46.     Dim TempWB As Workbook

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

  48.     'Copy the range and create a new workbook to past the data in
  49.     rng.Copy
  50.     Set TempWB = Workbooks.Add(1)
  51.     With TempWB.Sheets(1)
  52.         .Cells(1).PasteSpecial Paste:=8
  53.         .Cells(1).PasteSpecial xlPasteValues, , False, False
  54.         .Cells(1).PasteSpecial xlPasteFormats, , False, False
  55.         .Cells(1).Select
  56.         Application.CutCopyMode = False
  57.         On Error Resume Next
  58.         .DrawingObjects.Visible = True
  59.         .DrawingObjects.Delete
  60.         On Error GoTo 0
  61.     End With

  62.     'Publish the sheet to a htm file
  63.     With TempWB.PublishObjects.Add( _
  64.          SourceType:=xlSourceRange, _
  65.          Filename:=TempFile, _
  66.          Sheet:=TempWB.Sheets(1).Name, _
  67.          Source:=TempWB.Sheets(1).UsedRange.Address, _
  68.          HtmlType:=xlHtmlStatic)
  69.         .Publish (True)
  70.     End With

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

  78.     'Close TempWB
  79.     TempWB.Close savechanges:=False

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

  82.     Set ts = Nothing
  83.     Set fso = Nothing
  84.     Set TempWB = Nothing
  85. End Function
複製代碼





歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)