Excel publish html to outlook error

S

stevenleongusa

I try to write a vbscript to extract data sheet from Excel and attach
the data to outlook. It works fine in Excel Macro. but does not work
in .vbs script.

I received a runtime error while running the
ActiveWorkbook.PublishObjects.Add... Does anyone how to resolve this
problem? Did I setup the PublishObjects correctly?

This is the script I created.

Dim appOutlook ' As Object
Dim MailItem 'As Object
Dim FSObject 'As Object
Dim myTo 'As String
Dim myCC 'As String
Dim mySub 'As String

Dim FileName, WorksheetName, xRange, tmpFile
FileName = "Milestones.xls"
htmlFile = "myRange.html"
WorksheetName = "sheet1"
xRange = "C1:G12"
Set objExcel = Wscript.CreateObject("Excel.Application")
Set objWB = objExcel.Workbooks.Open(GetPath & FileName)
Set objSheet = objExcel.ActiveWorkbook.Worksheets(WorksheetName)

' Make Excel visible while debugging
objExcel.Visible = False
objSheet.Activate

objExcel.Range(xRange).Select
Set rngeSend = objExcel.Range(xRange)
Set FSObject = CreateObject("Scripting.FilesystemObject")
Set appOutlook = CreateObject("Outlook.Application")
Set MailItem = appOutlook.CreateItem(0)

myTo = "(e-mail address removed)"
mySub = "This is Milestones"

tmpFile = GetPath & htmlFile
MsgBox "tmpFile = " & tmpFile
MsgBox "rngeSend.Parent.Name = " & rngeSend.Parent.Name
MsgBox "rngeSend.Address = " & rngeSend.Address

objExcel.ActiveWorkbook.PublishObjects.Add(0, tmpFile,
rngeSend.Parent.Name, rngeSend.Address, 0).Publish True

Set TStream = FSObject.OpenTextFile(tmpFile, 1)
strHTMLBody = TStream.ReadAll
strHTMLBody = Replace(strHTMLBody, "align=center",
"align=left", , , vbTextCompare)

TStream.Close
Kill tmpFile

With MailItem
.HTMLBody = strHTMLBody
.Subject = mySub
.To = myTo
.Display
End With

Set MailItem = Nothing
Set appOutlook = Nothing
Set FSObject = Nothing
Set TStream = Nothing
objWB.Close
Set objWB = Nothing
objExcel.Quit
Set objExcel = Nothing
IF err.number <> 0 then
Msgbox "Error: " & err.number & " - " & err.description
End If
On Error Goto 0
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top