Solved

Help with formatting column widths in vba upon export to excel from access.

Posted on 2013-05-10
5
2,338 Views
Last Modified: 2013-05-16
I've got a query in an access database which I'm using in some vba code.  I'm exporting its results to an Excel spreadsheet.  All is working well except that the columns are all the same width and rather narrow.  How can I cause them to automatically adjust to appropriate widths?

Here is the code:
DoCmd.OutputTo acOutputQuery, "OptCouncilRpt", acFormatXLS, "C:\Temp\OptCouncilRpt.xls"

Open in new window

0
Comment
Question by:David L. Hansen
[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
  • 2
  • 2
5 Comments
 
LVL 41

Accepted Solution

by:
Sharath earned 250 total points
ID: 39157327
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 250 total points
ID: 39157340
after exporting to excel, you have to open the excel file using vba to set the formatting

DoCmd.OutputTo acOutputQuery, "OptCouncilRpt", acFormatXLS, "C:\Temp\OptCouncilRpt.xls"
                                 

dim xlObj as object
set xlobj=createobject("excel.application")
      xlobj.workbooks.open "C:\Temp\OptCouncilRpt.xls"

   
            xlObj.activeSheet.usedrange.columns.autofit

            xlObj.activeworkbook.save

           xlObj.quit
1
 
LVL 15

Author Comment

by:David L. Hansen
ID: 39157380
Sounds great capricorn, there must be a small problem though.  Your code runs without error but only creates the file, it does not open.  Not sure why.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39157449
<only creates the file, it does not open. >

the code open it, hidden, formatted the column width and saved.

open the excel file and see.

do you want it to stay open?
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 39172333
Yes, sorry for forgetting about this.  I did want it to open automatically.  I found the code I needed too.  Thanks a ton.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

751 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