0

Excel で範囲をコピーし、Outlook 電子メールの本文に貼り付ける VBA コードがあります。このコードは、私の同僚のコンピューターのいくつかでは機能しますが、私のものでは機能しません。コードは .temp ファイルを作成し、To:、CC:、および Subject を設定しますが、本文には何も表示されません。HTMLを貼り付けようとしています。設定とかそういうのかなと思っているのですが、どこから手を付けていいのかわかりません。これに関する任意の助けをいただければ幸いです。

Sub CreateEmail()

   Dim OutApp As Object
   Dim OutMail As Object

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

    sbj = "***"
    toStr = "****"
    Ccstr = "*****"

   Dim rng As Range
   Set rng = Nothing
   Set rng = Range("Flash")

On Error Resume Next
With OutMail
    .To = toStr
    .Display
    .CC = Ccstr
    .BCC = ""
    .Subject = sbj & datestr
    .HTMLBody = RangetoHTML(rng)


End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub

Function RangetoHTML(rng As Range)
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

Application.ScreenUpdating = False

TempFile = Environ$("temp") & "/" & "temp" & ".htm"

    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

    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

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=")

TempWB.Close savechanges:=False

Kill TempFile

Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing

Application.ScreenUpdating = True

終了機能

4

1 に答える 1

0

代わりにこの方法を使用することにしました。これはExcelの範囲を貼り付けますが、HTMLバージョンは貼り付けません。HTML を使用する必要はありませんでした。これも同様に機能しますが、色などはありません。

Sub SendFlash()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object

Set rng = Nothing
On Error Resume Next
Set rng = Range("K4:L8").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

toStr = "*"
Ccstr = "*"
sbj = "Tran"

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

On Error Resume Next
With OutMail
    .To = toStr
    .CC = Ccstr
    .BCC = ""
    .Subject = sbj & Date
    .Body = TableRangeToTabDelim(rng)
    .display
End With
On Error GoTo 0

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

Set OutMail = Nothing
Set OutApp = Nothing
End Sub

Function TableRangeToTabDelim(TableRange As Range) As String
Dim TableCols As Integer
Dim TableRows As Integer
Dim ReturnString As String

TableCols = TableRange.Columns.Count
TableRows = TableRange.Rows.Count

i = 1
j = 1

Do While i <= TableRows
    Do While j <= TableCols
        ReturnString = ReturnString & TableRange.Cells(i, j).Value
        If j <> TableCols Then
            ReturnString = ReturnString & Chr(9)
        End If
        j = j + 1
    Loop
    j = 1
    If i <> TableRows Then
        ReturnString = ReturnString & Chr(10)
    End If
    i = i + 1
Loop

TableRangeToTabDelim = ReturnString
End Function
于 2012-11-20T17:11:15.093 に答える