Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-05-2023, 02:29 PM
fops fops is offline Close inactive document Windows 10 Close inactive document Office 2019
Novice
Close inactive document
 
Join Date: Mar 2023
Posts: 6
fops is on a distinguished road
Default Close inactive document

Hi,
i want to close a document automatically. It works for an active doc which is in the foreground. When I open another doc and place the doc i want to get closed in the background, the doc will not close.

To close the doc i have:

Application.OnTime When:=Now + TimeValue("00:00:30"), Name:="CloseDoc"


In function CloseDoc() is have:


Sub CloseDoc()
'Documents("file.docm").Activate


'ThisDocument.Close SaveChanges:=wdSaveChanges

Dim wdApp As Word.Application
Set wdApp = GetObject(, "Word.Application")
wdApp.Documents("file.docm").Close SaveChanges:=wdSaveChanges
End Sub


I've also tried the uncommented statements in the Sub, but none of them did it.
Any hint to solve the prob?

Thank you in advance & greetings
fops
Reply With Quote
  #2  
Old 04-05-2023, 05:59 PM
Guessed's Avatar
Guessed Guessed is offline Close inactive document Windows 10 Close inactive document Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
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
  #3  
Old 04-05-2023, 09:27 PM
gmayor's Avatar
gmayor gmayor is offline Close inactive document Windows 10 Close inactive document Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Are you running this code from Word or from some other application such as Excel?
You have used an application.ontime version from Word VBA, but also used Set wdApp = GetObject(, "Word.Application") which suggests another application. This is unnecessary when you are already working in Word.

If you are running the code in Excel, you need to change the line to
Code:
Application.OnTime EarliestTime:=Now + TimeValue("00:00:30"), Procedure:="CloseDoc"
and the macro should be
Code:
Sub CloseDoc()
Dim oDoc As Object
Dim wdApp As Object
    Set wdApp = GetObject(, "Word.Application")
    For Each oDoc In wdApp.Documents
        If LCase(oDoc.Name) = "file.docm" Then
            oDoc.Close -1
            Exit For
        End If
    Next oDoc
End Sub
where file.docm is the name of the file you want to close.
If you are running the code from Word (not the document with the code) then you only need
Code:
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
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #4  
Old 04-06-2023, 04:05 AM
fops fops is offline Close inactive document Windows 10 Close inactive document Office 2019
Novice
Close inactive document
 
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
  #5  
Old 04-06-2023, 05:04 PM
Guessed's Avatar
Guessed Guessed is offline Close inactive document Windows 10 Close inactive document Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
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

Your sample code shows you are looking to close a docm file - is the code being run contained in the same document you are trying to close?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 04-06-2023, 05:27 PM
fops fops is offline Close inactive document Windows 10 Close inactive document Office 2019
Novice
Close inactive document
 
Join Date: Mar 2023
Posts: 6
fops is on a distinguished road
Default

Yes, the code is placed in the same document.
Reply With Quote
  #7  
Old 04-06-2023, 10:40 PM
Guessed's Avatar
Guessed Guessed is offline Close inactive document Windows 10 Close inactive document Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
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

There is a grey area here - you are running code which closes the file that the code is executing from - while it is executing. This may be the source of your problem.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 04-07-2023, 05:38 AM
fops fops is offline Close inactive document Windows 10 Close inactive document Office 2019
Novice
Close inactive document
 
Join Date: Mar 2023
Posts: 6
fops is on a distinguished road
Default

Hi,

then, if i am right in my assumption, i could create a template, a blank template, (never worked with templates ) with one of your codes only and then check for my file.docm if open and then close it.


Additionally i would have in my file.docm:
---------------------------------------------------------------------------------

Sub CloseDoc()
ThisDocument.Close SaveChanges:=wdSaveChanges
End Sub

...

Application.OnTime When:=Now + TimeValue("00:00:30"), Name:="CloseDoc"

...
---------------------------------------------------------------------------------

just for the case the only document open is file.docm.
Is that approx. right?

Greetings
fops
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to dectect if close document and now no document open skarden Word VBA 2 10-24-2022 07:03 AM
Close inactive document Close Document Without Saving Prompt Joe528 Word 3 10-08-2021 06:12 PM
Document close if/then/else error eduzs Word VBA 1 01-02-2021 05:56 AM
Save and Close powerpoint if it is inactive MetteGaga PowerPoint 0 04-16-2015 05:14 AM
Prompt when close the document ubns Word 15 04-29-2012 10:07 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:17 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft