vb 无法保存 excel 内容
程序运行后,不但无法保存excel文件,另外,程序就没法应了,类似假死。Private Sub Command1_Click()
Form11.WindowState = 1
FileName1 = "test.xlsx"
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application") '判断Excel是否打开
If Err.Number <> 0 Then
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
xlApp.Visible = False '设置EXCEL对象不可见
End If
Err.Clear
If Dir(App.Path & "\" & FileName1) = "" Then '判断文件是否存在
MsgBox App.Path & "\" & FileName1 & "未找到!", vbOKOnly, "友情提示"
Exit Sub
End If
For Each xlBook In xlApp.Workbooks
If xlBook.Name = "test.xlsx" Then
MsgBox "文件已打开!请不要重复打开。", vbOKOnly, "友情提示"
xlBook.Activate
Set xlSheet = xlBook.Worksheets("Sheet1") '设置活动工作表
xlApp.Cells(6, 4) = Form11.Text1.Text
'Exit Sub
Else
Set xlBook = xlApp.Workbooks.Open(App.Path & "\" & FileName1) '打开工件簿文件
xlBook.Activate
Set xlSheet = xlBook.Worksheets("Sheet1") '设置活动工作表
xlApp.Cells(6, 4) = Form11.Text1.Text
End If
Set xlSheet = xlBook.Worksheets("Sheet1") '设置活动工作表
xlSheet.Cells(6, 4) = Form11.Text1.Text
Next
xlApp.DisplayAlerts = False '不显示警告信息
xlBook.Save
xlApp.Quit '结束EXCEL对象
Set xlApp = Nothing '释放xlApp对象
End Sub