Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当Word的另一个实例打开时,无法从VBA宏终止文件_Vba_Ms Word_Kill - Fatal编程技术网

当Word的另一个实例打开时,无法从VBA宏终止文件

当Word的另一个实例打开时,无法从VBA宏终止文件,vba,ms-word,kill,Vba,Ms Word,Kill,此代码位于word启动菜单中的word宏模板中。它可以从任何word文档执行 Dim myFile As String Dim myPath As String myPath = ActiveDocument.Path & "\" myFile = Dir$(myPath & "*Invoice.pdf") If Len(myFile) > 0 Then Debug.Print Len(myFile) Debug.Print myFile Kill

此代码位于word启动菜单中的word宏模板中。它可以从任何word文档执行

Dim myFile As String
Dim myPath As String
myPath = ActiveDocument.Path & "\"
myFile = Dir$(myPath & "*Invoice.pdf")

If Len(myFile) > 0 Then
    Debug.Print Len(myFile)
    Debug.Print myFile
    Kill myFile
End If
当我运行此代码时,如果在打开Word文档的此实例并执行宏时打开了另一个MS Word文件,则会收到一条错误消息“File not found”

长度>0 文件名(Greenbriar-Invoice.pdf)正确且存在。(在任何给定文件夹中,文件名中始终只有一个带有“发票”的pdf)

如果没有打开其他word文档,则不会收到此错误消息

即使打开了另一个Word文档,如何修改代码以运行


提前感谢。

ActiveDocument。路径
指打开时其他文档的目录<代码>此文档。路径应指包含包含代码的模板的目录。有关精彩的讨论,请参阅。

与我假设的
ActiveDocument
调用有关的内容。。。