如何从excel中复制未隐藏的行,并通过outlook复制邮件

时间:2013-09-13 13:40:34

标签: excel-vba vba excel

我不是VBA Guru,但知道一点。我被困在一个区域。让我来描述下面的情景:

我想通过邮件使用Excel向对方发送一些详细信息。我不需要的一些数据,我使用了一些实际隐藏行的VBA代码。现在每当我使用我的代码复制整个页面(作为HTML格式)并使用outlook发送时,它实际上也会采用我不想发送的隐藏行。

**隐藏行的代码为空白**

Sub Example()
    Dim cel As Range, rng As Range
    Set rng = Range("E8:E31")
    For Each cel In rng
       If cel.Value = "" Then
           cel.EntireRow.Hidden = True
       End If
    Next cel 
End Sub

**发送邮件的代码**

Function RangetoHTML(rng As Range)
'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)
    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

    '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

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

未经测试,但请尝试更改:

rng.Copy

rng.SpecialCells(xlCellTypeVisible).Copy

这只会复制非隐藏的行。