我正在通過一個vb腳本運行一個宏,該腳本從數據庫導入超過1000條記錄並將它們複製到Excel表中,然後發送一封電子郵件說明報告已準備就緒。當我輸入較少的記錄(100或200)時,它運行良好。但是,當我導入整個記錄(超過1000個)時,即使程序運行良好,我也會收到一條窗口消息「Microsoft Excel正在等待另一個應用程序完成OLE操作」。Microsoft Excel正在等待另一個應用程序來完成OLE動作
有什麼辦法可以隱藏這條消息。另外,如果我隱藏這條消息,程序是否會繼續運行?下面是我的代碼:
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
Set rst = New ADODB.Recordset
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
cn.Open ("User ID=flt" & _
";Password=flts1hp" & _
";Data Source=SIH_PROD" & _
";Provider=MSDAORA.1" & _
";PLSQLRSet=0")
Set oxcel = New Excel.Application
Set wbk = oxcel.Workbooks.Add()
With oxcel
.ActiveSheet.Name = "Report"
strFileName = "C:\Users\extract.xlsx"
'headings
For i = 0 To rs.Fields.Count - 1
.Cells(row, col) = rs.Fields(i).Name
.Rows(row).RowHeight = 45
.Cells(row, col).WrapText = True
col = col + 1
Next
.Range("A2").Select
.Selection.CopyFromRecordset rs
With wbk
.Application.DisplayAlerts = False
On Error GoTo Error_Message
.SaveAs (strFileName), AccessMode:=xlExclusive, _
ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges
.Close
End With
.Quit
End With
With OutMail
.To = "[email protected]"
.CC = ""
.BCC = ""
.Subject = "Done"
.Body = "Done"
.Send
End With
Set OutApp = Nothing
Set OutMail = Nothing
End Sub
在'.SaveAs(strFileName),AccessMode:= xlExclusive'之後錯過了'_',繼續執行下一行。我添加了它。 –
也許你可以在你的代碼中加入一兩個** [DoEvents](http://msdn.microsoft.com/en-us/library/aa262728(v=vs.60).aspx)**,就像你每次一樣在你的循環中完成一步。我想你正在用這個循環來調用你在這裏粘貼的子文件。 –
@simpLEMAn:只有當應用程序是可視化的基礎時才能使用Doevents。但我的代碼純粹是excel vba ... –