Jump to content

Open, Edit and Print Excel spreadsheet from Access VBA


Recommended Posts

Hey guys! It's been a LOOOOONNNNGGG time.

Anyways, I'm trying to figure out how I can program a button in Access with an On Click event that will:

1. Open a file in Excel

2. Select a worksheet

3. Enter some some data from form fields in Access

4. Print the document

5. Close Excel

Anyone have any insight into this? I have found a lot of information by doing a google search, but none of it quite fits what I'm trying to do. I can successfully do this with Word, and the coding it's very similar to what I'm seeing for Excel. Thanks for your help.

Link to comment
Share on other sites


Long time no see. You should pop in more often.

Try this as a module to run from a button click:

Sub Run_Excel()
Dim appexcel As Object
Set appexcel = CreateObject("Excel.Application")
'Open Workbook, replace Devlist.xls with your filename
appexcel.workbooks.Open "devlist.xls"
appexcel.Visible = True

'Select Sheet1
appexcel.Sheets("Sheet1").Select

'repeat as needed
' Select Cell to input data into
appexcel.Range("A2").Select
' Get data from table dlookup(Value field, table, criteria)
appexcel.activecell.Value = DLookup("[ID]", "Sheet1", "[ID]=2")

'Set print area
appexcel.ActiveSheet.PageSetup.PrintArea = "$A$1:$K$2"
'Print document
appexcel.ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True

End Sub

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...