Solved

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

Posted on 2013-05-10
5
2,260 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 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
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…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

828 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