Solved

How do I adjust excel column widths when exporting from Access with DoCmd.OutputTo

Posted on 2008-10-12
2
365 Views
Last Modified: 2013-11-27
Hi I am using the following code to export an excel spreadsheet from Access.

DoCmd.OutputTo acOutputQuery, "qry_export", acFormatXLS, rs!state_FK & "-" & rs!Declaration & " Loan Closing " & DatePart("m", dteReport) & "-" & DatePart("d", dteReport) & "-" & DatePart("yyyy", dteReport) & " .xls"

What vba code do I use to make the second column ("B") export with the autofit option so the cell contents aren't truncated?
0
Comment
Question by:BradleyOnTheRoad
2 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 22699686
Since you need to automate Excel to do the column width, I'd just do this:

Dim xlApp As Object, xlWb As Object
Dim rsOut As DAO.Recordset

Set xlApp = CreateObject("Excel.Application")
Set xlWb = xlApp.Workbooks.Add
With xlWb
    Set rsOut = CurrentDb.OpenRecordset("qry_export")
    With .Worksheets(1)
        .[a1].CopyFromRecordset rsOut
        .Columns(2).AutoFit
    End With
    .SaveAs rs!state_FK & "-" & rs!Declaration & " Loan Closing " & Format(dteReport, "yyyy-mm-dd") & ".xls"
    .Close False
End With

Set xlWb = Nothing
xlApp.Quit
Set xlApp = Nothing
0
 

Author Closing Comment

by:BradleyOnTheRoad
ID: 31505514
Thanks. That'll do the trick.
0

Featured Post

Free Trending Threat Insights Every Day

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
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…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

760 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

20 Experts available now in Live!

Get 1:1 Help Now