I'm currently using Windows 7 and Office 2010. I have an older macro that was creating and sending an e-mail in Outlook. It was using a function (below) to create the body of the e-mail. It had been working but I'm having an issue getting it to work in Office 2010. The macro copies info from a data file and pastes in a different worksheet in the macro. As it goes to create the e-mail, it copies the data into a newworbook. It then calls this function to create the body of the e-mail. When the macro hits the line below (ERROR HAPPENS HERE), it kicks out of the function and continues on creating and sending the e-mail but there is no body in the e-mail. Any suggestions on what is wrong with this line of code would be greatly appreciated. Thanks for the help........
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
Please disregard this question as I figured out what was wrong. Once of the variables didn't have any data, so the field was blank. Once I made sure the variable had data, the code ran as expected.