OLE automation: Excel Template question

M

Mr. California

As a novice VBA programmer, I have a problem with a routine written as
a click procedure from a form to open an Excel template, insert some
information, print, and (ideally) return control back to the form. The
problem is only in that I don't want to answer the question from Excel
"Do you want to save (y/n):". I just want to print the sheet. Can I
do this in such a way that excel will not prompt me to save? I don't
see any alternative but to unload the form, and that is what causes the
prompt to save. I'm sure there is a better way to do this, but as I
said, I'm new to this stuff. Any help is appreciated. Code appears
below.

Craig
(e-mail address removed)

Option Compare Database
Dim xlObject As Object ' Declare variable to hold the reference to the
Excel Object.

Private Sub AutomateExcel_Click()
'On Error GoTo Err_AutomateExcel_Click

'Create an object for Excel
Set xlObject = CreateObject("excel.application")

'Make the Excel application visible
xlObject.Visible = True
Set xlBook =
xlObject.Workbooks.Add("\\Termsrvr\tshome\********\********\Dev\TS.xlt")
'Activate the worksheet, select the range, activate a cell in the range
xlObject.Worksheets("Sheet1").Activate
xlObject.ActiveSheet.Range("B5").Select
xlObject.ActiveSheet.Range("B5").Activate

'Set focus to LNF on Access form
LNF.SetFocus
'Place the LNF in the active cell.
xlObject.ActiveCell.Value = UCase(LNF.Text)

xlObject.ActiveSheet.Range("F5").Select
xlObject.ActiveSheet.Range("F5").Activate

'Set focus to TSEmpLocation on Access form
TSEmpLocation.SetFocus
'Place TSEmpLocation in the active cell.
xlObject.ActiveCell.Value = TSEmpLocation.Text

xlObject.ActiveSheet.Range("I5").Select
xlObject.ActiveSheet.Range("I5").Activate

'Set focus to TSEmpNum on Access form
TSEmpNum.SetFocus
'Place TSEmpNum in the active cell.
xlObject.ActiveCell.Value = TSEmpNum.Text

xlBook.PrintOut


On Error Resume Next
xlObject.UserControl = True

Exit_AutomateExcel_Click:
xlObject.Quit
Set xlBook = Nothing
Set xlObject = Nothing
Exit Sub
Err_AutomateExcel_Click:
MsgBox Err.Description
Resume Exit_AutomateExcel_Click

End Sub

Private Sub Detail_Click()

End Sub

Private Sub Form_Load()

End Sub
 
J

Jay Taplin

All you need to do is insert this line when you are all done with the
workbook:

xlBook.Close False

The "false" value is for the SaveChanges parameter...

Jay Taplin MCP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top