2016-12-16 10 views
0

このコードの機能は、特定の範囲のセル、各セル(1ML-234-1Rのようなコードを持つ)をコピーしてOutlookメールの本文に配置することです(Ron de Bruin Excelオートメーションコード)。私は、各セルの前後にある体の中に現れるスペースを削除し、各セルの値をコンマ(、)で区切りたい。トリムコマンドを使用しましたが、運がありませんでした。Outlookのメール本文にキルスペース

Sub Mail_Selection_Range_Outlook_Body() 

    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("Sheet1").Range("D1: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 = "[email protected]" 
     .CC = "" 
     .BCC = "" 
     .Subject = "Load Shed " 
     .HTMLBody = Trim(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 

    Set ts = Nothing 
    Set fso = Nothing 
    Set TempWB = Nothing 
End Function 

enter image description here

使用しているRangeToHTML機能がポートに与えられた範囲を設計し、それをHTML形式で構造を既存のです維持され、それがこの

enter image description here

+0

どのスペース?イメージはより明確になります。ところで、あなたはあなたのhtmlの中で、整列、 'align = center'について話していますか? – cyboashu

+0

@cyboashuさんがあなたの参考に写真を追加しました。 – werdakloi

答えて

1

のようになりたいです。あなたは単なる文字列で区切られた範囲内の値のリストが必要な場合は、のようにはるかに簡単機能を使用することができます。

Function fnConcatRange(rng As Range, Optional delim As String) As String 

For i = 1 To rng.Count 
    fnConcatRange = fnConcatRange & rng(i) 
    If i < rng.Count Then fnConcatRange = fnConcatRange & delim 
Next 

End Function 

そして、あなたがすることもでき

.HTMLBody = fnConcatRange(rng, ",") 

のようにあなたの電子メールに入れ特定の方法で電子メールの本文を書式設定する場合は、任意のHTMLタグを文字列として追加します。

これよりも徹底した連結機能があります(ここにあるもの:Concatenate multiple ranges using vba)が、必要な処理を行う必要があります。

関連する問題