Board logo

標題: 想請問如何透過VBA將EXCEL表中的內容,轉成圖片並寄出 [打印本頁]

作者: peiruu    時間: 2012-2-8 15:36     標題: 想請問如何透過VBA將EXCEL表中的內容,轉成圖片並寄出

想請問如何透過VBA將EXCEL表中的內容,轉成圖片,並寄出。
目前在網路上找到比較可行的方法,似乎是將body轉為html的方式
http://www.rondebruin.nl/mail/folder2/mail4.htm

但是之後想依照上面的方式,先把原本的range copy成圖片,輸入到暫存的sheet1中,
但是要把這個內容轉成html的時候似乎發生問題? 這邊無法繼續運作。
請好心高手可以幫忙,謝謝!!!!


Private Sub CommandButton1_Click()

    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set rng = Nothing
    Set rng = Sheets("Sent Mail").Range("A1:I240")
    '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 protected]"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .HTMLBody = RangetoHTML(rng)
        .Display
        '.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-2010
    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)

    rng.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    TempWB.Sheets(1).Paste

    'Publish the sheet to a htm file

    With TempWB.PublishObjects.Add( _

         SourceType:=xlSourcePrintArea, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).Range("A1:I250"), _
         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




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