首页 > 解决方案 > 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 或编程方面不太熟练。

标签: vbaprintingms-word

解决方案


您是否尝试过添加一个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:这里有三个选项,因为我无法复制您的问题,因此无法自己测试...


推荐阅读