vba - MS Word 宏中的延迟
问题描述
我在 MS Word 中使用宏来打印文档(到虚拟 pdf 打印机),保存文件并关闭文档:
Sub PrintClose()
ActiveDocument.PrintOut Copies:=1
ActiveDocument.Save
If Application.Documents.Count > 1 Then
ActiveDocument.Close
Else
Application.Quit
End If
End Sub
问题是打印的文档有时不可读。可以打开,但是字符都错了。
我认为问题可能是在正确执行打印之前文档关闭得太早,因为当我在虚拟 pdf 打印机 (PDF24) 中打开打印的文档时,它已经无法读取。因此,我认为问题不在于 pdf 打印机,而在于打印过程。所以我试图在打印和关闭之间添加延迟。
我只能像这样使用 Excels Wait 命令:
CreateObject("Excel.Application").Wait (Now + TimeValue("00:00:05"))
但是无论我把它放在哪里,这个命令总是首先执行。所以 Word 等待指定的时间然后打印然后关闭这并不能解决我的问题。
是否可以在 MS Word 中添加打印和关闭文档之间的延迟?我在 Visual Basic 或编程方面不太熟练。
解决方案
您是否尝试过添加一个DoEvents
?文档
Sub PrintClose()
ActiveDocument.PrintOut Copies:=1
DoEvents
ActiveDocument.Save
If Application.Documents.Count > 1 Then
ActiveDocument.Close
Else
Application.Quit
End If
End Sub
或者,试试@Cindy Meister 的建议:
Sub PrintClose()
Application.Options.PrintBackground:=False
ActiveDocument.PrintOut Copies:=1
ActiveDocument.Save
If Application.Documents.Count > 1 Then
ActiveDocument.Close
Else
Application.Quit
End If
End Sub
或第三个选项:文档
Sub PrintClose()
ActiveDocument.PrintOut Copies:=1
Do While Application.BackgroundPrintingStatus > 0
DoEvents
Loop
ActiveDocument.Save
If Application.Documents.Count > 1 Then
ActiveDocument.Close
Else
Application.Quit
End If
End Sub
PS:这里有三个选项,因为我无法复制您的问题,因此无法自己测试...
推荐阅读
- laravel - laravel 可以资源回字符串而不是数组
- excel - vba 等待命令提示符完成?
- java - 我在 Android Studio 中编写了一个按钮来更改屏幕,但是当我尝试打开它时它一直在崩溃
- node.js - /usr/bin/fuser -k 5003/tcp 在 nodejs 中失败,但在 shell 中工作
- javascript - 从javascript中具有不同属性的2个数组中获取不同的元素
- html - 在 chrome 浏览器的网站右侧显示额外的空白,但在 Firefox 浏览器上工作正常
- clickhouse - 在 Clickhouse 中的多个列上应用 argMax
- haskell - 使用(伪)可变映射调用函数(并更改映射)
- python - Arduino WebSockets 服务器和 Python 客户端之间的冻结和连接问题
- javascript - 使用 axios 的不一致数据响应