简体   繁体   English

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

[英]Access Vba - format condition not saved in Excel file (unreadable content)

I'm trying to create an Excel file from an Access database. 我正在尝试从Access数据库创建Excel文件。 I need to create some conditional formatting in the excel file. 我需要在excel文件中创建一些条件格式。 I save the file, but when I re-open the file a warning message Unreadable content is shown and, even repairing, the conditional formatting is lost. 我保存了文件,但是当我重新打开文件时,显示警告消息,显示Unreadable content ,即使进行修复,条件格式也会丢失。

I tried many options and read a lot of posts but none solving this issue. 我尝试了很多选择,并且阅读了很多帖子,但没有解决此问题。

This is involved part of the code: 这是代码的一部分:

'==========================================================================
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

'==========================================================================

My office version is 2010 (ver. 14.0.6112.5000 32bit). 我的办公室版本是2010(版本14.0.6112.5000 32位)。 I tried many saving formats but some of them hang Access (like xlExcel12 ). 我尝试了许多保存格式,但是其中一些挂起了Access(例如xlExcel12 )。 Some of the others give an incompatibility error with extension during saving (like xlExcel8 ). 其他一些在保存过程中给出了扩展名不兼容的错误(例如xlExcel8 )。

Any suggestions? 有什么建议么?

You might want to try xlWorkbookNormal (value -4143 ) as the save format which is the Excel default (explicitly). 您可能要尝试使用xlWorkbookNormal (值-4143 )作为保存格式,这是Excel的默认设置(显式)。

However, I have seen similar issues pop up when you are explicitly referencing Excel object in your code (ie Excel Library 14.0 is selected in the "References" of the VBA project). 但是,当您在代码中显式引用Excel对象时(例如,在VBA项目的“参考”中选择了Excel Library 14.0),我看到了类似的问题弹出。

The solution which has worked for me is to remove the "Excel Library" Reference and late-bound the object instead: 对我有用的解决方案是删除“ 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

The downside to this during development is you will lose intellisence. 在开发过程中这样做的不利之处是您将失去智能。 So what you can do is keep the early-bound objects for development purposes, but comment them out for deployment (and vice-versa): 因此,您可以做的是保留早期绑定的对象用于开发目的,但将它们注释掉以进行部署(反之亦然):

' 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM