我目前正在使用Windows 7和Office 2010.我有一个较旧的宏,它在Outlook中创建并发送电子邮件。它使用一个函数(下面)来创建电子邮件的正文。它一直在工作,但我遇到了在Office 2010中运行它的问题。宏从数据文件复制信息并粘贴在宏中的不同工作表中。在创建电子邮件时,它会将数据复制到新手册中。然后它调用此函数来创建电子邮件的正文。当宏击中下面的行(ERROR HAPPENS HERE)时,它会启动该功能并继续创建和发送电子邮件,但电子邮件中没有正文。关于这行代码有什么问题的任何建议将不胜感激。谢谢你的帮助........
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"
' TempFile = "C:\temp" & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
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
With TempWB.PublishObjects.Add( _ ' error happens here
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
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=")
TempWB.Close SaveChanges:=False
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
答案 0 :(得分:0)
因为我弄清楚出了什么问题,请忽略这个问题。一旦变量没有任何数据,那么该字段为空。一旦我确定变量有数据,代码就按预期运行。