主题:[讨论]请教解决vb创建excel文档出现“是否保存对“book1”的更改”提示的方法
创建了一个excel文档,当excel软件已打开时,不会提示需另存为“book1.xlsx”文件,当excel软件关闭时,提示“是否保存对“book1”的更改?”,如何做到在excel软件关闭时能自动按设定路径保存,而不提示是否保存对“book1”的更改?代码如下:
Public Sub Form_Load()
Set NewxlApp = New Excel.Application
Set NewxlApp = GetObject("excel.application")
If Err Then
Err.Clear
Set NewxlApp = CreateObject("Excel.Application")
If Err Then
MsgBox Err.Description
Exit Sub
End If
End If
NewxlApp.Visible = False
Set xlBook5 = NewxlApp.Workbooks.Add
End Sub
Private Sub Form_Unload(Cancel As Integer)
xlBook5.SaveAs (“d:\”+ "V2.xls")
xlBook5.Close
NewxlApp.Quit
Set NewxlApp = Nothing
Set xlBook5 = Nothing
Set xlSheet5 = Nothing
End Sub
Private Sub cmd_Click()
Set xlSheet5 = xlBook5.Worksheets(1)
xlSheet5.Activate
xlSheet5.Cells(1, 1) = 1
Set xlApp = GetObject(, "excel.application")
If Err Then
Err.Clear
Set xlApp = CreateObject("Excel.Application")
If Err Then
MsgBox Err.Description
Exit Sub
End If
End If
xlApp.Visible = False
Set xlBook1 = xlApp.Workbooks.Open(“d:\” + "V3.xls")
Set xlSheet1 = xlBook1.Worksheets(1)
xlSheet1.Activate
P= xlSheet1.Cells(1, 8)
xlBook1.Close (True)
100 xlApp.Quit
Set xlApp = Nothing
End Sub
在运行到cmd_Click()中的100 xlApp.Quit 提示“是否保存对“book1”的更改?”。由于牵涉到两个Excel.Application,一个在unload中退出,估计是xlapp这里退出时,影响了NewxlApp,不知如何解决?
由于实际所需,需要如此设定Excel.Application,不能把unload中的代码移到cmd_click中。
Public Sub Form_Load()
Set NewxlApp = New Excel.Application
Set NewxlApp = GetObject("excel.application")
If Err Then
Err.Clear
Set NewxlApp = CreateObject("Excel.Application")
If Err Then
MsgBox Err.Description
Exit Sub
End If
End If
NewxlApp.Visible = False
Set xlBook5 = NewxlApp.Workbooks.Add
End Sub
Private Sub Form_Unload(Cancel As Integer)
xlBook5.SaveAs (“d:\”+ "V2.xls")
xlBook5.Close
NewxlApp.Quit
Set NewxlApp = Nothing
Set xlBook5 = Nothing
Set xlSheet5 = Nothing
End Sub
Private Sub cmd_Click()
Set xlSheet5 = xlBook5.Worksheets(1)
xlSheet5.Activate
xlSheet5.Cells(1, 1) = 1
Set xlApp = GetObject(, "excel.application")
If Err Then
Err.Clear
Set xlApp = CreateObject("Excel.Application")
If Err Then
MsgBox Err.Description
Exit Sub
End If
End If
xlApp.Visible = False
Set xlBook1 = xlApp.Workbooks.Open(“d:\” + "V3.xls")
Set xlSheet1 = xlBook1.Worksheets(1)
xlSheet1.Activate
P= xlSheet1.Cells(1, 8)
xlBook1.Close (True)
100 xlApp.Quit
Set xlApp = Nothing
End Sub
在运行到cmd_Click()中的100 xlApp.Quit 提示“是否保存对“book1”的更改?”。由于牵涉到两个Excel.Application,一个在unload中退出,估计是xlapp这里退出时,影响了NewxlApp,不知如何解决?
由于实际所需,需要如此设定Excel.Application,不能把unload中的代码移到cmd_click中。