DoEvents是干什么用的?

本文介绍了DoEvents在程序中的作用,即通过适时地将控制权交还给操作系统,实现窗口内容的及时更新,避免程序因长时间占用而不响应的问题。通过具体示例展示了DoEvents如何改善用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   把控制权交给操作系统,以响应窗口重画、最大化、最小化等要求,避免出现应用程序不响应操作系统请求而被操作系统误以为死机了。
   举个例子:在窗口中放一个textbox,然后写程序,循环从1到10万,然后循环体里面就是把这个数写到textbox里面,如果没有DoEvents,程序运行的时候就是死机一样,然后直到最后窗口显示10万,中间数字什么也看不见;如果在每次写了textbox以后,都来一下DoEvents,窗口就会重画,运行的时候就能看见textbox里面的数一个一个的长上去。
检查以下下面的VB代码,无法实现复制是为什么Sub ExportToWord() On Error GoTo ErrorHandler Dim wdApp As Object Dim wdDoc As Object Dim wdSlide As Object Dim wdShape As Object Dim ws As Worksheet Dim rowCount As Long Dim row As Long Dim slideIndex As Integer Dim colIndex As Integer Dim title As String Dim text As String Dim cellValue As String ' Dim picCount As Integer ' Dim textCount As Integer ' Dim animationSettings As Object ' Dim OldLeft As Single, OldTop As Single ' Dim OldWidth As Single, OldHeight As Single ' Dim hasAnimation As Boolean ' Dim i As Integer ' ' 新增:存储原图片ID和新图片对象的字典 ' Dim originalShapeId As Long ' Dim shapeMapping As Object Dim findRange As Object ' 查找原组"NQA"的范围 Dim startPos As Long, endPos As Long ' 原组的起始和结束位置 Dim originalGroup As Object ' 原组文本范围 Dim newGroup As Object ' 复制后的新组 Dim offsetPara As Integer ' 新组与原组的段落间隔(控制距离) Dim isFirst As Boolean ' 判断是否是第一次查找 Dim firstGroup As Object ' 存储原组范围的集合 Dim Response As Integer ' 错误处理用变量(原代码缺失) ' 设置工作表 Set ws = ActiveSheet rowCount = ws.Cells(ws.Rows.Count, "A").End(xlUp).row offsetPara = 2 Set firstGroup = CreateObject("Scripting.Dictionary") ' 打开PowerPoint应用程序 Set wdApp = CreateObject("Word.Application") wdApp.Visible = True wdApp.Activate ' 强制激活Word窗口,确保操作被响应 ' 让用户选择PowerPoint文件 With Application.FileDialog(msoFileDialogOpen) .Filters.Clear .Filters.Add "Word Files", "*.docx; *.doc", 1 If .Show = -1 Then Set wdDoc = wdApp.Documents.Open(.SelectedItems(1)) Else MsgBox "未选择PowerPoint文件。操作已取消。" Exit Sub End If End With ' 3. 定位原组"NQA"的文本范围(从第一个"N"到最后一个"A") Set findRange = wdDoc.Content isFirst = True ' 查找"N"确定起始位置 With findRange.Find .text = "题号NO" ' 原组的起始标记(可根据实际文本调整,如"题号N") .Forward = True .Wrap = 1 ' wdFindContinue:查找整个文档 .MatchCase = False ' 不区分大小写 .MatchWholeWord = True ' 精确匹配整个标记 .Execute If .Found Then startPos = findRange.Start ' 记录"N"的起始位置 Else MsgBox "未找到标记'N',无法确定原组范围!" wdApp.Quit Exit Sub End If End With ' 查找"A"确定结束位置(原组的结束标记) findRange.SetRange startPos, wdDoc.Content.End ' 从"N"位置向后查找 With findRange.Find .text = "答案解析A" ' 原组的结束标记(可根据实际文本调整,如"解析A") .Forward = True .Wrap = 1 ' wdFindContinue:查找整个文档 .MatchCase = False ' 不区分大小写 .MatchWholeWord = True ' 精确匹配整个标记 .Execute If .Found Then endPos = findRange.End ' 记录"A"的结束位置 Else MsgBox "未找到标记'A',无法确定原组范围!" wdApp.Quit Exit Sub End If End With ' 定义原组"NQA"的完整范围(从"N"开始到"A"结束) Set originalGroup = wdDoc.Range(startPos, endPos) If originalGroup.text = "" Then MsgBox "原组范围为空,无法复制!" wdApp.Quit Exit Sub ElseIf originalGroup.text <> "" Then MsgBox "原组有值" End If ' 从第二页开始插入数据 'slideIndex = 2 'For row = 2 To rowCount For row = 2 To 4 'If UCase(ws.Cells(row, 1).Value) = "Y" Then ' 检查是否需要添加新的幻灯片 'If wdPress.Slides.Count < slideIndex Then ' 使用Duplicate方法复制第一张幻灯片 'Set wdSlide = wdPress.Slides(1).Duplicate 'wdSlide.MoveTo slideIndex '确保上个操作做完替换Application.Wait Now强制停留 'DoEvents ' 稍等1秒,防止复制粘贴出错 '上一版本(刘)会停留一秒,但插入图片太慢了现在去掉了 'Application.Wait Now + TimeValue("00:00:01") 'Else 'Set wdSlide = wdPress.Slides(slideIndex) 'End If 'Set shapeMapping = CreateObject("Scripting.Dictionary") '计算非图片图形的数量 ' 复制原组并粘贴到文档末尾 originalGroup.Copy ' 复制原组文本(含格式) ' 移动光标到文档末尾(关键:确保粘贴位置正确) wdDoc.Content.End.Select ' 选中末尾,激活粘贴位置 wdDoc.Content.End.InsertParagraphAfter wdDoc.Content.End.InsertParagraphAfter ' 在末尾插入空段落 wdDoc.Content.End.Paste ' 粘贴新组 DoEvents ' textCount = 0 ' ' 循环遍历幻灯片中的每个形状 ' 'For Each wdShape In wdDoc.Shapes ' ' ' If wdShape.HasTextFrame Then ' textCount = textCount + 1 ' text = wdShape.TextFrame.TextRange.text ' text = UCase(text) ' 转换为大写以不区分大小写 ' ' ' 清空原有文本框内容 ' wdShape.TextFrame.TextRange.text = "" ' ' ' 循环检查工作表的每一列 ' Dim textToAdd As String ' textToAddN = "" ' textToAddQ = "" ' textToAddA = "" ' For colIndex = 1 To ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column ' title = UCase(ws.Cells(1, colIndex).Value) ' cellValue = ws.Cells(row, colIndex).Value ' ' ' 如果列标题包含标记且单元格不为空,则准备添加数据 ' If title Like "*NO*" And cellValue <> "" Then ' textToAddN = textToAddN & cellValue & "." ' ElseIf title Like "*题干*" And cellValue <> "" Then ' textToAddQ = textToAddQ & cellValue & vbLf ' ElseIf title Like "*选项*" And cellValue <> "" Then ' textToAddQ = textToAddQ & cellValue & vbLf ' ElseIf title Like "*答案*" And cellValue <> "" Then ' textToAddA = textToAddA & cellValue & vbLf ' ElseIf title Like "*解析*" And cellValue <> "" Then ' textToAddA = textToAddA & cellValue & vbLf ' End If ' Next colIndex ' ' ' 如果添加的文本最后有换行符,则移除它 ' If Len(textToAddN) > 0 And Right(textToAddN, Len(vbCrLf)) = vbCrLf Then ' textToAddN = Left(textToAddN, Len(textToAddN) - Len(vbCrLf)) ' End If ' If Len(textToAddQ) > 0 And Right(textToAddQ, Len(vbCrLf)) = vbCrLf Then ' textToAddQ = Left(textToAddQ, Len(textToAddQ) - Len(vbCrLf)) ' End If ' If Len(textToAddA) > 0 And Right(textToAddA, Len(vbCrLf)) = vbCrLf Then ' textToAddA = Left(textToAddA, Len(textToAddA) - Len(vbCrLf)) ' End If ' ' ' 将处理过的文本添加到文本框中 ' 'wdShape.TextFrame.TextRange.text = textToAdd ' With wdDoc.Content.Find ' .text = "题号NO" ' .Replacement.text = textToAddN ' .Format = False ' .Execute Replace:=2 ' End With ' With wdDoc.Content.Find ' .text = "题干选项Q" ' .Replacement.text = textToAddQ ' .Format = False ' .Execute Replace:=2 ' End With ' With wdDoc.Content.Find ' .text = "答案解析A" ' .Replacement.text = textToAddA ' .Format = False ' .Execute Replace:=2 ' End With ' End If 'Next wdShape 'DoEvents ' 稍等1秒,让PowerPoint处理完上一个操作 '上一版本(刘)会停留一秒,但插入图片太慢了现在去掉了 'Application.Wait Now + TimeValue("00:00:01") ' 准备填充下一页幻灯片 'slideIndex = slideIndex + 1 'End If Next row ' 结束后保存PPT 'wdPress.Save MsgBox "数据插入完成!" Set wdDoc = Nothing Set wdApp = Nothing Set ws = Nothing Exit Sub ErrorHandler: If Response = vbYes Then ' 如果用户选择是,关闭PowerPoint并退出宏 If Not wdApp Is Nothing Then wdApp.Quit Set wdApp = Nothing End If Exit Sub Else ' 如果用户选择否,尝试从下一个操作继续 Resume Next End If End Sub
最新发布
07-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值