我尝试从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:\_TOOLS\Result.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
)。
有什么建议吗?
答案 0 :(得分:0)
您可能希望尝试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:\_TOOLS\Result.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:\_TOOLS\Result.xlsx", -4143 ' -4143 = xlWorkbookNormal