打开空的 Excel 电子表格并从 Access VBA 写入它不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4647186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opening empty Excel spreadsheet and writing into it from Access VBA doesn't work
提问by Andrea
I've written an Access 2007 application that opens an empty Excel spreadsheet and writes into it with the following (shortened) VBA code:
我编写了一个 Access 2007 应用程序,它打开一个空的 Excel 电子表格并使用以下(缩短的)VBA 代码写入其中:
Dim Excel_App As Excel.Application
Set Excel_App = CreateObject("Excel.Application")
Excel_App.Visible = True
Excel_App.Workbooks.Add
With Excel_App
.Columns("A:ZZ").ColumnWidth = 25
.Range("A2:ZZ2").VerticalAlignment = xlCenter
.Range("A2:ZZ2").Font.FontStyle = "Bold"
.Range("A" & CStr(iHeadingRows)).Select
.ActiveCell.FormulaR1C1 = "ABC"
End With
The above code works on all installations of Windows (Vista|Xp)/Access 2007 (updated with all patches) I've tested but the customer's one. On his one, the empty Excel spreadsheet gets opened but remains empty when I try to write into it and a runtime error is generated.
上面的代码适用于我测试过的所有 Windows (Vista|Xp)/Access 2007(更新了所有补丁),但客户的。在他的一个,空的 Excel 电子表格被打开,但当我尝试写入它时仍然是空的,并且生成了一个运行时错误。
Is it possible some system policy for the user or some particular setup is blocking the normal behaviour I've tested on the other PCs? And if that's the case, what can be done to fix it in the most unobtrusive way?
用户的某些系统策略或某些特定设置是否可能阻止我在其他 PC 上测试的正常行为?如果是这种情况,可以采取什么措施以最不显眼的方式修复它?
Thanks for any advice!
感谢您的任何建议!
采纳答案by marg
Just so this Question doesn't remain without an answer. Apparently the source of the problem was, that prior Excel Versions only had 256 columns and you tried to access a cell in the column "ZZ".
只是为了这个问题不会没有答案。显然,问题的根源在于,以前的 Excel 版本只有 256 列,而您试图访问“ZZ”列中的单元格。
Here is a solution that should run with all Excel Versions:
这是一个应该与所有 Excel 版本一起运行的解决方案:
Dim Excel_App As Excel.Application
Set Excel_App = CreateObject("Excel.Application")
Excel_App.Visible = True
Excel_App.Workbooks.Add
With Excel_App
.Range("A:A").EntireRow.ColumnWidth = 25
.Range("A2").EntireRow.VerticalAlignment = xlCenter
.Range("A2").EntireRow.Font.FontStyle = "Bold"
.Range("A" & CStr(iHeadingRows)).Select
.ActiveCell.FormulaR1C1 = "ABC"
End With