T
tmort
I have some code that I am trying to switch to late binding. It is code that
saves the result of a query as an Excel file, then sends this file as an
attaqchment to an Outlook email then deletes the file.
It used to work before I made both changes to the the code dealing with
Excel and Outlook application/object.
Now I get an error at the line:
DoCmd.OutputTo acOutputQuery, "process export qry", acFormatXLS,
CurrentProject.Path & "\" & pFilename, 0
saying that it cannot save the output file to the file name specified.
As I mentioned this used to work before I made changes. It does compile
though.
Any help will be appreciated.
The code is:
Function compexport()
Dim stto As String
Dim stcc As String
Dim stsubject As String
Dim ststartDate As String
Dim stenddate As String
Dim stfrmt As String
Dim stconame As String
Dim stmessage As String
Dim stnoto As String
Dim stnodate As String
Dim stnoconame As String
Dim stpermnumber As String
Dim ststartdateatt As String
Dim stenddateatt As String
Dim mPathAndFile As String, mFileNumber As Integer
Dim R As Recordset, mFieldNum As Integer
Dim mOutputString As String
Dim booDelimitFields As Boolean
Dim booIncludeFieldnames As Boolean
Dim mFieldDeli As String
Dim pbooIncludeFieldnames As String
Dim outApp As Object, objOutlook As Object, outmsg As Object, olmailitem As
Object
Dim oexcel As Object
'Dim osheet As Worksheet
Dim osheet As Object
'Dim rngToFormat As Range
Dim rngToFormat As Object
stconame = Nz([Forms]![export form]![coname], "none")
ststartDate = Nz([Forms]![export form]![begin], "none")
ststartdateatt = Replace(ststartDate, "/", "-")
stenddate = Nz([Forms]![export form]![end], "none")
stenddateatt = Replace(stenddate, "/", "-")
stpermnumber = Nz([Forms]![export form]![cmbpermnumber], "none")
stfrmt = DLookup("[Comp_format]", "export format settings")
stsubject = stconame & " " & "Compliance Sampling Data" & " " & ststartDate
& " " & "to" & " " & stenddate
stto = Nz([Forms]![export form]![to], "none")
stcc = Nz([Forms]![export form]![cc], "")
stmessage = Nz([Forms]![export form]![Message], "")
stnoto = "You forgot to enter a Send To email address"
stnodate = "You must enter a beginning and ending date for the data you wish
to export"
stnoconame = "You forgot to enter a company name"
pbooIncludeFieldnames = "true"
If stto = "none" Then
MsgBox stnoto
Exit Function
Else
If stconame = "none" Then
MsgBox stnoconame
Exit Function
Else
If ststartDate = "none" Then
MsgBox stnodate
Exit Function
Else
If stendate = "none" Then
MsgBox stnodate
Exit Function
Else
If stfrmt = "acFormatXLS" Then
'DoCmd.SendObject acSendQuery, "compliance export qry", acFormatXLS, [stto],
[stcc], , stconame & " " & "Compliance Sampling Data" & " " & ststartDate & "
" & "to" & " " & stenddate, [stmessage], False
pFilename = stconame & " P" & stpermnumber & " " & ststartdateatt & " to " &
stenddateatt & " Compliance Data.xls"
DoCmd.OutputTo acOutputQuery, "compliance export qry", acFormatXLS,
CurrentProject.Path & "\" & pFilename, 0
'DoCmd.TransferSpreadsheet acExport, , "compliance export qry",
CurrentProject.Path & "\" & pFilename, True
'DoCmd.TransferSpreadsheet acImport, 3,"Employees","C:\Lotus\Newemps.wk3",
True, "A1:G12"
mPathAndFile = CurrentProject.Path & "\" & pFilename
'*************************
'Set oapp = CreateObject("Excel.Application")
Set oapp = CreateObject("Excel.Application")
Set oexcel = oapp.Workbooks.Open(Filename:=mPathAndFile)
Set osheet = oexcel.Worksheets("compliance export qry")
oapp.Visible = False
oapp.DisplayAlerts = False
osheet.Activate
With oexcel.Worksheets("compliance export qry").Columns
..Columns("A:S").AutoFit
End With
With oexcel.Worksheets("compliance export qry").PageSetup
..Zoom = False
..FitToPagesTall = 1000
..FitToPagesWide = 1
..Orientation = 2
..PrintGridlines = 0
..PrintTitleRows = "A1:S1"
'.LeftHeader =
..CenterHeader = "&14" & pFilename & "&10"
'.RightHeader =
..LeftFooter = "Report Created &D &T"
'.CenterFooter =
..RightFooter = "Page &P of &N"
..LeftMargin = oapp.InchesToPoints(0.25)
..RightMargin = oapp.InchesToPoints(0.25)
..TopMargin = oapp.InchesToPoints(0.75)
..BottomMargin = oapp.InchesToPoints(0.5)
..HeaderMargin = oapp.InchesToPoints(0.5)
..FooterMargin = oapp.InchesToPoints(0.25)
End With
With osheet.Range("A1:S1")
Set rngToFormat = osheet.Range(oexcel.Worksheets("compliance export
qry").Range("S1"), .Cells(osheet.Rows.Count, "C").end(xlUp).Offset(0, -2)) '
End With
With rngToFormat.Cells.Select
'With borders
oapp.Selection.Interior.ColorIndex = 2
oapp.Selection.Interior.Pattern = 1
oapp.Selection.Borders(xlDiagonalDown.LineStyle) = -4142
oapp.Selection.Borders(xlDiagonalUp).LineStyle = -4142
With oapp.Selection.Borders(xlEdgeLeft)
..LineStyle = 1
..Weight = 2
..ColorIndex = -4105
End With
With oapp.Selection.Borders(xlEdgeTop)
..LineStyle = 1
..Weight = 2
..ColorIndex = -4105
End With
With oapp.Selection.Borders(xlEdgeBottom)
..LineStyle = 1
..Weight = 2
..ColorIndex = -4105
End With
With oapp.Selection.Borders(xlEdgeRight)
..LineStyle = 1
..Weight = 2
..ColorIndex = -4105
End With
With oapp.Selection.Borders(xlInsideVertical)
..LineStyle = 1
..Weight = 2
..ColorIndex = -4105
End With
With oapp.Selection.Borders(xlInsideHorizontal)
..LineStyle = 1
..Weight = 2
..ColorIndex = -4105
End With
End With
With osheet.Range("A1:S1")
..Font.ColorIndex = 1
..Font.Bold = -1
..Interior.ColorIndex = 15
..Interior.Pattern = 1
End With
Set osheet = Nothing 'disconnect from the Worksheet
oexcel.Close SaveChanges:=True 'Save (and disconnect from) the Workbook
Set oexcel = Nothing
oapp.Quit 'Close (and disconnect from) Excel
Set oapp = Nothing
'*******************************************
Set outApp = CreateObject("Outlook.Application")
Set outmsg = outApp.CreateItem(olmailitem)
If stcc = "" Then
With outmsg
..Recipients.Add (stto)
..subject = stsubject
..ReadReceiptRequested = -1
..body = stmessage
..Importance = olImportanceHigh
..Attachments.Add (mPathAndFile)
..Send
End With
Else
With outmsg
..Recipients.Add(stto).Type = 1
..Recipients.Add(stcc).Type = 2
..subject = stsubject
..ReadReceiptRequested = -1
..body = stmessage
..Importance = olImportanceHigh
..Attachments.Add (mPathAndFile)
..Send
End With
End If
Kill mPathAndFile
saves the result of a query as an Excel file, then sends this file as an
attaqchment to an Outlook email then deletes the file.
It used to work before I made both changes to the the code dealing with
Excel and Outlook application/object.
Now I get an error at the line:
DoCmd.OutputTo acOutputQuery, "process export qry", acFormatXLS,
CurrentProject.Path & "\" & pFilename, 0
saying that it cannot save the output file to the file name specified.
As I mentioned this used to work before I made changes. It does compile
though.
Any help will be appreciated.
The code is:
Function compexport()
Dim stto As String
Dim stcc As String
Dim stsubject As String
Dim ststartDate As String
Dim stenddate As String
Dim stfrmt As String
Dim stconame As String
Dim stmessage As String
Dim stnoto As String
Dim stnodate As String
Dim stnoconame As String
Dim stpermnumber As String
Dim ststartdateatt As String
Dim stenddateatt As String
Dim mPathAndFile As String, mFileNumber As Integer
Dim R As Recordset, mFieldNum As Integer
Dim mOutputString As String
Dim booDelimitFields As Boolean
Dim booIncludeFieldnames As Boolean
Dim mFieldDeli As String
Dim pbooIncludeFieldnames As String
Dim outApp As Object, objOutlook As Object, outmsg As Object, olmailitem As
Object
Dim oexcel As Object
'Dim osheet As Worksheet
Dim osheet As Object
'Dim rngToFormat As Range
Dim rngToFormat As Object
stconame = Nz([Forms]![export form]![coname], "none")
ststartDate = Nz([Forms]![export form]![begin], "none")
ststartdateatt = Replace(ststartDate, "/", "-")
stenddate = Nz([Forms]![export form]![end], "none")
stenddateatt = Replace(stenddate, "/", "-")
stpermnumber = Nz([Forms]![export form]![cmbpermnumber], "none")
stfrmt = DLookup("[Comp_format]", "export format settings")
stsubject = stconame & " " & "Compliance Sampling Data" & " " & ststartDate
& " " & "to" & " " & stenddate
stto = Nz([Forms]![export form]![to], "none")
stcc = Nz([Forms]![export form]![cc], "")
stmessage = Nz([Forms]![export form]![Message], "")
stnoto = "You forgot to enter a Send To email address"
stnodate = "You must enter a beginning and ending date for the data you wish
to export"
stnoconame = "You forgot to enter a company name"
pbooIncludeFieldnames = "true"
If stto = "none" Then
MsgBox stnoto
Exit Function
Else
If stconame = "none" Then
MsgBox stnoconame
Exit Function
Else
If ststartDate = "none" Then
MsgBox stnodate
Exit Function
Else
If stendate = "none" Then
MsgBox stnodate
Exit Function
Else
If stfrmt = "acFormatXLS" Then
'DoCmd.SendObject acSendQuery, "compliance export qry", acFormatXLS, [stto],
[stcc], , stconame & " " & "Compliance Sampling Data" & " " & ststartDate & "
" & "to" & " " & stenddate, [stmessage], False
pFilename = stconame & " P" & stpermnumber & " " & ststartdateatt & " to " &
stenddateatt & " Compliance Data.xls"
DoCmd.OutputTo acOutputQuery, "compliance export qry", acFormatXLS,
CurrentProject.Path & "\" & pFilename, 0
'DoCmd.TransferSpreadsheet acExport, , "compliance export qry",
CurrentProject.Path & "\" & pFilename, True
'DoCmd.TransferSpreadsheet acImport, 3,"Employees","C:\Lotus\Newemps.wk3",
True, "A1:G12"
mPathAndFile = CurrentProject.Path & "\" & pFilename
'*************************
'Set oapp = CreateObject("Excel.Application")
Set oapp = CreateObject("Excel.Application")
Set oexcel = oapp.Workbooks.Open(Filename:=mPathAndFile)
Set osheet = oexcel.Worksheets("compliance export qry")
oapp.Visible = False
oapp.DisplayAlerts = False
osheet.Activate
With oexcel.Worksheets("compliance export qry").Columns
..Columns("A:S").AutoFit
End With
With oexcel.Worksheets("compliance export qry").PageSetup
..Zoom = False
..FitToPagesTall = 1000
..FitToPagesWide = 1
..Orientation = 2
..PrintGridlines = 0
..PrintTitleRows = "A1:S1"
'.LeftHeader =
..CenterHeader = "&14" & pFilename & "&10"
'.RightHeader =
..LeftFooter = "Report Created &D &T"
'.CenterFooter =
..RightFooter = "Page &P of &N"
..LeftMargin = oapp.InchesToPoints(0.25)
..RightMargin = oapp.InchesToPoints(0.25)
..TopMargin = oapp.InchesToPoints(0.75)
..BottomMargin = oapp.InchesToPoints(0.5)
..HeaderMargin = oapp.InchesToPoints(0.5)
..FooterMargin = oapp.InchesToPoints(0.25)
End With
With osheet.Range("A1:S1")
Set rngToFormat = osheet.Range(oexcel.Worksheets("compliance export
qry").Range("S1"), .Cells(osheet.Rows.Count, "C").end(xlUp).Offset(0, -2)) '
End With
With rngToFormat.Cells.Select
'With borders
oapp.Selection.Interior.ColorIndex = 2
oapp.Selection.Interior.Pattern = 1
oapp.Selection.Borders(xlDiagonalDown.LineStyle) = -4142
oapp.Selection.Borders(xlDiagonalUp).LineStyle = -4142
With oapp.Selection.Borders(xlEdgeLeft)
..LineStyle = 1
..Weight = 2
..ColorIndex = -4105
End With
With oapp.Selection.Borders(xlEdgeTop)
..LineStyle = 1
..Weight = 2
..ColorIndex = -4105
End With
With oapp.Selection.Borders(xlEdgeBottom)
..LineStyle = 1
..Weight = 2
..ColorIndex = -4105
End With
With oapp.Selection.Borders(xlEdgeRight)
..LineStyle = 1
..Weight = 2
..ColorIndex = -4105
End With
With oapp.Selection.Borders(xlInsideVertical)
..LineStyle = 1
..Weight = 2
..ColorIndex = -4105
End With
With oapp.Selection.Borders(xlInsideHorizontal)
..LineStyle = 1
..Weight = 2
..ColorIndex = -4105
End With
End With
With osheet.Range("A1:S1")
..Font.ColorIndex = 1
..Font.Bold = -1
..Interior.ColorIndex = 15
..Interior.Pattern = 1
End With
Set osheet = Nothing 'disconnect from the Worksheet
oexcel.Close SaveChanges:=True 'Save (and disconnect from) the Workbook
Set oexcel = Nothing
oapp.Quit 'Close (and disconnect from) Excel
Set oapp = Nothing
'*******************************************
Set outApp = CreateObject("Outlook.Application")
Set outmsg = outApp.CreateItem(olmailitem)
If stcc = "" Then
With outmsg
..Recipients.Add (stto)
..subject = stsubject
..ReadReceiptRequested = -1
..body = stmessage
..Importance = olImportanceHigh
..Attachments.Add (mPathAndFile)
..Send
End With
Else
With outmsg
..Recipients.Add(stto).Type = 1
..Recipients.Add(stcc).Type = 2
..subject = stsubject
..ReadReceiptRequested = -1
..body = stmessage
..Importance = olImportanceHigh
..Attachments.Add (mPathAndFile)
..Send
End With
End If
Kill mPathAndFile