Export to an Excel-sheet, AUTOSIZING all the colums. (1 Viewer)

Luc Ringoir

New member
Local time
Today, 03:33
Joined
Jun 14, 2010
Messages
1
HI

I open an Excel-sheet with a line Access-code

DoCmd.OutputTo acOutputQuery, "qryPlanningOnLine", "ExcelWorkbook(*.xlsx)", “C:\Planning\Planning.xlsx”, True

and it’s working fine.

However I would open this Excel-sheet, AUTOSIZING all the colums.

Is it possible?
 

Rx_

Nothing In Moderation
Local time
Yesterday, 19:33
Joined
Oct 22, 2009
Messages
2,803
For general moving Access into Excel with code - this is still my favorite:
http://www.btabdevelopment.com/ts/default.aspx?PageId=10

Once the Excel document is open
Right click in the Ribbon (menu) and Customize the Ribbon
On the Right-Hand list box (Customize the Ribbon:)
the Developer box is unchecked - check it
Now the menu has "Developer"
When Developer tab is open, choose Record Macro
On the upper Left-Hand cell (just above 1 and just to the left of "A") choose
This selects the entire Worksheet
Put the mouse cursor just between the line between A | B the cursor will change to show the line is chosen - Double Click
Now, Stop Recording (menu)
Double click on the Macro box - Sub Macro1 will open and display
Cells.Select
Cells.EntireColumn.AutoFit

While the Macro Recorder won't take care of advanced programming, it will provide basic guidance for the Excel Object, Properties, and Methods.

This code can typically be placed back into the Access VBA. It will just need the object reference to Excel to run from there.

Example:
Dim MyExcel as Excel.Application
See code link above to open excel from Access and then run this extra:

MyExcel.Cells.Select
MyExcel.Cells.EntireColumn.AutoFit

I didn't mean to steal any thunder. They just gave me a new Xenon computer with SSD Hard drive, so I am re-setting up all of my Office applications. Including the Menu item for Excel Developer.
The Company Web Troll Monitor now blocks MZTools. Lucky I kept the last download. For a while, I was wondering if I could get to Bob Larson's site mentioned above.
 

vbaInet

AWF VIP
Local time
Today, 02:33
Joined
Jan 22, 2010
Messages
26,374
While the Macro Recorder won't take care of advanced programming, it will provide basic guidance for the Excel Object, Properties, and Methods.
... and it never gives optimised code, for example the Select is not necessary.
 

Luciano

Registered User.
Local time
Today, 03:33
Joined
Jul 29, 2007
Messages
25
Hi
The following code is working fine:

Dim objXl As Object
Dim objActiveWkb As Object
DoCmd.OutputTo acOutputQuery, ”qryPlanningOnLine” , "ExcelWorkbook(*.xlsx)" , “C:\Planning\Planning.xlsx” , False
Set objXl = CreateObject("Excel.Application")
Set objActiveWkb = objXl.Workbooks.Open(“C:\Planning\Planning.xlsx”)
objActiveWkb.ActiveSheet.Cells.EntireColumn.AutoFit
objActiveWkb.Close SaveChanges:=True
objXl.Workbooks.Open (“C:\Planning\Planning.xlsx”)
objXl.Visible = True
 

namliam

The Mailman - AWF VIP
Local time
Today, 03:33
Joined
Aug 11, 2003
Messages
11,695
Why would you, open, save, close, open ???

Why not stick to Open, Save ??

Did you add
Set objXl = Nothing
Set objActiveWkb = Nothing
To clean up your varaibles?
 

Luciano

Registered User.
Local time
Today, 03:33
Joined
Jul 29, 2007
Messages
25
Hi namliam

je hebt inderdaad overschot van gelijk, 'k heb mijn code aangepast en dit geeft een beter en sneller resultaat.
Thanks
DoCmd.OutputTo acOutputQuery, "qryPlanningOnLine, "ExcelWorkbook(*.xlsx)", "C:\Planning\Planning.xlsx", False
Dim objXl As Object
Dim objActiveWkb As Object
Set objXl = CreateObject("Excel.Application")
Set objActiveWkb = objXl.Workbooks.Open("C:\Planning\Planning.xlsx")
objActiveWkb.ActiveSheet.Cells.EntireColumn.AutoFit
objXl.Visible = True
Set objXl = Nothing
Set objActiveWkb = Nothing
 

namliam

The Mailman - AWF VIP
Local time
Today, 03:33
Joined
Aug 11, 2003
Messages
11,695
Google translate fails :)

Though you are no longer saving your excel sheet after your Autofit, thus if the user closes it without saving, the autofit is undone.
 

Luciano

Registered User.
Local time
Today, 03:33
Joined
Jul 29, 2007
Messages
25
How saving the Excel-file before openeing ?
 

namliam

The Mailman - AWF VIP
Local time
Today, 03:33
Joined
Aug 11, 2003
Messages
11,695
objActiveWkb.Save

Or something simular I think
 

Users who are viewing this thread

Top Bottom