This is driving me crazy! 
I am instantiating an Excel application from an Access project. All works well until the following code is executed
Code:
Private Sub SlantColumnHeadings()
' MxlsWorksheet.Range(Cells(1, 3), Cells(1, 8)).Select
' MxlsApp.Goto "Activities"
' With Range("Activities")
MxlsWorksheet.Range("$C$1:$I$1").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 60
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
End Sub
Now, this code works on the first, third, fifth, ... executions but fails on the second, fourth, ... executions. If it's relevant, the error code is 1044 if I'm using the Range object/method or otherwise that the Selection is Nothing. You can see the various different ways I have tried from the commented-out statements. All have the same effect. The code resides in an Access module.
Since the code works 50% of the time, I conclude it's OK; there's something else amiss. I cannot believe there is a hidden toggle in the underlying software, so what is different about the even numbered executions? Here's a summary of my actions:
- Software execution OK
- Software fails - reset Access VBA code
- Software execution OK
- Software fails - reset Access VBA code
- ...
So is it the VBA Reset that's clearing whatever prevents the code executing successfully a second time? Has anyone encountered this or similar before and, more important, what was the solution? My Excel Application is Set .. New .. each time and all other Excel objects are set based on the new application so I can't believe it's a carry-over instance that's causing this.
Code:
Set MxlsApp = New Excel.Application
Set MxlsWorkbook = MxlsApp.Workbooks.Add
Set MxlsWorksheet = MxlsApp.Worksheets(1)
Ideas anyone?