Solved

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

Posted on 2008-10-12
2
393 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

830 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