【发布时间】:2017-12-16 21:43:21
【问题描述】:
我想生成一个无法访问的 Excel 工作簿并设置其格式。文件的创建很容易,但我对格式感到困惑。
文件创建 将 strCurrentDBName 调暗为字符串
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)
格式
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
这是我的错误(运行时错误“1004”:应用程序定义或对象定义错误)
xlsheet1.Range(xl.Selection, xl.Selection.End(xlDown)).Select
xlsheet1.Selection.EntireRow.AutoFit
End With
【问题讨论】: