我正在从Access 2010 / Windows 7迁移到Access 2016 / Windows 10.我将SQL Server数据库中的数据读入Excel模板,保存完成的电子表格,然后通过CDO将其邮寄给各种收件人。这在Access 2010 / Windows 7中运行良好多年。
在Access 2016 / Windows 10中,我最初经常得到一个" Excel已停止工作"信息;在保存电子表格之前,访问代码已停在明显随机的位置。如果我设置一个断点并逐步完成代码,一切正常。然后我发现,如果我将代码暂停500毫秒(如下面的缩写代码所示),一切正常(但不会暂停100毫秒)。虽然它现在有效,但这不是一个非常令人满意的解决方案;有更好的吗?
Dim Temp_rs As New ADODB.Recordset
Set ObjXL = New Excel.Application
ObjXL.Visible = False
Set ObjWkb = ObjXL.Workbooks.Open("\\xxx.xxx.xxx\xxxxa\xx\xxxxxxxx\xxxxx\xxxxxxxx\Template.xlsm")
Set ObjSht = ObjWkb.Worksheets("Audit")
ObjSht.Activate
ObjSht.Unprotect ("xxxxxxxx")
'Quite a lot of reading data from various recordsets
mod_OpenMyRecordset Temp_rs, SomeRecordSet
ObjSht.Cells(6, 4) = "Toezegging" & Str(Temp_rs!tzNummer_i)
...etc etc etc
Sleep (500) 'Wait for 0.5 second (necessary for Windows 10!)
ObjSht.Cells(7, 1).Value = " " & SomeValue
ObjSht.Cells(8, 1).Value = " " & SomeValue
ObjSht.Cells(9, 1).Value = " " & SomeValue
ObjSht.PageSetup.RightFooter = "Pagina &P van &N"
ObjSht.EnableSelection = xlUnlockedCells
ObjWkb.Worksheets("Sheet1").Visible = True
ObjSht.Visible = xlSheetVeryHidden
ObjSht.Protect ("xxxxxxxx")
MyPath = "\\xxx.xxx.xxx\xxxxa\xx\xxxxxxxx\xxxxx\xxxxxxxx\xxxxxxxxx" & Trim(ObjSht.Cells(9, 1).Value) & ".xlsm"
ObjWkb.SaveAs MyPath
ObjWkb.Close
Set ObjSht = Nothing
Set ObjWkb = Nothing
ObjXL.Quit
Set ObjXL = Nothing
Dim objMessage As Object, objBodyPart As Object
Set objMessage = CreateObject("CDO.Message")
Set objBodyPart = objMessage.BodyPart
objBodyPart.Charset = "UTF-8"
Set MyObjMessage = CreateObject("CDO.Message")
objMessage.addAttachment MyPath
objMessage.From = SomeValue
objMessage.Subject = SomeValue
objMessage.To = SomeValue
objMessage.Textbody = SomeValue
objMessage.BCC = SomeValue
With objMessage
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = pubMailServer
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Configuration.Fields.Update
.Send
End With
答案 0 :(得分:0)
尝试缩小问题的潜在根源: 1-代码看起来很好并且之前显然正常工作,所以它不太可能成为代码逻辑的问题 2-我一直在移入和移出Access的大量数据,从来没有你的问题,所以我也怀疑它的大小或数量你正在移动(纠正我,如果我错了,它像500,000行x 150个标签...) 3-我发现创建额外的MSO应用程序可能会导致致命/灾难性错误;有没有理由你创建一个新的Excel应用程序,而不仅仅是一个新的工作簿?或者你在Access中运行它,在这种情况下没关系...... 4-你的代码:
ObjSht.Cells(6, 4) = "Toezegging" & Str(Temp_rs!tzNummer_i)
...etc etc etc
稍微有点担心,因为你正在为一个看起来像是大量数据的东西一个一个地直接写入范围......这是非常低效的,并且取决于它的编写方式会耗尽系统内存并导致错误 - 究竟是什么你正在使用这段代码吗?
希望这有帮助, TheSilkCode