Excel to Outlook, script works in 2000 but problem in 2003

sy278

New Member
Joined
Jul 22, 2005
Messages
6
Hi Guys,

I have the routine below set up to take a worksheet, and use the worksheet as the body of an email. The email needs to include a header image in the body, the routine below works perfectly in excel / outlook 2000, but in 2003 the email has the image attached but the body just shows the IE missing image indicator.

Can anyone tell me whats wrong?

Code:
Public Function SheetToHTML(sh As Worksheet)

    Dim TempFile As String
    Dim Nwb As Workbook
    Dim fso As Object
    Dim ts As Object

    sh.Copy
    Set Nwb = ActiveWorkbook

    With Nwb.Sheets(1)
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With

    TempFile = Environ$("temp") & "/" & _
               Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    Nwb.SaveAs TempFile, xlHtml
    Nwb.Close False

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    SheetToHTML = ts.ReadAll
    ts.Close


    Set ts = Nothing
    Set fso = Nothing
    Set Nwb = Nothing
    Kill TempFile
End Function
Public Sub EMail()
  
' Outlook objects
   Dim OutApp As Outlook.Application
    Dim OutMail As Outlook.MailItem
  Dim colAttach As Outlook.Attachments
  Dim l_Attach As Outlook.Attachment
  Dim oSession As MAPI.Session
  ' CDO objects
  Dim oMsg As MAPI.Message
  Dim oAttachs As MAPI.Attachments
  Dim oAttach As MAPI.Attachment
  Dim colFields As MAPI.Fields
  Dim oField As MAPI.Field
    Dim done, current, temp, addresses
  
  Dim strEntryID As String
      
  ' Delete excess Rows
   Rows("1:3").Select
    Selection.Delete Shift:=xlUp
  
   ' create new Outlook MailItem
  Set OutApp = CreateObject("Outlook.Application")
  Set OutMail = OutApp.CreateItem(olMailItem)

  ' add graphic as attachment to Outlook message
  ' change path to graphic as needed
  Set colAttach = OutMail.Attachments
  Set l_Attach = colAttach.Add("\\Other\OP-GDC.gif")
  OutMail.Close olSave
  strEntryID = OutMail.EntryID
  Set OutMail = Nothing
  ' *** POSITION CRITICAL *** you must dereference the
  ' attachment objects before changing their properties
  ' via CDO
  Set colAttach = Nothing
  Set l_Attach = Nothing
    
  ' initialize CDO session
  On Error Resume Next
  Set oSession = CreateObject("MAPI.Session")
  oSession.Logon "", "", False, False
  
  ' get the message created earlier
  Set oMsg = oSession.GetMessage(strEntryID)
  ' set properties of the attached graphic that make
  ' it embedded and give it an ID for use in an <IMG> tag
  Set oAttachs = oMsg.Attachments
  Set oAttach = oAttachs.Item(1)
  Set colFields = oAttach.Fields
  Set oField = colFields.Add(CdoPR_ATTACH_MIME_TAG, "image/gif")
  Set oField = colFields.Add(&H3712001E, "myident")
  oMsg.Fields.Add "{0820060000000000C000000000000046}0x8514", 11, True
  oMsg.Update
  
 
        
    current = 1
    done = False
    temp = ""
    addresses = ""
    Do
        temp = Worksheets(2).Range("a" & current).Value
        If (temp = "") Then
        done = True
        Else: addresses = addresses & ";" & temp
        End If
        current = current + 1
    Loop Until done = True
    
     ' get the Outlook MailItem again
  Set OutMail = OutApp.GetNamespace("MAPI").GetItemFromID(strEntryID)
    
  ' add HTML content -- the <IMG> tag
    With OutMail
        .To = addresses
        .CC = ""
        .BCC = ""
        .Subject = "Genesys Telephony Communication - " & Date
        .HTMLBody = "<IMG align=baseline border=0 hspace=0 src=cid:myident>" & SheetToHTML(Sheets("Report"))
        .Send   'or use .Display
    End With
    Application.ScreenUpdating = True
    
      
  ' clean up objects
  Set oField = Nothing
  Set colFields = Nothing
  Set oMsg = Nothing
  oSession.Logoff
  Set oSession = Nothing
  Set OutMail = Nothing
    Set OutApp = Nothing
    
    'Close workbook without saving
    Application.DisplayAlerts = False
 Application.Quit

End Sub

Any help would be fantastic thanks.

SY
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Mate...this is my personal experience.

Excel 2003 is bogged up software with a lot many bugs. I have Excel 2003 but I use Excel 2000 !
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top