>我有下面的代码,它适用于从 A 到 F 的连续列范围。
Private Sub CommandButton1_Click()
Dim Rng As Range
Dim WorkRng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim StrBody As String
StrBody = "Find the table below"
Set Rng = Nothing
On Error Resume Next
Set Rng = Sheets("Sheet1").Range("A01:F10").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
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = ""
.cc = ""
.BCC = ""
.Subject = "Test"
.HTMLBody = StrBody & RangetoHTML(Rng)
.Display
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
但是,我现在想从范围中排除 D 列,因此新范围将是 A01:C10 和 E01:F10。这是我的 RangetoHTML 函数,
Function RangetoHTML(Rng As Range)
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
'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 值,如下所示,但如果我在 Outlook 邮箱中进行此更改,则不会出现任何内容,
Set Rng = Sheets("Sheet1").Range("A01:C10,E01:F10").SpecialCells(xlCellTypeVisible)
请帮帮我!
尝试在Set = Rng
之前隐藏.Columns("D:D")
例
With ThisWorkbook.Sheets("Sheet1") .Columns("D:D").EntireColumn.Hidden = True End With Set Rng = Sheets("Sheet1").Range("A01:F10").SpecialCells(xlCellTypeVisible)
或者这个
With ThisWorkbook.Sheets("Sheet1") .Columns("D:D").EntireColumn.Hidden = True Set Rng = .Range("A01:F10").SpecialCells(xlCellTypeVisible) .Columns("D:D").EntireColumn.Hidden = False End With
看起来您在关闭工作簿之前没有保存工作簿; 因此,在复制之前删除不需要的范围可能是最简单的方法。
Columns("D:D").Select
Selection.Delete Shift:=xlToLeft