我尝试从excel复制数据并自动通过电子邮件发送。一切正常,除了CF. “
我应用下面的代码,但它也应对了条件格式规则,因为格式在它粘贴在outlook中时会发生变化。请帮助我们解决这个问题。
函数RangetoHTML(作为范围)
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
结束功能
答案 0 :(得分:1)
在这个小例子中,我们从一小块具有条件格式的单元开始。宏:
将其放入标准模块中。生成的Word表格应格式化为Excel表格,但没有“条件”
Sub UsingWord()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add
Range("A1:B2").Copy
With wrdDoc
wrdApp.Selection.PasteAndFormat (wdPasteDefault)
.SaveAs ("C:\TestFolder\tdoc.docx")
.Close
End With
wrdApp.Quit
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub
选择您自己的文件和文件夹名称。
您必须在VBA中包含对Word对象模型的引用>工具>参考强>
答案 1 :(得分:0)
条件格式被视为格式化,因此xlPasteFormats是它粘贴CF的原因。我会尝试申请
.Cells(1).FormatConditions.Delete
复制值后,就在开始粘贴选项之前。