访问 Vba - 格式条件未保存在 Excel 文件中(不可读的内容)



>我正在尝试从 Access 数据库创建一个 Excel 文件。我需要在 excel 文件中创建一些条件格式。我保存了文件,但是当我重新打开文件时,会显示一条警告消息Unreadable content,即使修复,条件格式也会丢失。

我尝试了很多选择并阅读了很多帖子,但没有一个可以解决这个问题。

这是代码的一部分:

'==========================================================================
Dim xl As Excel.Application
Dim wk3 As Workbook
Dim ws3 As Worksheet
Set xl = New Excel.Application   ' Create a excel instance
Set wk3 = xl.Workbooks.Add       ' Create a new workbook
Set ws3 = wk3.Worksheets(1)      ' Add a worksheet to the new wrkbk
...
'
' Add conditional formatting to a range
'
ws3.Range("A1:A10").FormatConditions.Add xlCellValue, xlEqual, "=TRUE"
ws3.Range("A1:A10").FormatConditions(1).Interior.Color = vbGreen
' 
' Save and close file
'
wk3.SaveAs "D:_TOOLSResult.xlsx" ' Save as Excel 2010 file     
wk3.Close False                    ' Close file 
' 
' Close excel and free memory
'
Set wk3 = Nothing
Set ws3 = Nothing
xl.Quit
Set xl = Nothing
'==========================================================================

我的办公版本是 2010(版本 14.0.6112.5000 32 位)。我尝试了许多保存格式,但其中一些挂起了 Access(如xlExcel12)。其他一些在保存期间给出与扩展名不兼容的错误(如xlExcel8)。

有什么建议吗?

您可能想尝试xlWorkbookNormal(值-4143)作为Excel默认值(明确)的保存格式。

但是,当您在代码中显式引用 Excel 对象时,我看到类似的问题弹出(即在 VBA 项目的"参考"中选择了 Excel 库 14.0)。

对我有用的解决方案是删除"Excel 库"引用并后期绑定对象:

Dim xl As Object, wk3 As Object, ws3 As Object
' Create Excel object based on version installed on the machine (late bound).
Set xl = CreateObject("Excel.Application")
' Rest of your code can remain unchanged...
wk3.SaveAs "D:_TOOLSResult.xlsx", -4143 ' -4143 = xlWorkbookNormal

在开发过程中这样做的缺点是您将失去智能。因此,您可以做的是保留早期绑定对象用于开发目的,但将它们注释掉以进行部署(反之亦然):

' Keep these for development.
' These require the Excel Library to be explicitly referenced.
'Dim xl As Excel.Application
'Dim wk3 As Workbook
'Dim ws3 As Worksheet
'Set xl = New Excel.Application   ' Create a excel instance

' Use for deployment.
' Remove the Excel Library reference to use this section.
Dim xl As Object, wk3 As Workbook, ws3 As Worksheet
' Create Excel object based on version installed on the machine (late bound).
Set xl = CreateObject("Excel.Application")

' Rest of your code can remain unchanged...
wk3.SaveAs "D:_TOOLSResult.xlsx", -4143 ' -4143 = xlWorkbookNormal

最新更新