I have a scheduled tasks that opens a batch file that runs an Excel module which runs through some calculations then sends an outlook e-mail. The e-mail, attaches some info to the body and displays, rather than sends, in order for the user to validate and the e-mail before sending. When logged in during the day, the module runs fine and does what it's supposed to do as all calculations are run and the e-mail pops up with the correct data; however, at night, the batch shows Excel ran all the calculations but the e-mail never pops up. So it's not interacting with Outlook when logged out. I'm on an Aruba system and have set up all the Windows settings to turn off display never, power & sleep turn off (never) and I don't log off at night (just switch my screen off) in hopes it doesn't log me out; however, in the morning, it has me log back in again. Is there anyway around the logoff that is happening or is there a way to force the module to communicate with outlook?
Here's the code that sends the e-mail:
Here's the code that sends the e-mail:
VBA Code:
Sub Mail_Selection_Range_Outlook_Body4()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim StrBody2 As String
Dim StrDate As String
txtdate = Format(Now, "mm") & "_" & Format(Now, "dd") & "_" & Format(Now, "yyyy")
strbody = "Link to file:" & vbCrLf & vbCrLf & "<a href=""[URL='https://sharepoint.abc.com/sites/Shared%20Documents/Report_']https://sharepoint.abc.com/sites/Shared Documents/Report_[/URL]" & txtdate & "%204pm.xlsx?Web=1"">FILE</a>"
Set rng = Nothing
On Error Resume Next
'You can also use a fixed range if you want
Set rng = ThisWorkbook.Sheets("Email").Range("A1:B16").SpecialCells(xlCellTypeVisible)
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
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.Display
.To = "Cag, Michelle <[EMAIL]Michelle.Cag@abc.com[/EMAIL]>"
.CC = ""
.BCC = ""
.Subject = "4PM Report"
.HTMLBody = strbody & RangetoHTML(rng) & .HTMLBody
.Display 'or use .Send
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
Call SaveBucketsFile4
End Sub
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"
'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).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.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
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
Last edited by a moderator: