View Single Post
 
Old 04-05-2023, 05:59 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

If you are explicit about what document you want to close then you need to handle an error if that document isn't open. You might also want to ensure it isn't the foreground doc. This code looks through the current docs to close a specific one only if it is not the active document.
Code:
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
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote