CC10
asked on
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.Appl ication")
I have checked that the 14.0 Object Library boxes are ticked in both Outlook and Excel references.
Thanks,
CC
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(xlC ellTypeVis ible)
'You can also use a range if you want
'Set rng = Sheets("Position").Range(" A1:Q36").S pecialCell s(xlCellTy peVisible)
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.Appl ication")
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
.Send
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: http://www.jpsoftwaretech.com/excel-vba/send-email-from-excel/
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.A dd(0, 0, rng.Width + 10, rng.Height + 10)
cht.Chart.Paste
cht.Chart.Export imgSrc
cht.Delete
ExitProc:
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> " & _
"<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
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
'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Na me, _
Source:=TempWB.Sheets(1).U sedRange.A ddress, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.Fi leSystemOb ject")
Set ts = fso.GetFile(TempFile).Open AsTextStre am(1, -2)
RangetoHTML = ts.ReadAll
ts.Close
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
Set OutApp = CreateObject("Outlook.Appl
I have checked that the 14.0 Object Library boxes are ticked in both Outlook and Excel references.
Thanks,
CC
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(xlC
'You can also use a range if you want
'Set rng = Sheets("Position").Range("
Set rng = Sheets("Position").Range("
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.Appl
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
.Send
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: http://www.jpsoftwaretech.com/excel-vba/send-email-from-excel/
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
'Set rng = Range("A1").CurrentRegion
rng.CopyPicture xlScreen, xlPicture
Set cht = ActiveSheet.ChartObjects.A
cht.Chart.Paste
cht.Chart.Export imgSrc
cht.Delete
ExitProc:
Application.ScreenUpdating
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> " & _
"<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
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
'Publish the sheet to a htm file
With TempWB.PublishObjects.Add(
SourceType:=xlSourceRange,
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Na
Source:=TempWB.Sheets(1).U
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.Fi
Set ts = fso.GetFile(TempFile).Open
RangetoHTML = ts.ReadAll
ts.Close
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER