473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Exporting a query to excel using outputTo and adding a title

I am using OutputTo to export a query to excel, prompt the user to save as
and then open the excel file. See below:
DoCmd.OutputTo acOutputQuery, "q701ExportFields", acFormatXLS, , True 'Opend
Excel Automatically

Now I want to add a row to the opened spreadsheet at the top and add a title.
How do I accomplish this? I am new to excel formatting, but I created a
macro and the code to do this is:
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Range("A1").Select
ActiveCell.FormulaR1C1 = "Title"

I get an error when I run this. Do I have to set an object variable and
select the active sheet if I use the OutputTo method as described above?

Thanks,
Karen

--
Message posted via http://www.accessmonster.com
Apr 18 '06 #1
3 3078
Try something like this

I use the following and it seems to work fine:

reportfilename = ReportDirectory & reportfilename
Set es = CreateObject("Excel.Application")
es.Visible = False
es.Workbooks.Open FileName:=reportfilename
es.Sheets("q701ExportFields").Select ' or whatever the tab ends up
being named
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Range("A1").Select
ActiveCell.FormulaR1C1 = "Title"

' at this point you can do almost anything that you can do in a macro
(including running an excel macro
es.ActiveWorkbook.Save
es.ActiveWorkbook.Close (False)
es.Application.Quit

=============================

I set this one up to not show what is being done but if you take out
the es.visible it will show. And if you take out the close and quit it
will remain open.
Ron

Apr 18 '06 #2
Thanks for your response. The only problem I have is that I would have to
handle the naming of the file and the directory. If I use the OutputTo
method, the save as dialog comes up and the user can save the file wherever
they want. I may be missing something simple, but I just don't know enough
about the excel export. I was hoping to still use the OutputTo method and
simply add a title to the opened spreadsheet. Do you know if that is
possible?

Thanks.
Ron2005 wrote:
Try something like this

I use the following and it seems to work fine:

reportfilename = ReportDirectory & reportfilename

Set es = CreateObject("Excel.Application")

es.Visible = False

es.Workbooks.Open FileName:=reportfilename

es.Sheets("q701ExportFields").Select ' or whatever the tab ends up
being named
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Range("A1").Select
ActiveCell.FormulaR1C1 = "Title"

' at this point you can do almost anything that you can do in a macro
(including running an excel macro

es.ActiveWorkbook.Save
es.ActiveWorkbook.Close (False)

es.Application.Quit

=============================

I set this one up to not show what is being done but if you take out
the es.visible it will show. And if you take out the close and quit it
will remain open.

Ron


--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200604/1
Apr 19 '06 #3
The problem there is that if they are in that sense selecting where to
save the file to, you have to somehow find out where that file is in
order for you to open it and do whatever is necessary.

That is an area that I have not gotten into so I am not sure if it is
doable.

A thought,
In the code, it is using a save. Instead of doing that, just before
that change the visible to yes and then use saveas instead of save.
That may give you the what you want with you getting a chance to do the
extra stuff before it is saved. I would still do the copy and opening
the copy before hand so that they don't accidentially overwrite your
master with something that won't work properly.

Ron

Apr 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: deko | last post by:
This code is behind a button that exports the contents of a query ("qryXL_Tx") to an Excel spreadsheet. It works fine the first time, but will not run a second time. When I go to look at my...
5
by: Carl | last post by:
Please can anyone tell me how I can create a macro to save the results of a query as an excel file? The query is called Student List and I would like to save it to "My Documents". We have had...
2
by: Kenneth | last post by:
How do I remove the limitation in Access that deny me from exporting 24000 rows and 17 columns (in a query) into Excel? Kenneth
4
by: searider86 | last post by:
Does anyone know if there is a snippet of code that automatically removes the wrap text format in excel? I have an access form that displays data and allows the user to click on a cmdbutton to...
3
by: Frederico Ottoni | last post by:
Hi there, Please, how can I export a report from access to excel? Is there a command to do this? A sample would be very appreciated. Thanks in advance. Ottoni. *** Sent via Developersdex...
5
by: sara | last post by:
I have reports that run from a form where the user can choose a date range, or they run automatically for a week in the "Weekly Reports" option. I created 2 queries and 2 reports - one query...
8
by: paquer | last post by:
I'd like to provide an Export Function from my forms, where the User can choose the File name & save location Although I cannot get the "Save As" dialog box to open properly from access. What is...
3
by: JHNielson | last post by:
I am having quite the unique problem trying to Export to Excel and I need ot find a solution within 3 hours. -- PLEASE HELP! The system exports a file from the application that the users can make...
2
by: atlbearcat | last post by:
Here's one that's been bugging me for about a week now... I have a form that allows users to filter records, simple enough. But I want to give them the option to export the filtered records to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.