Excel code in access

Dave Eyley

Registered User.
Local time
Today, 18:07
Joined
Sep 5, 2002
Messages
254
I am trying to convert an Excel Macro to apply to a spreadsheet after it's been created and populated from Access.
However, my experience with Excel code is limited and I find lines like -

.Application.HorizontalAlignment = -4108 'xlCenter

come up as 'Object does not support this property or method'

Can anyone suggest what I'm missing here, besides my marbles.

Dave Eyley
 
More code would help :)

assuming you have dimmed the XL app as
Dim appXL As Excel.Application

then try
appXL.HorizontalAlignment = -4108 'xlCenter

If in any module you goto tools>reference... you can set a reference to Excel which should let you use its variables

HTH

Peter
 
Thanks Peter,

I'll try that...

Dave
 
Hi Dave,
Excel and Access have some common & some different VBA instructions. If you want to use an Excel code in Access you should create an Excel application from Access, something like

Dim MyExcelSheet as object
Set MyExcelSheet = CreateObject("Excel.Sheet")

MyExcelSheet.Application.HorizontalAlignment = -4108

But, if what you want is to do Excel VBA orders in Access objects... oops, bad thing. The VBA collections are different, you cannot translate it directly, you'll have to find the instruction and syntax that "do the same function" in VBA Access (and I can advance you that some instructions in VBA Excel have NO EQUIVALENCES in VBA Access and viceversa).
 
I did do something similar -

Dim xlApp As Object
Dim xlSheet As Object

Set xlApp = CreateObject("Excel.Application")
Set xlSheet = xlApp.WorkBooks.Open("D:\PropertyExportByDept.xls").Sheets(1)

With xlApp
.Application.Cells.Select
.Application.Selection.RowHeight = 45
.Application.Rows("1:1").Select

.Application.HorizontalAlignment = -4108 'xlCenter
.Application.VerticalAlignment = -4108
.Application.WrapText = True
.Application.Orientation = 0
.Application.AddIndent = False
.Application.IndentLevel = 0
.Application.ShrinkToFit = False
.Application.ReadingOrder = -5002 'xlContext
.Application.MergeCells = False


And added the Reference - MS Excel 10.0 Object Library but I still get the error saying -

'Object does not support this property or method'

Dave
 
Hi again Dave,
"mergecells", "wraptext", "shrinktofit"... all these instructions concern to CELLS. You have selected a range (the first row) but when you write the instruction you don't mention it has to be applied to the selection, so you're trying to apply these instructions to the "Excel Application", not to the selected row. That's the reason of your error message.
".Selection" doesn't works in Access, as far as I know. What to do:
Change your instructions from

.Application.verticalalignment = -4108
to
.Application.Rows("1:1").verticalalignment = -4108
 
I'm a little bit confused about what you are trying to do. Are you trying to set properties for the whole application, the whole sheet or just row 1...

Application.Rows("1:1").Select

If you're trying to format a range in the current sheet, I would use...

With xlSheet
.Range("1:1").HorizontalAlignment = xlCenter


Whole Sheet...

With xlSheet
.cells.HorizontalAlignment = xlCenter


Give me a little bit of background information and I will try to help you.

Carl
 
Hi Carl,

I was hoping to copy the code from an Excel Macro to format a spreadsheet after I'd created and populated it in Access. Some reports are exported into Excel and then used for other uses by different managers.
So, naively, I cut and pasted the macro from Excel into the Access code where I generated the Spreadsheet and thought that I might just be lucky enough to correct a few lines and end up with a formatted spreadsheet directly from the database.
Silly me, eh?

Having little knowledge of Excel I can see I've bitten off more than I can chew here...that's why I like databases.

I'm sure I can do it, eventually...

This is where I am at the moment... The following code is on a Command button.

