[英]Copy multiple columns, keeping formatting, from Excel to Outlook mail
[英]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.