Hi
I have built a bit of code to e-mail a series of Excel files
which hold data from different queries.I have built the basic
e-mailing into a function and embedded a call to Excel in order
to format the spreadsheet as I am using DoCmd.TransferSpreadsheet
which doesn't retain any formatting.
I have included the function code below with a dummy sub which
calls the function twice, using the same values for each variable.
The first time it runs through it works perfectly. You will see there is
a second call to the function. When it tries the second time I get
an error on the line "Cells.EntireColumn.Autofit". It errors out with
Method Cells of Object _Global failed. I am struggling to work out
why the code works on the first pass through but won't on a second
loop through. Any ideas welcome.
I have built a bit of code to e-mail a series of Excel files
which hold data from different queries.I have built the basic
e-mailing into a function and embedded a call to Excel in order
to format the spreadsheet as I am using DoCmd.TransferSpreadsheet
which doesn't retain any formatting.
I have included the function code below with a dummy sub which
calls the function twice, using the same values for each variable.
The first time it runs through it works perfectly. You will see there is
a second call to the function. When it tries the second time I get
an error on the line "Cells.EntireColumn.Autofit". It errors out with
Method Cells of Object _Global failed. I am struggling to work out
why the code works on the first pass through but won't on a second
loop through. Any ideas welcome.
Code:
Sub Test()
Dim DF As String, TM As String, cm As String, sm As String
DF = "TEST Exeter_MH_Email_Data"
TM = "xxx ' site won't let me put real addresses!
cm = "yyy" ' site won't let me put real addresses!
sm = "Test"
Test_Email_data DF, TM, cm, sm
Test_Email_data DF, TM, cm, sm
End Sub
Function Test_Email_data(DataFile As String, To_mail As String, CC_mail As String, Subj_mail As String)
If FileORDirExists = True Then
Kill "C:\CAB43\" & DataFile & ".xls"
End If
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
DataFile, "C:\CAB43\" & DataFile & ".xls", True
Dim xlApp As Object
Dim xlWkb As Object
Dim xlSht As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlWkb = xlApp.Workbooks.Open("C:\CAB43\" & DataFile & ".xls")
Set xlSht = xlApp.ActiveWorkbook.Worksheets(1)
xlApp.DisplayAlerts = False
xlApp.Interactive = False
xlApp.ScreenUpdating = False
xlSht.Activate
'code to format spreadsheet before e-mailing
With xlSht
Cells.EntireColumn.AutoFit
Range("A1").Select
End With
Set Crng = ActiveCell.CurrentRegion
RowCount = Crng.Rows.Count
Colcount = Crng.Columns.Count
Range(Cells(1, 1), Cells(RowCount, Colcount)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ThemeColor = 2
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ThemeColor = 2
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ThemeColor = 2
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ThemeColor = 2
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ThemeColor = 2
.TintAndShade = 0
.Weight = xlThin
End With
xlApp.ActiveWorkbook.Save
xlApp.DisplayAlerts = True
xlApp.Interactive = True
xlApp.ScreenUpdating = True
xlWkb.Close
xlApp.Quit
Set xlSht = Nothing
Set xlWkb = Nothing
Set xlApp = Nothing
Set Olk = CreateObject("Outlook.Application")
Set Itm = Olk.CreateItem(olMailItem)
With Itm
.To = To_mail
.CC = CC_mail
.Subject = Subj_mail
.Attachments.Add ("C:\CAB43\" & DataFile & ".xls")
.Body = "Please find attached details of new UBRN(s). " _
& vbCrLf & vbCrLf & "Helpdesk Team"
.Send
End With
Set Itm = Nothing
Set Olk = Nothing
Kill "C:\CAB43\" & DataFile & ".xls"
End Function