If Not IsNothing(Me!Searchstr) Then
DoCmd.SetWarnings False
DoCmd.RunSQL ("SELECT Property.Property, Space_Room_Data.Block, Space_Room_Data.Floor, Space_Room_Data.Rm_No, [Faculties and Depts].FacDiv, [Faculties and Depts].DeptName, Space_Room_Type.TypeDesc, Space_Room_Use.RmUsDesc,Space_Room_Data.Area INTO ExportTableDept FROM Space_Room_Type RIGHT JOIN (Space_Room_Use RIGHT JOIN ([Faculties and Depts] RIGHT JOIN (Property LEFT JOIN Space_Room_Data ON Property.BuildingCode = Space_Room_Data.BuildingCode) ON [Faculties and Depts].Dept_Code = Space_Room_Data.DataDept) ON Space_Room_Use.RmUs_Code = Space_Room_Data.DataUse) ON Space_Room_Type.Type_Code = Space_Room_Data.DataType WHERE " & Searchstr & " ORDER BY Property.Property, Space_Room_Data.Block, Space_Room_Data.Floor, Space_Room_Data.Rm_No;")
DoCmd.SetWarnings True
DoCmd.OutputTo acOutputTable, "ExportTableDept", acFormatXLS, "D:\PropertyExportByDept.xls", False

Dim xlApp As Object
Dim xlSheet As Object

Set xlApp = CreateObject("Excel.Application")
Set xlSheet = xlApp.WorkBooks.Open("D:\PropertyExportByDept.xls").Sheets(1)

With xlApp
.Application.Cells.Select
.Application.Selection.RowHeight = 45
.Application.Rows("1:1").Select

.Application.HorizontalAlignment = -4108 'xlCenter
.Application.VerticalAlignment = -4108
.Application.WrapText = True
.Application.Orientation = 0
.Application.AddIndent = False
.Application.IndentLevel = 0
.Application.ShrinkToFit = False
.Application.ReadingOrder = -5002 'xlContext
.Application.MergeCells = False

.Application.Rows("1:1").Select
.Application.Selection.Font.Bold = True
.Application.Range("D1").Select
.Application.ActiveCell.FormulaR1C1 = "Room No"
.Application.ActiveCell.Characters(START:=1, Length:=7).Font
.Application.FontStyle = "Bold"
.Application.Size = 10
.Application.Strikethrough = False
.Application.Superscript = False
.Application.Subscript = False
.Application.OutlineFont = False
.Application.Shadow = False
.Application.Underline = -4142 'xlUnderlineStyleNone
.Application.ColorIndex = 1

.Application.Range("E1").Select
.Application.ActiveCell.FormulaR1C1 = "Fac/Div"
.Application.ActiveCell.Characters(START:=1, Length:=7).Font
.Application.FontStyle = "Bold"
.Application.Size = 10
.Application.Strikethrough = False
.Application.Superscript = False
.Application.Subscript = False
.Application.OutlineFont = False
.Application.Shadow = False
.Application.Underline = -4142 'xlUnderlineStyleNone
.Application.ColorIndex = 1

.Application.Range("F1").Select
.Application.ActiveCell.FormulaR1C1 = "Dept Name"
.Application.ActiveCell.Characters(START:=1, Length:=9).Font
.Application.Application.FontStyle = "Bold"
.Application.Size = 10
.Application.Strikethrough = False
.Application.Superscript = False
.Application.Subscript = False
.Application.OutlineFont = False
.Application.Shadow = False
.Application.Underline = -4142 'xlUnderlineStyleNone
.Application.ColorIndex = 1

.Application.Range("G1").Select
.Application.ActiveCell.FormulaR1C1 = "Room Type"
.Application.ActiveCell.Characters(START:=1, Length:=9).Font
.Application.FontStyle = "Bold"
.Application.Size = 10
.Application.Strikethrough = False
.Application.Superscript = False
.Application.Subscript = False
.Application.OutlineFont = False
.Application.Shadow = False
.Application.Underline = -4142 'xlUnderlineStyleNone
.Application.ColorIndex = 1

