problem with office 2010 sending mail

I have recently installed Office 2010 and now have a problem with the following code. It freezes on :

 Set OutApp = CreateObject("Outlook.Application")

I have checked that the 14.0 Object Library boxes are ticked in both Outlook and Excel references.


Here is the code:
Option Explicit
Public Const TEMP_PIC_NAME = "EmailTemp.png" 'change extension to change picture type
Sub PositionMail()
' Don't forget to copy the function RangetoHTML in the module.
' Working in Office 2000-2010
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object

    Set rng = Nothing
    On Error Resume Next
    'Only the visible cells in the selection
    'Set rng = Selection.SpecialCells(xlCellTypeVisible)
    'You can also use a range if you want
    'Set rng = Sheets("Position").Range("A1:Q36").SpecialCells(xlCellTypeVisible)
    Set rng = Sheets("Position").Range("A1:v36")
    On Error GoTo 0

    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    'On Error Resume Next
    With OutMail
        .To = "xxxx"
        .CC = "xxxx"
        .BCC = ""
        .Subject = "PositionSheet" & " " & Date
        .HTMLBody = RangetoHTML2(rng)
        .display        'Send   'or use .Display
    End With
    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
    'ActiveWorkbook.Close True
End Sub
'Following CopyRangeToGIF and RangetoHTML2 routines Source:  Adapted from:
Sub CopyRangeToGIF(rng As Range) ' save a range from Excel as a picture
'Dim rng As Excel.Range
Dim cht As Excel.ChartObject
Dim imgSrc As String

    imgSrc = ThisWorkbook.Path & "\" & TEMP_PIC_NAME
    Application.ScreenUpdating = False
    'Set rng = Range("A1").CurrentRegion
    rng.CopyPicture xlScreen, xlPicture
    Set cht = ActiveSheet.ChartObjects.Add(0, 0, rng.Width + 10, rng.Height + 10)
    cht.Chart.Export imgSrc
    Application.ScreenUpdating = True
    Set cht = Nothing
    Set rng = Nothing

End Sub
Function RangetoHTML2(rng As Range)
Dim imgSrc As String

    Call CopyRangeToGIF(rng)
    imgSrc = ThisWorkbook.Path & "\" & TEMP_PIC_NAME
    RangetoHTML2 = "<body><font face=Arial color=#000080 size=2></font>" & _
        "<img alt='' hspace=0 src='" & imgSrc & "' align=baseline 0rder=0>&nbsp;" & _
        "<br /><br />No Trading on a Friday or for 24hours after a stop. No averaging of positions. If the intramonth loss exceeds 1%, max exposure of the portfolio cannot exceed 50%</body>"
End Function
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2010
    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 past the data in
    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
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        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, _
        .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
    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
End Function
Who is Participating?
terencinoConnect With a Mentor Commented:
Well your code works just fine for me, in Excel 2010. I didn't have to tick any other references, these were ticked as shown attached.ReferencesAre you using the 32-bit version of Office or the 64-bit version
CC10Author Commented:
I uninstalled the 64 bit and installed the 32-bit version and now it works again. Thanks.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.