如何停止打开excel表以及excel书 [英] How to stop opening an excel sheet as well as an excel book
问题描述
我想打开一个excel工作簿作为打印数据的渠道。
我使用以下代码;
I want to open an excel workbook as a conduit to print out data.
I use the following code;
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
oXL = CreateObject("Excel.Application")
If oXL Is Nothing Then
MessageBox.Show("Excel is not properly installed!!")
Return
End If
oXL.Visible = True
oXL.UserControl = False ' How to stop excelclosing immediately
'create workbook
oWB = oXL.Workbooks.Add
'select the active worksheet
oSheet = oWB.ActiveSheet
' add the code to populate the form
' then print the excel sheet and close excel
PrintForm1.PrinterSettings.PrinterName = "Microsoft Print to PDF"
If PrintForm1.PrinterSettings.IsValid Then
PrinterName = PrintForm1.PrinterSettings.PrinterName
oSheet.PrintOut(From:=1, To:=1, Copies:=1, ActivePrinter:=PrinterName, Collate:=True, Preview:=True)
'Make sure that you release object references.
oRng = Nothing
oSheet = Nothing
oWB.Close(SaveChanges:=False)
oXL.Quit()
oXL = Nothing
Else
MessageBox.Show("Microsoft Print to PDF printer not set up!")
End If
代码成功打印出所需的表格。
问题是单页表格excel文档是由
The code successfully prints out the required form.
The problem is that a singles sheet excel document was generated by
oXL = CreateObject("Excel.Application")
然后工作簿由
and then a workbook is generated by
oWB = oXL.Workbooks.Add
然后当我关闭Excel时,首先使用
Then when I come to closing Excel, firstly,using
oWB.Close(SaveChanges:=False)
关闭工作簿而不保存。然后使用
to close the workbook without saving. Then using
oXL.Quit()
关闭Excel但是我被问到是否要保存Sheet Excel文件。
如何停止打开单张Excel表格,或者如何在不保存的情况下关闭它?
我尝试过:
我试图在互联网上查找信息以排序问题但失败了。
to close Excel BUT then I am asked if I want to save the Sheet Excel file.
How do I stop the single Excel Sheet being opened or alternately how do I close it without saving?
What I have tried:
I have tried to find info on the internet to sort the problem but failed.
推荐答案
您要求保存Excel文件的原因是负责关闭工作簿和释放资源的那段代码在If - End if
语句中,但必须是在它之外。
The reason you're asking about saving Excel file is that that piece of code responsible for closing workbook and releasing resources is insideIf - End if
statement, but it have to be outside of it.
If PrintForm1.PrinterSettings.IsValid Then
PrinterName = PrintForm1.PrinterSettings.PrinterName
oSheet.PrintOut(From:=1, To:=1, Copies:=1, ActivePrinter:=PrinterName, Collate:=True, Preview:=True)
'Make sure that you release object references.
oRng = Nothing
oSheet = Nothing
oWB.Close(SaveChanges:=False)
oXL.Quit()
oXL = Nothing
Else
MessageBox.Show("Microsoft Print to PDF printer not set up!")
End If
'Make sure that you release object references.
oRng = Nothing
oSheet = Nothing
oWB.Close(SaveChanges:=False)
oXL.Quit()
oXL = Nothing
这篇关于如何停止打开excel表以及excel书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!