.Application.Range("H1").Select
.Application.ActiveCell.FormulaR1C1 = "Room Use"
.Application.ActiveCell.Characters(START:=1, Length:=8).Font
.Application.FontStyle = "Bold"
.Application.Size = 10
.Application.Strikethrough = False
.Application.Superscript = False
.Application.Subscript = False
.Application.OutlineFont = False
.Application.Shadow = False
.Application.Underline = -4142 'xlUnderlineStyleNone
.Application.ColorIndex = 1

.Application.Columns("I:I").Select
.Application.Selection.NumberFormat = "0.00"
.Application.Columns("A:I").Select
.Application.ActiveSheet.PageSetup.PrintArea = "$A:$I"
.Application.ActiveSheet.PageSetup
.Application.PrintTitleRows = "$1:$1"
.Application.PrintTitleColumns = ""

.Application.ActiveSheet.PageSetup.PrintArea = "$A:$I"
.Application.ActiveSheet.PageSetup
.Application.LeftHeader = ""
.Application.CenterHeader = "&""Arial,Bold""&12Faculty Space&""Arial,Regular""&10 " & Chr(10) & ""
.Application.RightHeader = ""
.Application.LeftFooter = "&D"
.Application.CenterFooter = ""
.Application.RightFooter = ""
.Application.LeftMargin = Application.InchesToPoints(0.75)
.Application.RightMargin = Application.InchesToPoints(0.75)
.Application.TopMargin = Application.InchesToPoints(1)
.Application.BottomMargin = Application.InchesToPoints(1)
.Application.HeaderMargin = Application.InchesToPoints(0.5)
.Application.FooterMargin = Application.InchesToPoints(0.5)
.Application.PrintHeadings = False
.Application.PrintGridlines = False
.Application.PrintComments = -4142 'xlPrintNoComments
.Application.PrintQuality = 600
.Application.CenterHorizontally = False
.Application.CenterVertically = False
.Application.Orientation = 2 'xlLandscape
.Application.Draft = False
.Application.PaperSize = 9 'xlPaperA4
.Application.FirstPageNumber = -4105 'xlAutomatic
.Application.Order = 1 'xlDownThenOver
.Application.BlackAndWhite = False
.Application.Zoom = False
.Application.FitToPagesWide = 1
.Application.FitToPagesTall = 363
.Application.PrintErrors = 0 'xlPrintErrorsDisplayed

End With
Set xlApp = Nothing
Set xlSheet = Nothing
xlApp.ActiveWorkBook.Save
xlApp.ActiveWorkBook.Close
xlApp.Quit

MsgBox "The data has been exported into a spreadsheet." & Chr$(10) & Chr$(10) & "The Spreadsheet is called 'PropertyExportbyDept.xls' and will be in your local D: directory"
Else
MsgBox "No Search criteria set - Go back and make selections", vbExclamation, "No Search Criteria"
End If


It's a mess, I know. I've been trying it from different angles and advice and search results on the forum so the Macro doesn't look like it used to...

Any help would gratefully appreciated. But I'm asking a lot, I know.

Dave
 
With xlApp
.Application.Cells.Select

Try it without the .Application. In effect you have
Application.Application.Cells.Select

With xlApp
.Cells.Select


Peter
 
Thanks to both Carl and Peter I now have a formatted spreadsheet with data as a single button operation. I didn't think I'd get there but it worked.

Thanks again..

Dave Eyley
 
OK, spoke too soon...

It works fine once but gives an error on the second attempt. I have found that in the task list Excel is running as a process when it shouldn't be. If I end the task then the spreadsheet creation and format works ok...

I thought I closed the spreadsheet by including -

xlApp.ActiveWorkBook.Save
xlApp.ActiveWorkBook.Close
xlApp.Quit


So what could the problem be?

Dave
 
Well, I've tried every combination of Set xlApp = Nothing, Set xlSheet = Nothing, Quit, Close and Save and I still get the problem...

