Formatting for Excel workbook exported from Access (1 Viewer)

Ingeneeus

Registered User.
Local time
Today, 12:40
Joined
Jul 29, 2011
Messages
89
Hi --
I've been banging my head up against this all morning. I feel like I'm really close to cracking it, but just can't get the last step.

I've managed to cobble together the code below. It exports the results of my query to an Excel workbook and plants it on my desktop. So far, so good. I've managed to open the file once it's created. I've even managed to set the row height for my results.
Code:
Private Sub CmdRunReport_Click()

'This exports the "Aging Purchse Orders" query to an Excel workbook and retains the original formatting
DoCmd.OutputTo ObjectType:=acOutputQuery, ObjectName:="Aging Purchase Orders", OutputFormat:=acFormatXLSX, OutputFile:="C:\Users\g.birkholz\Desktop\Aging POs " & Format(Date, "mm-dd-yyyy") & ".xlsx"

'This opens up the Excel workbook
Dim xlObj As Object
Set xlObj = CreateObject("excel.application")
xlObj.visible = True
      xlObj.workbooks.Open "C:\Users\g.birkholz\Desktop\Aging POs " & Format(Date, "mm-dd-yyyy") & ".xlsx"
'This sets the height of the data-bearing rows to 50
      xlObj.ActiveSheet.Rows("2:20").RowHeight = 50
    
End Sub
This is where I hit the wall. I want to set the vertical alignment of the rows to Top. I initially tried
Code:
xlObj.ActiveSheet.Rows("2:20").VerticalAlignment = xlTop
but had no luck. I tried
Code:
xlObj.ActiveSheet.Rows("2:20").Select
and then a With Selection, but that wouldn't fly either.

I've since read something about not being able to use xl* constants with Late Binding, which is evidently what I've done here. What I think I understand is that I now need to define a specific constant within the Sub () that will stand in for xlTop, but the water gets a little deep after that. I took a couple of examples I found on various forums and tried to adapt them, but to no luck.

Once again, I turn to you guys for the final piece of the puzzle!
 

Ingeneeus

Registered User.
Local time
Today, 12:40
Joined
Jul 29, 2011
Messages
89
I figured it out! Huzzah!
For anyone else in this situation, here's what I was missing:

Code:
Const xlTp As Long = -4160
      xlObj.ActiveSheet.Rows("2:20").VerticalAlignment = xlTp
It appears to be working like a charm. :)
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:40
Joined
Sep 21, 2011
Messages
14,429
Thanks for that.

I would have recorded a macro in Excel and viewed and amended that code?, but it appears that generates what you first used.
 

Ingeneeus

Registered User.
Local time
Today, 12:40
Joined
Jul 29, 2011
Messages
89
Thanks for that.

I would have recorded a macro in Excel and viewed and amended that code?, but it appears that generates what you first used.

Hi, Gasman --

That's exactly what I did; it's a trick I use often ;)
I was more than a little surprised when it didn't work. It eventually led me down the rabbit hole of "late binding" and having to define the constant for "xlTop"

I think this may be the first time I've ever actually contributed something constructive to this forum! :) Hope it will help somebody out down the road.
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:40
Joined
Sep 21, 2011
Messages
14,429
Well I have just replied to a thread in another forum, mentioning this thread as the o/p was having the same problem with horizontalalignment, so you never know.... :)
 

Users who are viewing this thread

Top Bottom