Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Export Access Report to Excel

Posted on 2007-12-02
5
Medium Priority
?
1,009 Views
Last Modified: 2013-11-28
I'm exporting an Access report to Excel and formatting the spreadsheet with the below code in my OnClick event.

I would like to (in order of priority):
1. Remove the prompt to save the file on xcelapp.quit (assuming that's when that prompt appears)
2. Have the user be able to specify the file path instead of having to use the default
3. Have the user be able to specify the file name instead have it hard coded

Any help is appreciated!
Thx,

mv
Dim stDocName As String
    
    stDocName = "rpt_Report1"
    DoCmd.OutputTo acReport, stDocName
 
    Dim xcelwb As Excel.Workbook
    Dim xcelapp As Excel.Application
    Dim Sheet As Excel.Worksheet
            
    Set xcelapp = New Excel.Application
    Set xcelwb = xcelapp.Workbooks.Open("rpt_Report1.xls")
    Set Sheet = xcelapp.ActiveWorkbook.Sheets(1)
       
       'format the excel data
       With Sheet
            .Rows("1:1").Font.Bold = True
       End With
       
    xcelapp.Quit
    Set xcelapp = Nothing
    Set xcelwb = Nothing
    Set Sheet = Nothing

Open in new window

0
Comment
Question by:Michael Vasilevsky
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 20394169
mvasilevsky,
<Remove the prompt to save the file on xcelapp.quit (assuming that's when that prompt appears)>
Ok then do you want it to Save or NOT save?
Either way the end of your code will look like this (Remove the comments on the Save option you want)
'    'Saves changes
'    xcelwb.Close True
'    'Does NOT save changes
'    'xcelwb.Close False
'    xcelapp.Quit
'    Set xcelapp = Nothing
'    Set xcelwb = Nothing
'    Set Sheet = Nothing

<Have the user be able to specify the file path instead of having to use the default>
<Have the user be able to specify the file name instead have it hard coded>

As far a s I can see you are specifying the File's location when you output it.
I presume you are opening the same file to be formatted.

In that case the File name and path are the same in both so all you need to do is specify the Output file, save that filename and path, then use that location to open the same file to be formatted.

Is this correct?

JeffCoachman
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 20394174
Hi mvasilevsky,

Try using the GetOpenFilename Method
http://www.dailydoseofexcel.com/archives/2004/06/09/getopenfilename/


Good Luck!

Gary
0
 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 2000 total points
ID: 20400048
mvasilevsky,

Here is a sample:
https://filedb.experts-exchange.com/incoming/ee-stuff/5974-Access--EE_Q22996269ExportExcel.zip

Notes:
-If you provide a filename of a file that already exists, it will be over-written without warning, so be careful!
If you want to see the existing files in the dialog box, reverse the comments on these two lines in the FileToOpen Function:
OFN.lpstrFilter = "Excel Files (*.xls)" + Chr$(0) + "*.999" + Chr$(0)
'OFN.lpstrFilter = "Excel Files (*.xls)" + Chr$(0) + "*.xls" + Chr$(0)


-You need to add the cleanup code to your error handler, in case the code fails and the Excel objects are not closed.
Something similar to this:

On Error GoTo Err_ExcelExportAndFormat

********** Your Export/Format Code Goes Here **********

'Recordset Cleanup
rst.Close
Set rst = Nothing
dbs.Close
Set dbs = Nothing

Exit_ExcelExportAndFormat:
    'Avoids the endless loop
    On Error Resume Next
    Set xcelapp = Nothing
    Set xcelwb = Nothing
    Set Sheet = Nothing
    Exit Sub

Err_ExcelExportAndFormat:
    MsgBox "There was an error executing the command." _
    & vbCrLf & vbCrLf & "Error " & Err.Number & ": " _
    & vbCrLf & vbCrLf & Error, vbExclamation
    Resume Exit_ExcelExportAndFormat



-The Code to make the Dialog box appear is what I use.
If you are interested, there is a more popular way here:
http://www.mvps.org/access/api/api0001.htm
(Neither one is short or easy to read!)
:O

Study and test it fully.
Let me know if you have any issues

JeffCoachman
0
 
LVL 10

Author Closing Comment

by:Michael Vasilevsky
ID: 31412206
Thank you much! Answered all my questions
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 20417902
mvasilevsky,

Glad I could help!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question