What is the correct method for ending a Excel application from Access?
And saving the changes, of course...

At the moment it leaves Excel as a process in Windows Task List which stops the code running more than once. Also, there's no evidence on the desktop or tasbar that there is any Excel spreadsheet open.

And... (this is getting too much I know)

When I open the spreadsheet it tells me that it's save in Excel 95 format and do I want to change it to the latest version?
I am using the latest version of Office for both Excel and Access so I don't understand...

Dave
 
You could try changing

Dim xlApp As Object
Dim xlSheet As Object

Set xlApp = CreateObject("Excel.Application")

to
Dim xlApp As Excel.Application
Dim xlSheet As Excel.Worksheet

Set xlApp = New Excel.Application

This may help with the version but dont hold me to it:)

I will have to think about the other bit!

Peter
 
I think I see where the problem might be...

I am using OutputTo to create the unformatted Spreadsheet called -

"D:\PropertyExportByDept"

Then, I'm using the routine (ex-macro) to format it.
Would it be that the first action is interfering with the second and causing Excel to hang?

Is there a way to create the Spreadsheet first using -

Dim xlApp As Excel.Application
Dim xlSheet As Excel.Worksheet

Set xlApp = New Excel.Application


Then, while the Excel App is open, apply the formatting?

Just a thought...

Dave
 
Try this for you closing down problem

xlApp.ActiveWorkBook.Save
xlApp.ActiveWorkBook.Close
xlApp.UserControl = False
xlApp.Quit
Set xlApp = Nothing
 
You could try TransferSpreadsheet rather than OutputTo

if that dont work then you could use code to loop though you data and poke it into XL but that could be slower.

Peter
 
TransferSpreadsheet is the way I would go.

This is how I normally do the Excel bit...

I use ExcelSheet because this is where most of the action will take place at sheet level.

Looking at your code, I would have written it like this...


Dim appExcel As New Excel.Application
Dim ExcelBook As Excel.Workbook
Dim ExcelSheet As Excel.Worksheet

Set ExcelBook = appExcel.Workbooks.Open(Filename _
, , False)

Set ExcelSheet = ExcelBook.worksheets(1)


With ExcelSheet
.Cells.RowHeight = 45

With .Range("1:1")
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = -5002 'xlContext
.MergeCells = False
.Font.Bold = True
End With

.Range("D1").value = "Room No"
.Range("E1").value = "Fac/Div"
.Range("F1").value = "Dept Name"
.Range("G1").value = "Room Type"
.Range("H1").value = "Room Use"

With .Range("D1:H1").Font
.Bold = True
.Size = 10
.ColorIndex = 1
End With

.Columns("I:I").NumberFormat = "#0.00"

.Columns("A:I").Select
With .PageSetUp
.PrintArea = "$A:$I"
.PrintTitleRows = "$1:$1"
.PrintTitleColumns = ""
.LeftHeader = ""
.CenterHeader = "&""Arial,Bold""&12Faculty Space&""Arial,Regular""&10 " & Chr(10) & ""
.RightHeader = ""
.LeftFooter = "&D"
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.75)
.RightMargin = Application.InchesToPoints(0.75)
.TopMargin = Application.InchesToPoints(1)
.BottomMargin = Application.InchesToPoints(1)
.HeaderMargin = Application.InchesToPoints(0.5)
.FooterMargin = Application.InchesToPoints(0.5)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 363
.PrintErrors =xlPrintErrorsDisplayed
End With
End With

ExcelBook.Close True
Set appExcel = Nothing[/B]

OK 2 things. I haven't got a copy of Access on my PC (will have by next Monday) so this code is off the top of my head so the code might need a tiny bit of work here and there. Secondly, I'm not sure how much of the page setup bit you need. I think a load of the properties are defaults and you can leave them out. Only leave in what you actually need.

Hope this helps.

Take Care.

Carl
 

Users who are viewing this thread

Back
Top Bottom