簡體   English   中英

如何僅從excel復制未隱藏的行並通過Outlook發送郵件

[英]how to copy only unhidden rows from an excel and mail via outlook

我不是VBA大師,但知道一點。 我被困在1個區域。 讓我描述以下情況:

我想使用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

請幫我。

未經測試 ,但嘗試更改:

rng.Copy

rng.SpecialCells(xlCellTypeVisible).Copy

這將僅復制未隱藏的行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM