View Single Post
 
Old 04-06-2023, 04:05 AM
fops fops is offline Windows 10 Office 2019
Novice
 
Join Date: Mar 2023
Posts: 6
fops is on a distinguished road
Default

Hi,


first thank you guys for replying to my post.

Yes Im runnig the code from Word.


I tried:


Sub CloseADoc()
Dim aDoc As Document
For Each aDoc In Application.Documents
Debug.Print aDoc.Name
If aDoc.Name = "Document2" And ActiveWindow.Document.Name <> "Document2" Then
aDoc.Close SaveChanges:=False
Exit For
End If
Next aDoc
End Sub



This works only when the document is the document in foreground.

When i debug the proper document/documentname is saved in aDoc.


I also tried this:


Sub CloseDoc()
Dim oDoc As Word.Document
For Each oDoc In Documents
If LCase(oDoc.Name) = "file.docm" Then
oDoc.Close -1
Exit For
End If
Next oDoc
End Sub



Same as with the first code. Only works if document is in foreground / has focus.


I additionally tried:


Documents("C:\Path\to\file.docm").Close SaveChanges:=wdDoNotSaveChanges


Same as above.


Cant find anything that would close the document if two (or more) documents are opened und the doc i want to close is somewhere in the background / has not focus.


Greetings
fops


Edit: I forgot to say its only related to Word. When i open the document and unfocus with e.g. win-explorer everything works like it should. Only with two or more word documents/windows it cant be closed when its unfocused or another document is the active one.
Reply With Quote