2
votes

This code is in a word macro template in the word start up menu. It can be executed from any word document.

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

When I run this code I get an error message "File not found" if I have another MS Word file open when I opened this instance of a word document and executed the macro.

Length is > 0 Filename (Greenbriar - Invoice.pdf) is correct and present. (there is always only one pdf with "Invoice" in the filename in any given folder)

I do not get this error message if no other word document is open.

How can I modify the code to run even if another Word document is open?

Thanks in advance.

1
Something to do with the ActiveDocument call I assume...Chrismas007

1 Answers

1
votes

ActiveDocument.Path refers to the directory of the other document when it is open. ThisDocument.Path should refer to the directory that contains the template that contains the code. See here for a nice discussion.