【发布时间】:2014-04-17 14:31:16
【问题描述】:
我尝试从 excel 复制数据并自动通过电子邮件发送。一切正常,除了 CF。 '
我应用了下面的代码,但它也处理了条件格式规则,因为格式在它粘贴到 Outlook 时会发生变化。请帮助我们解决此问题。
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 paste 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
结束函数
【问题讨论】:
-
为什么不直接删除 xlPasteFormats 行代码??
-
如果我删除了 xlPasteFormats,它将不会粘贴现有格式,也不会粘贴边框背景颜色等......
-
删除格式的任何条件方面的一种方法是首先将范围粘贴到 Word,然后从 Word 粘贴到 Outlook。
-
你能分享一个相同的示例代码吗?
-
看我的帖子........
标签: vba excel excel-formula conditional-formatting