【问题标题】:Excel Application Property using in Access VBA在 Access VBA 中使用 Excel 应用程序属性
【发布时间】: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

【问题讨论】:

    标签: excel ms-access vba


    【解决方案1】:

    您正在使用 xlDown 枚举值,它需要引用 Microsoft Excel 对象库。由于您使用的是后期绑定,因此可能未设置该引用。

    使用xlDown, -4121 的值解决它:

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

    请注意,如果您将 Option Explicit 放在模块顶部,则更容易发现此错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-09
      • 1970-01-01
      • 2011-04-07
      • 2020-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多