通过Access 2013 VBA编辑后无法打开Excel 2013文件

时间:2014-04-10 09:28:43

标签: excel vba ms-access excel-vba

我正在开发一个Access数据库,我有一个按钮,当点击它时,复制一个文件(模板excel文件),然后打开新副本,设置几个单元格的值,然后拯救和关闭它。另一个按钮完全相同,但之后调用另一个子来打印新文件。

我遇到的问题是,我无法在Excel中打开这些已编辑的文件。它们通过访问VBA代码打印没有任何问题,这意味着它会被编辑,保存,然后打印。但是,当我双击其中一个文件时,excel启动但文件根本没有打开。没有桌子。如果我已在Excel中打开文件,右键单击其中一个文件并选择打印,则会打印以前的Excel工作表。

这里是Acess VBA sub,它复制模板,然后继续编辑副本:

Private Sub SaveXLFile(FileNameFull As String)
'FileNameFull: Complete path for file to be saved, including extension.

Dim ExcelApp As Excel.Application
Dim ExcelBook As Excel.Workbook
Dim ExcelSheet As Excel.Worksheet

Dim fso As Object

'copy template
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
fso.CopyFile ThePath & "Arbetsorder - Blankett.xlsx", FileNameFull


Set ExcelBook = GetObject(FileNameFull)
Set ExcelApp = ExcelBook.Parent
Set ExcelSheet = ExcelBook.Worksheets(1)
'we are open and ready for action!


'take values from form and put them in the correct places
ExcelSheet.Range("C2").Value = [Kund]

'edit page 2
'Set ExcelSheet = ExcelBook.Worksheets(2)
'ExcelSheet.Range("B31").Value = [TB_Tid_Ställ]


ExcelBook.Close savechanges:=True
Set ExcelSheet = Nothing
Set ExcelBook = Nothing
Set ExcelApp = Nothing
End Sub

打印代码如下:

Private Sub PrintXLFile(FileNameFull As String)
'opens an excel file and prints it.
'FileNameFull: Complete path to file, including extension.
Dim ExcelApp As Excel.Application
Dim ExcelBook As Excel.Workbook
Dim ExcelSheet As Excel.Worksheet

Set ExcelBook = GetObject(FileNameFull)
Set ExcelApp = ExcelBook.Parent
Set ExcelSheet = ExcelBook.Worksheets(1)

ExcelSheet.PrintOut Copies:=1, Collate:=True
ExcelBook.Sheets(2).PrintOut Copies:=1, Collate:=True

ExcelBook.Close savechanges:=False 'close without changes
Set ExcelSheet = Nothing
Set ExcelBook = Nothing
Set ExcelApp = Nothing
End Sub

...实际按钮点击的子项不以任何方式显示阴影:

Private Sub KnappPrint_Click()
Dim NewFileNameWExt As String

NewFileNameWExt = [I-Order] & ".xlsx"
SaveXLFile (ThePath & "Arbetsordrar\" & NewFileNameWExt)
PrintXLFile (ThePath & "Arbetsordrar\" & NewFileNameWExt)
End Sub

我做错了什么?我试过只编辑第一张纸,以防我在切换纸张时出错了,但这并没有解决它。

更令人困惑的是,它在运行KnappPrint_Click()时打印正常,但在资源管理器中右键单击该文件并选择“打印”不执行任何操作。

1 个答案:

答案 0 :(得分:0)

您的工作表出现问题的原因是您打开工作簿的方式。尝试将Excel应用和工作簿分配更改为:

Set ExcelApp = CreateObject("Excel.Application")
Set ExcelBook = ExcelApp.Workbooks.Open(str)
Set ExcelSheet = ExcelBook.Worksheets(1)