Solved

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

Posted on 2013-05-10
5
2,076 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
  • 2
  • 2
5 Comments
 
LVL 40

Accepted Solution

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

Assisted Solution

by:Rey Obrero
Rey Obrero 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 119

Expert Comment

by:Rey Obrero
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now