2017-12-16 2 views
0

Ich möchte eine Excel-Arbeitsmappe außerhalb des Zugriffs generieren und formatieren. Die Erstellung der Datei ist einfach, aber ich habe Schwierigkeiten mit dem Format.Excel-Anwendungseigenschaft, die in Access VBA verwendet

Dateierstellung Dim strCurrentDBName As String

strCurrentDBName = CurrentDb.Name 
For i = Len(strCurrentDBName) To 1 Step -1 
    If Mid(strCurrentDBName, i, 1) = "\" Then 
     strPath = Left(strCurrentDBName, i) 
     Exit For 
    End If 
Next 
xlsxPath = strPath & "Report.xlsx" 

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "Report", xlsxPath, True 

MsgBox ("Report generated. " & xlsxPath) 

Format

Dim xl As Object 
'This deals with Excel already being open or not 
On Error Resume Next 
Set xl = GetObject(, "Excel.Application") 
On Error GoTo 0 
If xl Is Nothing Then 
    Set xl = CreateObject("Excel.Application") 
End If 

Set XlBook = GetObject(xlsxPath) 
'filename is the string with the link to the file ("C:/....blahblah.xls") 

'Make sure excel is visible on the screen 
xl.Visible = True 
XlBook.Windows(1).Visible = True 
'xl.ActiveWindow.Zoom = 75 

'Define the sheet in the Workbook as XlSheet 
Set xlsheet1 = XlBook.Worksheets(1) 

'Format 
With xlsheet1 
    xlsheet1.Rows("1:1").Select 

und hier mein Fehler (Laufzeitfehler '1004': Anwendungs ​​definiert oder objektdefinierter Fehler)

xlsheet1.Range(xl.Selection, xl.Selection.End(xlDown)).Select 
    xlsheet1.Selection.EntireRow.AutoFit 

End With 

Antwort

1

Sie verwenden den Enumerationswert xlDown, der erforderlich ist bietet einen Verweis auf die Microsoft Excel-Objektbibliothek. Da Sie späte Bindungen verwenden, ist diese Referenz wahrscheinlich nicht gesetzt.

Umgehen durch den Wert von xlDown verwenden, -4121:

xlsheet1.Range(xl.Selection, xl.Selection.End(-4121)).Select 

Beachten Sie, dass dieser Fehler leichter gewesen wäre zu erkennen, wenn Sie Option Explicit an der Spitze des Moduls gelegt hatten.