Hi
I have a work PC with XP 64 bit and a laptop with Win 7 64 bit both running Office 2007.
The below code works perfectly on the Win 7 machine, however on the XP machine while the code runs to completion most of the time sometimes it fails with varying error messages.
I found the reason for the failures to be part way through the code is an Excel routine, which ask Excel to Save, Close then Quit. On the XP machine when I open task manager, I have dozens on Excel instances open. Further if I run the code with task manager open and kill them one by one the code always run through to the end.
I have tried adding taskkill into the code but due to the speed of the process it kills both the running task and the new one that is starting.
Am I missing any patches/add-ins (anything) that will avoid upgrade to Win 7. Is their a smarter way of resolving this?
Thanks in advance
Rob
Private Sub Command0_Click()
Dim rs As DAO.Recordset
Dim sql As String
Dim strPath As String
Dim avarattach(3) As Variant
Dim stDocName As String
Dim ApXL As Object
Dim workBook As Object
Dim Worksheets As Object
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
x = [Forms]![Form]![Text10]
MonthTag = [Forms]![Form]![Text18]
Dim strbody As String
Dim SigString As String
Dim Signature As String
strPath = "C:\Temp\IncomeReport\"
sql = "SELECT DISTINCT Query.ServicerName, Query.Salutation, Query.ContactEmail FROM Query;"
Set rs = CurrentDb().OpenRecordset(sql, dbOpenSnapshot)
Do While Not rs.EOF
strBrokerCode = rs!ServicerName
Email = rs!ContactEmail
Salutation = rs!Salutation
DoCmd.OutputTo acOutputReport, "Master", acFormatPDF, strPath & strBrokerCode & " - SummaryIncomeRpt" & ".pdf"
DoCmd.OutputTo acOutputQuery, "qry_SummaryIncomeTable", acFormatXLS, strPath & strBrokerCode & " - FullIncomeRpt.xls", False, "", 0, acExportQualityPrint
Set ApXL = CreateObject("Excel.Application")
With ApXL
.Application.Visible = False
.UserControl = False
.Workbooks.Open strPath & strBrokerCode & " - FullIncomeRpt.xls"
.Columns("A:A").ColumnWidth = 7.5
.Columns("B:B").ColumnWidth = 33
.Columns("C:G").Select
.Selection.ColumnWidth = 12
.Columns("C").NumberFormat = "#,##0.00_);[Red](#,##0.00)"
.Columns("D").NumberFormat = "#,##0.00_);[Red](#,##0.00)"
.Columns("E").NumberFormat = "#,##0.00_);[Red](#,##0.00)"
.Columns("F").NumberFormat = "#,##0.00_);[Red](#,##0.00)"
.Columns("G").NumberFormat = "#,##0.00_);[Red](#,##0.00)"
.Columns("H:H").EntireColumn.Delete
.Rows("1:1").Select
.Selection.WrapText = True
.Selection.RowHeight = 45
.Range("A1").Select
End With
With ApXL.ActiveSheet.PageSetup
.LeftFooter = "&F"
.RightFooter = "&P of &N"
.LeftMargin = ApXL.InchesToPoints(0.25)
.RightMargin = ApXL.InchesToPoints(0.25)
.TopMargin = ApXL.InchesToPoints(0.25)
.BottomMargin = ApXL.InchesToPoints(0.5)
.HeaderMargin = ApXL.InchesToPoints(0.25)
.FooterMargin = ApXL.InchesToPoints(0.25)
.PrintQuality = 600
.Orientation = xlLandscape
.Draft = False
.FirstPageNumber = 1
.PaperSize = xlPaperA4
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
End With
With ApXL
.ActiveWorkbook.Save
.ActiveWorkbook.Close
.Application.Quit
End With
SigString = "C:\Temp\Sig\finance.htm"
If Dir(SigString) <> "" Then
Signature = GetBoiler(SigString)
Else
Signature = ""
End If
' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
objOutlook.Session.Logon
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
Set OutApp = CreateObject("Outlook.Application")
With objOutlookMsg
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add(Email)
objOutlookRecip.Type = olTo
' Add the CC recipient(s) to the message.
'Set objOutlookRecip = .Recipients.Add("")
'objOutlookRecip.Type = olCC
' Set the Subject, Body, and Importance of the message.
.Subject = "Cumulative Income Report for YTD " & MonthTag & " for " & strBrokerCode
.HTMLBody = "<SPAN STYLE='font: 8pt Verdana'>Dear " _
& Salutation & "<BR></BR><BR></BR>" & _
"Please find attached your Income Report for YTD Cumulative to June." & "<BR></BR><BR></BR>" & _
"Any queries drop a line to ############." & "<BR></BR><BR></BR>" & _
"There are 2 files attached, a summary page and full client by client page." & "<BR></BR><BR></BR>" & _
"Kind Regards." & "<BR></BR><BR></BR>" & _
"Finance Team." & "<BR></BR><BR></BR>" & _
"</span>" & "<BR></BR>" & Signature & "<BR></BR><BR></BR>"
.Importance = olImportanceHigh 'High importance
' Add attachments to the message.
If Not IsMissing(AttachmentPath) Then
AttachmentPath = strPath & strBrokerCode & " - SummaryIncomeRpt" & ".pdf"
Set objOutlookAttach = .Attachments.Add(AttachmentPath)
AttachmentPath = strPath & strBrokerCode & " - FullIncomeRpt.xls"
Set objOutlookAttach = .Attachments.Add(AttachmentPath)
End If
' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
If Not objOutlookRecip.Resolve Then
objOutlookMsg.Display
End If
Next
x = x + TimeValue([Forms]![Form]![Text14])
.DeferredDeliveryTime = x
.SendUsingAccount = OutApp.Session.Accounts.Item(3)
.Send
End With
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
rs.MoveNext
Loop
Set rs = Nothing
End Sub
Bookmarks